From 29b7198f061005fca41d69433e60bbfc84931327 Mon Sep 17 00:00:00 2001 From: Barney Sowood Date: Thu, 16 Oct 2025 18:50:23 +0100 Subject: [PATCH] Fix to ensure that asyncio uses SaltLoggingClass Fixes the salt logging implementation to check asyncio is using the SaltLoggingClass if it has already been imported. If it uses the standard logging.Logger() class then extra fields in the log format such as %(jid)s will cause an exception when asyncio logs anything. --- changelog/68400.fixed.md | 1 + salt/_logging/impl.py | 7 +++++++ .../unit/_logging/test_asyncio_logger.py | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 changelog/68400.fixed.md create mode 100644 tests/pytests/unit/_logging/test_asyncio_logger.py diff --git a/changelog/68400.fixed.md b/changelog/68400.fixed.md new file mode 100644 index 000000000000..763c76de20c8 --- /dev/null +++ b/changelog/68400.fixed.md @@ -0,0 +1 @@ +Fixes issue with asyncio logger not using SaltLoggingClass and causing exceptions when "%(jid)s" is used in a log format. diff --git a/salt/_logging/impl.py b/salt/_logging/impl.py index c4de686f1976..12b36c5da87e 100644 --- a/salt/_logging/impl.py +++ b/salt/_logging/impl.py @@ -559,6 +559,13 @@ def shutdown_temp_handler(): logging.root.addHandler(get_temp_handler()) +# Override asyncio logger class if asyncio is already imported +if "asyncio" in sys.modules: + asyncio_logger = logging.getLogger("asyncio") + if not isinstance(asyncio_logger, SaltLoggingClass): + asyncio_logger.__class__ = SaltLoggingClass + + # Now that we defined the default logging logger class, we can instantiate our logger # DO NOT MOVE THIS log = logging.getLogger(__name__) diff --git a/tests/pytests/unit/_logging/test_asyncio_logger.py b/tests/pytests/unit/_logging/test_asyncio_logger.py new file mode 100644 index 000000000000..d2b42af1ea53 --- /dev/null +++ b/tests/pytests/unit/_logging/test_asyncio_logger.py @@ -0,0 +1,19 @@ +""" +Tests to ensure asyncio logger uses SaltLoggingClass +""" + +import logging + + +def test_asyncio_logger_saltloggingclass(): + """ + Test that the asyncio logger is an instance of SaltLoggingClass + + It is imported before salt._logging so we need to ensure it is overridden + """ + + asyncio_logger = logging.getLogger("asyncio") + + import salt._logging.impl + + assert isinstance(asyncio_logger, salt._logging.impl.SaltLoggingClass)