如何配置rsyslog以与SysLogHandler日志记录类一起使用?

时间:2016-04-22 11:48:00

标签: python python-3.x logging rsyslog

为了编写" myapp"的日志消息。进入/var/log/local5.log,我使用SysLogHandler

问题

" MyApp的"运行良好,没有错误,但没有记录,/var/log/local5.log仍为空。

日志记录配置

日志配置文件的相关部分:

handlers:
    mainHandler:
        class: logging.handlers.SysLogHandler
        level: INFO
        formatter: defaultFormatter
        address: '/dev/log'
        facility: 'local5'

loggers:
    __main__:
        level: INFO
        handlers: [mainHandler]

记录测试

以下是我尝试在" myapp"的主脚本中编写日志的方法:

with open('myconfig.yml') as f:
    logging.config.dictConfig(yaml.load(f))

log = logging.getLogger(__name__)
log.info("Starting")

我已向sys.stderr.write()添加了一些/usr/lib/python3.4/logging/handlers.py,以了解发生了什么,我得到了:

$ myapp
[SysLogHandler._connect_unixsocket()] Sucessfully connected to socket: /dev/log
[SysLogHandler.emit()] called
[SysLogHandler.emit()] msg=b'<174>2016/04/23 07:17:00.453 myapp: main: Starting\x00'
[SysLogHandler.emit()] msg sent to unix socket (no OSError)

rsyslog配置

  • /etc/rsyslog.conf(相关部分;禁用TCP和UDP系统日志接收):

    $ModLoad imuxsock # provides support for local system logging
    $ModLoad imklog   # provides kernel logging support
    
    [...]
    
    $IncludeConfig /etc/rsyslog.d/*.conf
    
  • /etc/rsyslog.d/40-local.conf

    local5.*                        /var/log/local5.log
    

rsyslog测试

根据lsof输出,看起来rsyslogd正在听/dev/log(或者我错了?):

# lsof | grep "/dev/log"
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
rsyslogd  28044           syslog    0u     unix 0xffff8800b4b9b100       0t0    3088160 /dev/log
in:imuxso 28044 28045     syslog    0u     unix 0xffff8800b4b9b100       0t0    3088160 /dev/log
in:imklog 28044 28046     syslog    0u     unix 0xffff8800b4b9b100       0t0    3088160 /dev/log
rs:main   28044 28047     syslog    0u     unix 0xffff8800b4b9b100       0t0    3088160 /dev/log

我没有提出整个rsyslogd -N1输出,因为它有点长,但提及&#34;本地&#34;行:

# rsyslogd -N1 | grep local
rsyslogd: version 7.4.4, config validation run (level 1), master config /etc/rsyslog.conf
3119.943361369:7f39080fc780: cnf:global:cfsysline: $ModLoad imuxsock # provides support for local system logging
3119.944034769:7f39080fc780: rsyslog/glbl: using '127.0.0.1' as localhost IP
3119.946084095:7f39080fc780: requested to include config file '/etc/rsyslog.d/40-local.conf'
3119.946135638:7f39080fc780: config parser: pushed file /etc/rsyslog.d/40-local.conf on top of stack
3119.946432390:7f39080fc780: config parser: resume parsing of file /etc/rsyslog.d/40-local.conf at line 1
3119.946678298:7f39080fc780: config parser: reached end of file /etc/rsyslog.d/40-local.conf
3119.946697644:7f39080fc780: Decoding traditional PRI filter 'local5.*'
3119.946723904:7f39080fc780: symbolic name: local5 ==> 168
3119.949560475:7f39080fc780: PRIFILT 'local5.*'
3119.949675782:7f39080fc780:   ACTION 0x224cda0 [builtin:omfile:/var/log/local5.log]
3119.953397587:7f39080fc780: PRIFILT 'local5.*'
3119.953806713:7f39080fc780:   ACTION 0x224cda0 [builtin:omfile:/var/log/local5.log]
rsyslogd: End of config validation run. Bye.

我不明白我错过了什么。与我使用的版本(7.4.4)相匹配的rsyslog's documentation似乎过时了,我无法找到它的方式。不确定是否可以找到解决问题的方法。

编辑:

  • 无法定义&#34;个人&#34;设施,像&#34; myapp&#34; (即使它在rsyslog.conf中定义了,所以我改为使用&#39; local5&#39; one。

1 个答案:

答案 0 :(得分:3)

问题原因

我终于发现我之前使用不当的所有者和群组(/var/log/local5.log)创建了root:root。它们不合适,因为/etc/rsyslog.conf明确表示所有者和群组应该是syslog:syslog

#
# Set the default permissions for all log files.
#
$FileOwner syslog
$FileGroup adm
$FileCreateMode 0640
$DirCreateMode 0755
$Umask 0022
$PrivDropToUser syslog
$PrivDropToGroup syslog

不幸的是,其他日志文件rsyslog应该处理auth.log也是root:root,因此,从ls -lah看,我的与其他文件没有什么不同...(也是空的,我想知道为什么默认安装这样的非功能配置。)

不幸的是,rsyslog没有记录任何错误(或者至少我没有找到错误)。

可能有助于完成rsyslog配置的更多细节

作为旁注,rsyslog期望获取消息的特殊格式,如果没有,则默认添加一些信息(时间戳主机名)。可以修改它们。无论如何,从我的python脚本,我决定只将消息发送到日志并让rsyslog格式化输出。最后,我的日志配置文件的相关部分是:

formatters:
    rsyslogdFormatter:
        format: '%(filename)s: %(funcName)s: %(message)s'

handlers:
    mainHandler:
        class: logging.handlers.SysLogHandler
        level: INFO
        formatter: rsyslogdFormatter
        address: '/dev/log'
        facility: 'local5'

loggers:
    __main__:
        level: INFO
        handlers: [mainHandler]

我在/etc/rsyslog.conf中添加了自定义模板:

$template MyappTpl,"%$now% %timegenerated:12:23:date-rfc3339% %syslogtag%%msg%\n"

并相应地修改了/etc/rsyslog.d/40-local.conf

local5.*                        /var/log/local5.log;MyappTpl

我还想提一下,匹配包(ubuntu的rsyslog-doc)提供的文档当然与已安装的版本相匹配,并提供了我在在线文档中找不到的提示。