写入单独的日志文件 [英] Writing in separate log files

查看:36
本文介绍了写入单独的日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将不同类型的条目写入来自应用程序的单独日志文件中.由于我试图找出的原因,所有条目都出现在所有日志文件中.我可能做错了什么?

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 ?

我只希望关键条目进入/tmp/log/critical.log 和调试条目进入/tmp/log/debug.log 文件,而所有条目都可以进入/tmp/log/all.log 日志文件.

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.

以下是/etc/rsyslog.conf 文件中的条目

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

我的示例 C 程序编写系统日志条目...

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

推荐答案

这里的关键是(正如您可能已经猜到的)默认是在您选择的级别和低于它的级别进行记录.您可以通过修改选择器比较在 syslog 配置文件中更改它.如果没有指定默认是>=,你要=:

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

这篇关于写入单独的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆