【发布时间】:2016-11-22 17:33:44
【问题描述】:
这是我正在使用的python。
$ python3 --version
Python 3.5.2
这是经过谷歌搜索后的测试代码 (How to log to journald (systemd) via Python?)。我正在尝试使用日志。
#!/usr/bin/python3
import logging
from systemd.journal import JournalHandler
log = logging.getLogger('test')
log.addHandler(JournalHandler())
log.setLevel(logging.DEBUG)
log.warning("warn")
log.info("info")
log.error("error")
log.debug("debug")
我希望在日志中看到如下内容:
WARNING: warn
INFO: info
ERROR: error
DEBUG: debug
但这就是实际显示的内容:
Nov 22 09:29:56 host1 ./test_log.py[8997]: warn
Nov 22 09:29:56 host1 ./test_log.py[8997]: info
Nov 22 09:29:56 host1 ./test_log.py[8997]: error
Nov 22 09:29:56 host1 ./test_log.py[8997]: debug
日志消息没有日志级别前缀。感谢您的帮助。
更多信息,
我也尝试格式化。
logging.basicConfig(format='%(levelname)s %(message)s')
然后在标准输出上,我可以看到日志级别,但仍然没有在日志中。
【问题讨论】:
标签: python-3.x