大网数据广告活动
服务器运维问答

Not seeing logs for program managed by systemd

· 52CE 我爱测

I'm using systemd to manage a program on Ubuntu 16. I can't seem to get the logs from the program to output to journalctl or /var/log/syslog . The journald configuration is the default, which should forward all log messages < deb…

问题原文

原帖链接 · lps · CC BY-SA 3.0

I'm using systemd to manage a program on Ubuntu 16. I can't seem to get the logs from the program to output to journalctl or /var/log/syslog. The journald configuration is the default, which should forward all log messages < debug to syslog, at least. Here is the journald 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

The program definition is:

[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

When I do a print call in the program I don't see any output in journalctl or /var/log/syslog. It appears that the logs are not hitting journalctl, which is the first problem, but I don't understand how to change the configuration for it to get those. If I run the program manually (python3.5 catcher.py) from a shell I do see the print output on the console.

I have tried running changing ExecStart to use python3.5 -u so as to not buffer stdout, but that did not work. I've also tried changing the journald.conf ForwardToConsole setting to yes, and restarted the systemd-journald service.

Any help would be appreciated.

已采纳解答

原帖链接 · lps · CC BY-SA 3.0

I resolved this by using the python-systemd wrapper package as described in this stackoverflow article. Here is the gist of it:

import logging
from systemd.journal import JournalHandler

log = logging.getLogger('demo')
log.addHandler(JournalHandler())
log.setLevel(logging.INFO)
log.info("sent to journal")

Note, you'll need to install the OS system package of python-systemd (e.g. apt-get install python-systemd on Ubuntu), not the pip package. Apparently they are different.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-10-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。