我想让logging.info()访问journald (systemd)。
到目前为止,我只找到了读取日志的python模块(不是我想要的),或者像这样工作的模块:journal.send('Hello world')
发布于 2016-01-04 18:33:04
python-systemd有一个JournalHandler,您可以将其与日志记录框架一起使用。
从文档中:
import logging
from systemd.journal import JournalHandler
log = logging.getLogger('demo')
log.addHandler(JournalHandler())
log.setLevel(logging.INFO)
log.info("sent to journal")发布于 2017-03-05 12:02:18
作为官方包的替代,systemd package可以与Python3.6一起使用。它的来源也在github上。
这个实现是官方lib的镜像,只做了一些小改动:
import logging
from systemd import journal
log = logging.getLogger('demo')
log.addHandler(journal.JournaldLogHandler())
log.setLevel(logging.INFO)
log.info("sent to journal")或者一种更短的方法:
from systemd import journal
journal.write("Hello Lennart")发布于 2021-11-26 17:53:06
这是一个没有第三方模块的解决方案。它对我来说工作得很好,消息会出现在日志中。
import logging
import sys
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# this is just to make the output look nice
formatter = logging.Formatter(fmt="%(asctime)s %(name)s.%(levelname)s: %(message)s", datefmt="%Y.%m.%d %H:%M:%S")
# this logs to stdout and I think it is flushed immediately
handler = logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info('test')https://stackoverflow.com/questions/34588421
复制相似问题