aboutsummaryrefslogtreecommitdiffstats
path: root/common.py
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem@fripost.org>2024-06-11 22:08:49 +0200
committerGuilhem Moulin <guilhem@fripost.org>2024-06-11 22:08:54 +0200
commit42cfe748e23bad6fc51b14a3e5896a77fdb5c1d5 (patch)
treec356f25e90bda1d92b2be51e9c09c1cc677bd752 /common.py
parentf91fbc6972eb7f1451ea604b232eff8506d2c6ab (diff)
Use systemd.journal to log to journald when sarted via .service files.
This enables proper filtering by level etc. (incl. journald coloring).
Diffstat (limited to 'common.py')
-rw-r--r--common.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/common.py b/common.py
index 3eb8a3e..88a0f4d 100644
--- a/common.py
+++ b/common.py
@@ -27,6 +27,22 @@ from xdg.BaseDirectory import xdg_config_home
import logging
import yaml
+def init_logger(app=__file__, level=logging.WARNING):
+ log_fmt = logging.Formatter('%(levelname)s: %(message)s')
+ log = logging.getLogger()
+ log.setLevel(level)
+
+ if os.getenv('SYSTEMD_EXEC_PID', None) is None:
+ ch = logging.StreamHandler()
+ else:
+ # started in systemd, use journald for filtering incl. coloring
+ from systemd.journal import JournalHandler
+ ch = JournalHandler(SYSLOG_IDENTIFIER=app)
+
+ ch.setFormatter(log_fmt)
+ log.addHandler(ch)
+ return log
+
def load_config(path=None, groupnames=None):
if path is None:
for p in [Path(),