diff options
author | Guilhem Moulin <guilhem@fripost.org> | 2024-06-11 22:08:49 +0200 |
---|---|---|
committer | Guilhem Moulin <guilhem@fripost.org> | 2024-06-11 22:08:54 +0200 |
commit | 42cfe748e23bad6fc51b14a3e5896a77fdb5c1d5 (patch) | |
tree | c356f25e90bda1d92b2be51e9c09c1cc677bd752 | |
parent | f91fbc6972eb7f1451ea604b232eff8506d2c6ab (diff) |
Use systemd.journal to log to journald when sarted via .service files.
This enables proper filtering by level etc. (incl. journald coloring).
-rw-r--r-- | common.py | 16 | ||||
-rwxr-xr-x | webmap-download | 2 | ||||
-rwxr-xr-x | webmap-import | 2 |
3 files changed, 18 insertions, 2 deletions
@@ -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(), diff --git a/webmap-download b/webmap-download index a4a6413..fc5ceee 100755 --- a/webmap-download +++ b/webmap-download @@ -133,7 +133,7 @@ def download(url, dest, dir_fd=None, headers={}, session=requests, progress=None common.format_time(elapsed), common.format_bytes(int(size/elapsed))) if __name__ == '__main__': - logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) + common.init_logger(app=os.path.basename(__file__), level=logging.INFO) parser = argparse.ArgumentParser(description='Download or update GIS layers.') parser.add_argument('--cachedir', default=os.curdir, diff --git a/webmap-import b/webmap-import index 1bb235a..5cfe003 100755 --- a/webmap-import +++ b/webmap-import @@ -1013,7 +1013,7 @@ def importSource2(lyr_dst, path, args={}, basedir=None, extent=None): if __name__ == '__main__': - logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) + common.init_logger(app=os.path.basename(__file__), level=logging.INFO) parser = argparse.ArgumentParser(description='Extract and import GIS layers.') parser.add_argument('--cachedir', default=None, |