Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing in separate log files

Tags:

c

syslog

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();
}
like image 706
Me Unagi Avatar asked Oct 02 '13 08:10

Me Unagi


People also ask

How do I write logs in a separate file?

- Create a new appender by copying the FILE appender and change the name and path according to the requirement. - Add a threshold parameter to the new appender with the value set to the level to be logged. This will mean that all entries having this log level and above will be written into the file.

Can log files be edited?

Click Open/Close Log File on the Edit menu to write to a new log file, append to an existing log file, or close an open log file.

What does .log mean on a file?

Log files are a historical record of everything and anything that happens within a system, including events such as transactions, errors and intrusions.


1 Answers

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 !.

like image 68
ralight Avatar answered Oct 03 '22 21:10

ralight