如何使用rsyslogd登录/var/log/mail.log?

时间:2018-03-15 18:19:49

标签: c linux logging glibc rsyslog

我目前正在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);
}

这个&#34;应用程序&#34;编译,当我执行它时,它会在/var/log/syslog中生成一个条目,但/var/log/mail.log中的条目和{{1}中的条目}。

有人可以解释一下原因吗?

我在测试机器上使用/var/log/mail.err;这是来自rsyslogd的配置(请注意,/etc/rsyslog.conf只是空的,并且我已删除了所有明显与此问题无关的评论和行:)< / p>

/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.confrsyslogd的优先级为LOG_MAIL的邮件写入LOG_ERR。但是,对于文件路径前面有/var/log/mail.err的行,我有点不信任。我不知道这意味着什么,因为我在手册中找不到任何暗示。

那出了什么问题?

1 个答案:

答案 0 :(得分:0)

我讨厌回答我自己的问题,但我认为我在文档或glibc的源代码中发现了一个错误,并且我希望将此文档记录给此问题的未来访问者。

来自https://www.gnu.org/software/libc/manual/html_node/syslog_003b-vsyslog.html#syslog_003b-vsyslog(截至撰写本文时):

  

syslog提交具有设施和优先级的消息   facility_priority。宏LOG_MAKEPRI生成设施/优先级   来自设施和优先级,如下例所示:

     

LOG_MAKEPRI(LOG_USER,LOG_WARNING)

现在看看syslog.h中的一些代码,因为它位于我的测试机器上(Debian wheezy,最新版本,没有自定义补丁,非相关部分被删除):

/*
 * 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 */

我们显然有很多问题。

  • 顶部的评论:如果我有3个底部位,那么我有29个顶部位(而不是28位)。但这是一个小问题。

  • 设施代码已定义为向左移位三位。使用宏LOG_MAKEPRI(按照上面链接的手册页推荐)显然会将它们再次向左移三位,这显然是错误的。

<强>解

解决方案很简单:不要使用该宏;相反,只需OR设施代码和优先级代码。我试过了,但它确实奏效了。根据{{​​1}}中rsyslogd的配置,我的测试程序的错误消息现在按预期记录。

我对syslog.h中那个非常明显的错误感到非常惊讶......