From e5710b1c76388c541218cf8ffdeb31d1216b42fb Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Thu, 13 Nov 2025 21:21:30 +0100 Subject: [PATCH] Feat: add module name to logs --- .github/workflows/unittests.yml | 2 +- .../linuxmusterLinuxclient7/logging.py | 24 +++++++++++++------ .../tests/test_logging.py | 11 ++++++++- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/.github/workflows/unittests.yml b/.github/workflows/unittests.yml index 0646678..a864289 100644 --- a/.github/workflows/unittests.yml +++ b/.github/workflows/unittests.yml @@ -1,7 +1,7 @@ name: Unittests on: [push] jobs: - run: + run-unittests: runs-on: ${{ matrix.os }} strategy: matrix: diff --git a/usr/lib/python3/dist-packages/linuxmusterLinuxclient7/logging.py b/usr/lib/python3/dist-packages/linuxmusterLinuxclient7/logging.py index 4260166..a0fdbc0 100644 --- a/usr/lib/python3/dist-packages/linuxmusterLinuxclient7/logging.py +++ b/usr/lib/python3/dist-packages/linuxmusterLinuxclient7/logging.py @@ -1,4 +1,4 @@ -import traceback, re, sys, subprocess +import traceback, re, sys, subprocess, inspect from enum import Enum from linuxmusterLinuxclient7 import config @@ -61,11 +61,11 @@ def exception(exception): :param exception: The exception to log :type exception: Exception """ - error("=== An exception occurred ===") - error(str(exception)) + _log(Level.ERROR, "=== An exception occurred ===") + _log(Level.ERROR, str(exception)) # Only use for debugging! This will cause ugly error dialogs in X11 #traceback.print_tb(exception.__traceback__) - error("=== end exception ===") + _log(Level.ERROR, "=== end exception ===") def printLogs(compact=False,anonymize=False): """ @@ -128,6 +128,16 @@ def _log(level, message): if level == Level.FATAL: sys.stderr.write(message) - print("[{0}] {1}".format(level.name, message)) - message = message.replace("'", "") - subprocess.call(["logger", "-t", "linuxmuster-linuxclient7", f"[{level.name}] {message}"]) + try: + frame = inspect.stack()[2] + module = inspect.getmodule(frame[0]) + moduleName = module.__name__.replace("linuxmusterLinuxclient7.", "") + moduleName = f"({moduleName})" + except: + # This happens e.g. when logging from interactive shell + moduleName = "" + + logMessage = f"[{level.name}] {moduleName} {message}" + + print(logMessage) + subprocess.call(["logger", "-t", "linuxmuster-linuxclient7", logMessage]) diff --git a/usr/lib/python3/dist-packages/linuxmusterLinuxclient7/tests/test_logging.py b/usr/lib/python3/dist-packages/linuxmusterLinuxclient7/tests/test_logging.py index 8107372..9aebe16 100644 --- a/usr/lib/python3/dist-packages/linuxmusterLinuxclient7/tests/test_logging.py +++ b/usr/lib/python3/dist-packages/linuxmusterLinuxclient7/tests/test_logging.py @@ -12,7 +12,16 @@ def test_forAllLevels(mockSubprocessCall): logging.exception(Exception("exception")) logs = _getLoggedLogs(mockSubprocessCall) - assert logs == ["[DEBUG] debug", "[INFO] info", "[WARNING] warning", "[ERROR] error", "[FATAL] fatal", "[ERROR] === An exception occurred ===", "[ERROR] exception", "[ERROR] === end exception ==="] + assert logs == [ + "[DEBUG] (tests.test_logging) debug", + "[INFO] (tests.test_logging) info", + "[WARNING] (tests.test_logging) warning", + "[ERROR] (tests.test_logging) error", + "[FATAL] (tests.test_logging) fatal", + "[ERROR] (tests.test_logging) === An exception occurred ===", + "[ERROR] (tests.test_logging) exception", + "[ERROR] (tests.test_logging) === end exception ===" + ] @mock.patch("linuxmusterLinuxclient7.logging.print") @mock.patch("linuxmusterLinuxclient7.logging.open")