How to configure rsyslog for use with SysLogHandler logging class?

戏子无情 提交于 2019-12-04 16:47:58

Cause of the problem

I finally found out that I previously created /var/log/local5.log with inappropriate owner and group (root:root). They were inappropriate because /etc/rsyslog.conf tells explicitely owner and group should be syslog:syslog:

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

Unfortunately, the other log files rsyslog should take care of (like auth.log) were also root:root, so, seen from ls -lah, mine was not different from others... (what are also empty, I wonder why such a non-functional configuration is installed by default).

Unfortunately, rsyslog does not log any error (or at least I haven't found where).

Some more details that could be useful to finish rsyslog configuration

As a side note, rsyslog expects a special format for the messages it gets, and if it doesn't, it adds some informations, by default (timestamp hostname). It's possible to modify them. Anyway, from my python script, I decided to only send the message to log and let rsyslog format the output. So finally, the relevant parts of my logging configuration file are:

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]

And I added a customized template in /etc/rsyslog.conf:

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

and accordingly modified /etc/rsyslog.d/40-local.conf:

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

I also want to mention that the documentation provided by the matching package (rsyslog-doc for ubuntu) matches the installed version, of course, and provides hints I hadn't found in the online documentation.

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