我在/etc/rsyslog.conf有这个:
$template local1DynFile,"/path/to/my/log/%programname%.log.%NOW% $template local1LogFormat,"%msg:2:$:%\n" *,*;auth,authpriv,local0,local1.none ~/var/log/syslog local1.* ?local1DynFile;local1LogFormat
然后我有以下Python脚本( test.py ):
import logging import logging.handlers logger = logging.getLogger('pxet.foo') logger.addHandler(logging.handlers.SysLogHandler(address='/run/systemd/journal/syslog', facility='local1')) logger.handlers[0].setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG) logger.debug('test cockroach is a bug')
如果我做到以下几点:
rm -rf /path/to/my/log systemctl restart rsyslog python test.py
我没有得到日志消息。 然而:
mkdir -p /path/to/my/log python test.py
这一切工作。
如果不存在,是否可以让rsyslog创build目录,还是我自己必须这样做?
文档( http://www.rsyslog.com/doc/v8-stable/configuration/action/index.html#omfile-specific-configuration-statements )表示,rsyslog有$CreateDirs传统configuration选项来控制是否根据需要创build目录。 (对于下一个configuration格式,有一个类似命名的选项CreateDirs ,但是您的示例使用的是旧格式)。请尝试下面这行:
$CreateDirs on
在你之前
local1.* ?local1DynFile;local1LogFormat
线。