我正在使用systemd来pipe理Ubuntu 16上的程序。我似乎无法从程序中获取日志以输出到journalctl或/var/log/syslog 。 日志configuration是缺省configuration,至less应将所有日志消息<debug到syslog。 这里是杂志的conf:
>> cat /etc/systemd/journald.conf # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # Entries in this file show the compile time defaults. # You can change settings by editing this file. # Defaults can be restored by simply deleting this file. # # See journald.conf(5) for details. [Journal] #Storage=auto #Compress=yes #Seal=yes #SplitMode=uid #SyncIntervalSec=5m #RateLimitInterval=30s #RateLimitBurst=1000 #SystemMaxUse= #SystemKeepFree= #SystemMaxFileSize= #SystemMaxFiles=100 #RuntimeMaxUse= #RuntimeKeepFree= #RuntimeMaxFileSize= #RuntimeMaxFiles=100 #MaxRetentionSec= #MaxFileSec=1month #ForwardToSyslog=yes #ForwardToKMsg=no #ForwardToConsole=no #ForwardToWall=yes #TTYPath=/dev/console #MaxLevelStore=debug #MaxLevelSyslog=debug #MaxLevelKMsg=notice #MaxLevelConsole=info #MaxLevelWall=emerg
程序定义是:
[Unit] Description=My Program After=network.target [Service] User=vagrant Restart=always WorkingDirectory=/vagrant/transporter Environment=PORT=8080 ExecStart=/usr/bin/python3.5 catcher.py
当我在程序中进行print呼叫时,在journalctl或/var/log/syslog journalctl不到任何输出。 看起来这些日志不是journalctl ,这是第一个问题,但我不明白如何改变它的configuration来获取这些。 如果我从shell中手动运行程序( python3.5 catcher.py ),我会在控制台上看到打印输出。
我已经尝试运行更改ExecStart使用python3.5 -u以便不缓冲标准输出,但没有工作。 我也尝试将journald.conf ForwardToConsole设置更改为yes,然后重新启动systemd-journald服务。
任何帮助,将不胜感激。
我解决了这个通过使用这个stackoverflow文章中描述的python-systemd包装包。 这是它的要点:
import logging from systemd.journal import JournalHandler log = logging.getLogger('demo') log.addHandler(JournalHandler()) log.setLevel(logging.INFO) log.info("sent to journal")
请注意,您需要安装python-systemd的OS系统软件包(例如Ubuntu上的apt-get install python-systemd ),而不是pip软件包。 显然他们是不同的。