Writing in separate log files

安稳与你 提交于 2019-12-21 04:41:03

问题


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();
}

回答1:


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



来源:https://stackoverflow.com/questions/19132820/writing-in-separate-log-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!