Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to redirect syslog messages to stdout?

Tags:

c

linux

syslog

I have an application that can run in two modes, either with a CLI, or as a daemon.

I am using syslog() for logging. However, when run in CLI mode I'd like all the logging, except those marked LOG_DEBUG, messages to be sent to the console instead of logged.

I have tried to use setlogmask(), but that doesn't seem to redirect to console.

What is the best way to do this?

like image 401
Brandon Yates Avatar asked May 08 '13 16:05

Brandon Yates


2 Answers

As GNU-specific solution I would suggest using openlog(NULL, LOG_PERROR, your_facility). Not customizable(just duplicate on stderr).

like image 132
KAction Avatar answered Sep 20 '22 08:09

KAction


As suggested in the comments by maverik, I wrote a wrapper around syslog that determines whether to send the output to the log or console. Here it is in case anyone ever needs this.

void mylog (int level, const char *format, ...)
{
    va_list args;
    va_start (args, format);

    if (remote)
    {
        vsyslog(level, format, args);
    }   
    else
    {
        if (level == LOG_DEBUG)
            vsyslog(level, format, args);
        else
            vprintf(format, args);
    }
    va_end(args);
}
like image 31
Brandon Yates Avatar answered Sep 21 '22 08:09

Brandon Yates