为了编写" 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)
/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
根据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似乎过时了,我无法找到它的方式。不确定是否可以找到解决问题的方法。
编辑:
rsyslog.conf
中定义了,所以我改为使用&#39; local5&#39; one。答案 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
期望获取消息的特殊格式,如果没有,则默认添加一些信息(时间戳主机名)。可以修改它们。无论如何,从我的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
)提供的文档当然与已安装的版本相匹配,并提供了我在在线文档中找不到的提示。