Me Unagi
Me Unagi

Reputation: 625

Writing in separate log files

I am trying to write different type of entries in separate log files from an application. For reason which I am trying to find out, all entries appear in all log files. What could I be doing wrong ?

I want only critical entries to go in /tmp/log/critical.log and debug entries to go into /tmp/log/debug.log file while all enteries can go into /tmp/log/all.log log file.

Following are entries in /etc/rsyslog.conf file

local0.*                                                /tmp/log/all.log
local0.alert                                            /tmp/log/alert.log
local0.crit                                             /tmp/log/critical.log
local0.debug                                            /tmp/log/debug.log
local0.emerg                                            /tmp/log/emergency.log
local0.err                                              /tmp/log/error.log
local0.info                                             /tmp/log/info.log
local0.notice                                           /tmp/log/notice.log
local0.warning                                          /tmp/log/warning.log

My sample c program writing syslog entries...

#include<syslog.h>

main()
{
    openlog("myapp",LOG_CONS|LOG_PID|LOG_NDELAY,LOG_LOCAL0);

    syslog(LOG_EMERG|LOG_LOCAL0,"Emergency",getuid());
    syslog(LOG_ALERT|LOG_LOCAL0,"Alert",getuid());
    syslog(LOG_CRIT|LOG_LOCAL0,"Critical",getuid());
    syslog(LOG_ERR|LOG_LOCAL0,"Error",getuid());
    syslog(LOG_WARNING|LOG_LOCAL0,"Warning",getuid());
    syslog(LOG_NOTICE|LOG_LOCAL0,"Notice",getuid());
    syslog(LOG_INFO|LOG_LOCAL0,"Information",getuid());
    syslog(LOG_DEBUG|LOG_LOCAL0,"Debug",getuid());

    closelog();
}

Upvotes: 6

Views: 3648

Answers (1)

ralight
ralight

Reputation: 11618

The key here is that (as you've probably guessed) the default is to log at the level you choose and those below it. You can change that in the syslog config file by modifying the selector comparison. The default if not specified is >=, you want =:

local0.*                                                 /tmp/log/all.log
local0.=alert                                            /tmp/log/alert.log
local0.=crit                                             /tmp/log/critical.log
local0.=debug                                            /tmp/log/debug.log
local0.=emerg                                            /tmp/log/emergency.log
local0.=err                                              /tmp/log/error.log
local0.=info                                             /tmp/log/info.log
local0.=notice                                           /tmp/log/notice.log
local0.=warning                                          /tmp/log/warning.log

As well as <, >, <=, >=, you can negate the comparison using !.

Upvotes: 4

Related Questions