Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting standard output to syslog

I'm planning to package OpenTibia Server for Debian. One of the things I want to do is add startup via /etc/init.d and daemonization of the otserv process.

Thing is, we should probably redirect output to syslog. This is usually done via the syslog() function. Currently, the code is swarmed with:

std::cout << "Stuff to printout" << std::endl;

Is there a proper, easy to add, way to redirect standard output and standard error output into syslog without replacing every single "call" to std::cout and friends?

like image 489
Ivan Vučica Avatar asked Mar 20 '09 09:03

Ivan Vučica


People also ask

Which one is the redirecting standard error to standard output?

The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.

How do I redirect stderr and stdout to a log file?

Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout , use the 2>&1 or &> constructs.

Does stderr go to syslog?

Original Answer By default stdout and stderr of a systemd unit are sent to syslog.


1 Answers

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

like image 95
Alnitak Avatar answered Sep 20 '22 15:09

Alnitak