Redirecting standard output to syslog Redirecting standard output to syslog unix unix

Redirecting standard output to syslog


You can pipe your stdout to syslog with the logger command:

NAME

 logger - a shell command interface to the syslog(3) system log module

SYNOPSIS

 logger [-isd] [-f file] [-p pri] [-t tag] [-u socket] [message ...]

DESCRIPTION

 Logger makes entries in the system log.  It provides a shell command interface to the syslog(3) system log module.

If you don't supply a message on the command line it reads stdin


You can redirect any stream in C++ via the rdbuf() command. This is a bit convoluted to implement but not that hard.

You need to write a streambuf that would output to syslog on overflow(), and replace the std::cout rdbuf with your streambuf.

An example, that would output to a file (no error handling, untested code)

#include <iostream>#include <fstream>using namespace std;int main (int argc, char** argv) {   streambuf * yourStreamBuffer = NULL;   ofstream outputFileStream;   outputFileStream.open ("theOutputFile.txt");   yourStreamBuffer = outputFileStream.rdbuf();   cout.rdbuf(yourStreamBuffer);   cout << "Ends up in the file, not std::cout!";   outputFileStream.close();   return 0; }


Not sure whether a straight "C" answer suffices; but in "C" you can use underlying stdio features to plug the (FILE*) directly into syslog calls, without an intervening "logger" process. Check out http://mischasan.wordpress.com/2011/05/25/redirecting-stderr-to-syslog/