我目前正在使用Linux下的日志记录。我做了以下非常简单的测试应用程序(在普通C中):
#include <syslog.h>
int main(int argc, char **argv)
{
openlog("mytest", (LOG_PID | LOG_NDELAY), LOG_MAIL);
syslog(LOG_MAKEPRI(LOG_MAIL, LOG_ERR), "Test");
return(0);
}这个“应用程序”编译,当我执行它时,它在/var/log/syslog中生成一个条目,但是在/var/log/mail.log中不生成条目,在/var/log/mail.err中生成无条目。
谁能解释一下原因吗?
我正在测试机器上使用rsyslogd;这是来自/etc/rsyslog.conf的配置(请注意,/etc/rsyslog.d只是空的,我删除了所有与问题无关的注释和行):
:msg,startswith, " fetching user_deny" ~
:msg,startswith, " seen_db: user " ~
auth,authpriv.* /var/log/auth.log
*.*;auth,authpriv.none -/var/log/syslog
daemon.* -/var/log/daemon.log
kern.* -/var/log/kern.log
lpr.* -/var/log/lpr.log
mail.* -/var/log/mail.log
user.* -/var/log/user.log
mail.info -/var/log/mail.info
mail.warn -/var/log/mail.warn
mail.err /var/log/mail.err
*.=debug;\
auth,authpriv.none;\
news.none;mail.none -/var/log/debug
*.=info;*.=notice;*.=warn;\
auth,authpriv.none;\
cron,daemon.none;\
mail,news.none -/var/log/messages
*.emerg :omusrmsg:*
daemon.*;mail.*;\
news.err;\
*.=debug;*.=info;\
*.=notice;*.=warn |/dev/xconsole据我从阅读man rsyslog.conf中了解到的,这种配置应该使rsyslogd为LOG_MAIL编写具有优先级LOG_ERR到/var/log/mail.err的消息。不过,对于文件路径中有一个-的行,我有点不信任。我不知道这意味着什么,因为我在手册中找不到任何提示。
那到底是怎么回事?
发布于 2018-03-16 07:59:40
我讨厌回答我自己的问题,但我想我已经在文档中或者在glibc的来源中发现了一个bug,我希望为将来的访问者记录这个问题。
来自003b-vsyslog (按撰写本文时所述):
syslog提交带有facility_priority指示的功能和优先级的消息。宏LOG_MAKEPRI从设施和优先级生成设施/优先级,如下例所示: LOG_MAKEPRI(LOG_USER,LOG_WARNING)
现在看看来自syslog.h的一些代码,因为它驻留在我的测试机器上(Debian,最新的,没有自定义修补程序,不相关的部分被去掉):
/*
* priorities/facilities are encoded into a single 32-bit quantity, where the
* bottom 3 bits are the priority (0-7) and the top 28 bits are the facility
* (0-big number). Both the priorities and the facilities map roughly
* one-to-one to strings in the syslogd(8) source code. This mapping is
* included in this file.
*
* priorities (these are ordered)
*/
#define LOG_EMERG 0 /* system is unusable */
#define LOG_ALERT 1 /* action must be taken immediately */
#define LOG_CRIT 2 /* critical conditions */
#define LOG_ERR 3 /* error conditions */
#define LOG_WARNING 4 /* warning conditions */
#define LOG_NOTICE 5 /* normal but significant condition */
#define LOG_INFO 6 /* informational */
#define LOG_DEBUG 7 /* debug-level messages */
#define LOG_MAKEPRI(fac, pri) (((fac) << 3) | (pri))
/* facility codes */
#define LOG_KERN (0<<3) /* kernel messages */
#define LOG_USER (1<<3) /* random user-level messages */
#define LOG_MAIL (2<<3) /* mail system */
#define LOG_DAEMON (3<<3) /* system daemons */
#define LOG_AUTH (4<<3) /* security/authorization messages */
#define LOG_SYSLOG (5<<3) /* messages generated internally by syslogd */
#define LOG_LPR (6<<3) /* line printer subsystem */
#define LOG_NEWS (7<<3) /* network news subsystem */
#define LOG_UUCP (8<<3) /* UUCP subsystem */
#define LOG_CRON (9<<3) /* clock daemon */
#define LOG_AUTHPRIV (10<<3) /* security/authorization messages (private) */
#define LOG_FTP (11<<3) /* ftp daemon */
/* other codes through 15 reserved for system use */
#define LOG_LOCAL0 (16<<3) /* reserved for local use */
#define LOG_LOCAL1 (17<<3) /* reserved for local use */
#define LOG_LOCAL2 (18<<3) /* reserved for local use */
#define LOG_LOCAL3 (19<<3) /* reserved for local use */
#define LOG_LOCAL4 (20<<3) /* reserved for local use */
#define LOG_LOCAL5 (21<<3) /* reserved for local use */
#define LOG_LOCAL6 (22<<3) /* reserved for local use */
#define LOG_LOCAL7 (23<<3) /* reserved for local use */很明显,我们在这方面有很多问题。
LOG_MAKEPRI (如上面链接的手册页面所建议的那样),显然第二次将它们向左移动三位,这显然是错误的。溶液
解决方案很简单:不要使用该宏;相反,只需OR设施代码和优先级代码即可。我试过了,而且成功了。根据rsyslogd在/etc/rsyslog.conf中的配置,我的测试程序中的错误消息现在会按预期记录下来。
我很惊讶于系统中的一个非常明显的错误。
https://stackoverflow.com/questions/49306646
复制相似问题