Python3: Journal Logging Does Not Show Log Level?
This is the python I am using. $ python3 --version Python 3.5.2 This is the test code after some googling (How to log to journald (systemd) via Python?). I am trying to use journa
Solution 1:
Your basicConfig statement will create a StreamHandler, the format you set here will only be for the log you get on stdout. Look at the docs for Formatter objects which says If no fmt is specified, '%(message)s' is used.. Therefore you have to create a separate Formatter and use setFormatter on the JournalHandler to get your desired output.
A basic modification of your code below:
#!/usr/bin/env python
import logging
from systemd.journal import JournalHandler
log = logging.getLogger('test')
log_fmt = logging.Formatter("%(levelname)s %(message)s")
log_ch = JournalHandler()
log_ch.setFormatter(log_fmt)
log.addHandler(log_ch)
log.setLevel(logging.DEBUG)
log.warning("warn")
log.info("info")
log.error("error")
log.debug("debug")
This gives your desired output in the log
Nov2401:16:50localhost.localdomainapp.py[11279]: WARNINGwarnNov2401:16:50localhost.localdomainapp.py[11279]: INFOinfoNov2401:16:50localhost.localdomainapp.py[11279]: ERRORerrorNov2401:16:50localhost.localdomainapp.py[11279]: DEBUGdebug
Post a Comment for "Python3: Journal Logging Does Not Show Log Level?"