From dbc5a58e166edccd81594774ca6ea7066186c62a Mon Sep 17 00:00:00 2001 From: b8raoult <53792887+b8raoult@users.noreply.github.com> Date: Fri, 13 Dec 2024 14:18:13 +0000 Subject: [PATCH] Support from different logging message per threads/processes (#65) * Support from different logging message per threads/processes * Support from different logging message per threads/processes * add merge_configs() --- src/anemoi/utils/config.py | 8 ++++++++ src/anemoi/utils/logs.py | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/anemoi/utils/logs.py diff --git a/src/anemoi/utils/config.py b/src/anemoi/utils/config.py index 64da8d0..9c89b4b 100644 --- a/src/anemoi/utils/config.py +++ b/src/anemoi/utils/config.py @@ -376,3 +376,11 @@ def find(metadata, what, result=None, *, select: callable = None): find(v, what, result) return result + + +def merge_configs(*configs): + result = {} + for config in configs: + _merge_dicts(result, config) + + return result diff --git a/src/anemoi/utils/logs.py b/src/anemoi/utils/logs.py new file mode 100644 index 0000000..47d4798 --- /dev/null +++ b/src/anemoi/utils/logs.py @@ -0,0 +1,40 @@ +# (C) Copyright 2024 Anemoi contributors. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# +# In applying this licence, ECMWF does not waive the privileges and immunities +# granted to it by virtue of its status as an intergovernmental organisation +# nor does it submit to any jurisdiction. + + +"""Logging utilities.""" + +import logging +import threading + +thread_local = threading.local() + + +LOGGER = logging.getLogger(__name__) + + +def set_logging_name(name): + thread_local.logging_name = name + + +class ThreadCustomFormatter(logging.Formatter): + def format(self, record): + record.logging_name = thread_local.logging_name + return super().format(record) + + +def enable_logging_name(name="main"): + thread_local.logging_name = name + + formatter = ThreadCustomFormatter("%(asctime)s - %(logging_name)s - %(levelname)s - %(message)s") + + logger = logging.getLogger() + + for handler in logger.handlers: + handler.setFormatter(formatter)