From feffc399c8c27ad8a7e6ef277a791c4d21cd2894 Mon Sep 17 00:00:00 2001 From: Meisterlala <6453306+Meisterlala@users.noreply.github.com> Date: Thu, 2 Apr 2026 17:50:45 +0200 Subject: [PATCH] feat(waybar): add journald logging support and refine log configuration --- .chezmoiignore | 3 +- .gitignore | 2 + dot_config/waybar/autogen_window_rewrite.py | 47 +++++++++++++++++++-- 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.chezmoiignore b/.chezmoiignore index 3184887..f37c3c0 100644 --- a/.chezmoiignore +++ b/.chezmoiignore @@ -21,4 +21,5 @@ Documents !.config/oh-my-posh.yaml # pycache -**/__pycache__** +__pycache__/ +*.pyc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a60b85 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +*.pyc diff --git a/dot_config/waybar/autogen_window_rewrite.py b/dot_config/waybar/autogen_window_rewrite.py index f06fd52..133c77e 100755 --- a/dot_config/waybar/autogen_window_rewrite.py +++ b/dot_config/waybar/autogen_window_rewrite.py @@ -13,6 +13,11 @@ import urllib.request from datetime import datetime, timezone from pathlib import Path +try: + from systemd.journal import JournalHandler +except Exception: + JournalHandler = None + NERDFONT_GLYPHS_URL = ( "https://raw.githubusercontent.com/ryanoasis/nerd-fonts/master/glyphnames.json" ) @@ -603,6 +608,43 @@ def send_success_notification(added_mappings: list[tuple[str, str, str]]) -> Non ) +class MaxLevelFilter(logging.Filter): + def __init__(self, max_level: int): + super().__init__() + self.max_level = max_level + + def filter(self, record: logging.LogRecord) -> bool: + return record.levelno <= self.max_level + + +def configure_logging(level_name: str) -> None: + level = getattr(logging, level_name.upper(), logging.INFO) + root = logging.getLogger() + root.setLevel(level) + root.handlers.clear() + + if JournalHandler is not None: + # Native journald logging with proper PRIORITY mapping. + handler = JournalHandler(SYSLOG_IDENTIFIER="waybar-window-rewrite-autogen") + handler.setLevel(logging.DEBUG) + handler.setFormatter(logging.Formatter("%(message)s")) + root.addHandler(handler) + else: + formatter = logging.Formatter("%(levelname)s %(message)s") + + stdout_handler = logging.StreamHandler(sys.stdout) + stdout_handler.setLevel(logging.DEBUG) + stdout_handler.addFilter(MaxLevelFilter(logging.INFO)) + stdout_handler.setFormatter(formatter) + + stderr_handler = logging.StreamHandler(sys.stderr) + stderr_handler.setLevel(logging.WARNING) + stderr_handler.setFormatter(formatter) + + root.addHandler(stdout_handler) + root.addHandler(stderr_handler) + + def main() -> int: parser = argparse.ArgumentParser( description="Auto-generate waybar window-rewrite icons" @@ -649,10 +691,7 @@ def main() -> int: ) args = parser.parse_args() - logging.basicConfig( - level=getattr(logging, args.log_level.upper(), logging.INFO), - format="%(asctime)s %(levelname)s %(message)s", - ) + configure_logging(args.log_level) config_target_path = Path(args.config).expanduser() config_path, chezmoi_apply_path = resolve_config_path_for_chezmoi( -- 2.51.2