From b9832d0b248d4b1aff58cdea2b5b27c44baef8ed Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Mon, 20 Nov 2023 12:03:15 +0100 Subject: [PATCH] fix: journald compatible log level --- setup.cfg | 2 +- src/de/p1st/monitor/print_util.py | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/setup.cfg b/setup.cfg index e71ad53..5b0bd98 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ [metadata] name = de.p1st.monitor -version = 0.11.4 +version = 0.11.5 author = Daniel Langbein author_email = daniel@systemli.org description = periodically monitor and warn diff --git a/src/de/p1st/monitor/print_util.py b/src/de/p1st/monitor/print_util.py index 4cc6677..ddec96b 100644 --- a/src/de/p1st/monitor/print_util.py +++ b/src/de/p1st/monitor/print_util.py @@ -15,32 +15,38 @@ import sys # define SD_DEBUG "<7>" /* debug-level messages */ def print_emerg(message: object): - print(f'<0> {message}', file=sys.stderr) + do_print('<0> ', message, file=sys.stderr) def print_alert(message: object): - print(f'<1> {message}', file=sys.stderr) + do_print('<1> ', message, file=sys.stderr) def print_crit(message: object): - print(f'<2> {message}', file=sys.stderr) + do_print('<2> ', message, file=sys.stderr) def print_err(message: object): - print(f'<3> {message}', file=sys.stderr) + do_print('<3> ', message, file=sys.stderr) def print_warning(message: object): - print(f'<4> {message}', file=sys.stderr) + do_print('<4> ', message, file=sys.stderr) def print_notice(message: object): - print(f'<5> {message}', file=sys.stdout) + do_print('<5> ', message) def print_info(message: object): - print(f'<6> {message}', file=sys.stdout) + do_print('<6> ', message) def print_debug(message: object): - print(f'<7> {message}', file=sys.stdout) + do_print('<7> ', message) + + +def do_print(line_prefix: str, message: object, file=sys.stdout) -> None: + # Prefix each line in str(object) with `line_prefix`. + prefixed = line_prefix + str(message).replace('\n', '\n' + line_prefix) + print(prefixed, file=file)