From c06b3e166e111c7c18bb2b081ee5a11a83bf2bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rasmus=20Hag=20L=C3=B8vstad?= Date: Wed, 26 Jun 2024 13:09:42 +0200 Subject: [PATCH] Fix logging redudancy on multiple instances When making multiple trackers (or re-instantiating as common in Notebook-like environments), each instantiation of CarbonTracker causes logging to duplicate messages. This is due to logger.getLogging always returning the same logging instance when called with the same argument (singleton-like). This is fixed by having each instance of CarbonTracker have a unique logging_id, which makes each instance have their own logger. --- carbontracker/loggerutil.py | 11 ++++++----- carbontracker/tracker.py | 6 +++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/carbontracker/loggerutil.py b/carbontracker/loggerutil.py index 4b20624..6b6b075 100644 --- a/carbontracker/loggerutil.py +++ b/carbontracker/loggerutil.py @@ -7,6 +7,7 @@ import importlib_metadata as metadata from carbontracker import constants from typing import Union +from random import randint def convert_to_timestring(seconds: int, add_milliseconds=False) -> str: @@ -58,23 +59,23 @@ def filter(self, record): class Logger: - def __init__(self, log_dir=None, verbose=0, log_prefix=""): + def __init__(self, log_dir=None, verbose=0, log_prefix="", logger_id=0): self.verbose = verbose self.logger, self.logger_output, self.logger_err = self._setup( - log_dir=log_dir, log_prefix=log_prefix + log_dir=log_dir, log_prefix=log_prefix, logger_id=logger_id ) self._log_initial_info() self.msg_prepend = "CarbonTracker: " - def _setup(self, log_dir=None, log_prefix=""): + def _setup(self, log_dir=None, log_prefix="", logger_id=0): if log_prefix: log_prefix += "_" logger_name = f"{log_prefix}{os.getpid()}" logger = logging.getLogger(logger_name) - logger_err = logging.getLogger("carbontracker.err") - logger_output = logging.getLogger("carbontracker.output") + logger_err = logging.getLogger(f"carbontracker.{logger_id}.err") + logger_output = logging.getLogger(f"carbontracker.{logger_id}.output") logger.propagate = False logger.setLevel(logging.DEBUG) logger_output.propagate = False diff --git a/carbontracker/tracker.py b/carbontracker/tracker.py index ac6bc0c..ad6887e 100644 --- a/carbontracker/tracker.py +++ b/carbontracker/tracker.py @@ -8,6 +8,7 @@ from typing import List, Union import numpy as np +from random import randint from carbontracker import constants from carbontracker import loggerutil @@ -331,7 +332,10 @@ def __init__( try: pids = self._get_pids() self.logger = loggerutil.Logger( - log_dir=log_dir, verbose=verbose, log_prefix=log_file_prefix + log_dir=log_dir, + verbose=verbose, + log_prefix=log_file_prefix, + logger_id=str(randint(1, 999999)), ) self.tracker = CarbonTrackerThread( delete=self._delete,