Skip to content

Commit

Permalink
Support from different logging message per threads/processes (#65)
Browse files Browse the repository at this point in the history
* Support from different logging message per threads/processes

* Support from different logging message per threads/processes

* add merge_configs()
  • Loading branch information
b8raoult authored Dec 13, 2024
1 parent 6ee6947 commit dbc5a58
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/anemoi/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 40 additions & 0 deletions src/anemoi/utils/logs.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit dbc5a58

Please sign in to comment.