Ubuntu – Not seeing logs for program managed by systemd

journaldpythonsystemdUbuntu

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.

Best Answer

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.