diff --git a/pocs/__init__.py b/pocs/__init__.py index 4ee599a99..c741222c7 100644 --- a/pocs/__init__.py +++ b/pocs/__init__.py @@ -72,7 +72,6 @@ def _check_config(temp_config): # Global vars _config = None -_logger = None class PanBase(object): @@ -96,12 +95,9 @@ def __init__(self, *args, **kwargs): _check_config(_config) self.config = _config - global _logger - if _logger is None: - _logger = get_root_logger() - _logger.info('{:*^80}'.format(' Starting POCS ')) - - self.logger = kwargs.get('logger', _logger) + self.logger = kwargs.get('logger') + if not self.logger: + self.logger = get_root_logger() self.__version__ = __version__ diff --git a/pocs/utils/google/storage.py b/pocs/utils/google/storage.py index f64c6ffb6..bbffe288a 100644 --- a/pocs/utils/google/storage.py +++ b/pocs/utils/google/storage.py @@ -3,7 +3,7 @@ from gcloud import storage -from pocs import _logger +import pocs.utils.logger class PanStorage(object): @@ -14,7 +14,7 @@ def __init__(self, project_id='panoptes-survey', bucket_name=None, prefix=None): "A valid bucket name is required.") super(PanStorage, self).__init__() - self.logger = _logger + self.logger = pocs.utils.logger.get_root_logger() self.project_id = project_id self.prefix = prefix diff --git a/pocs/utils/logger.py b/pocs/utils/logger.py index 7a4a77b69..2e6f2c655 100644 --- a/pocs/utils/logger.py +++ b/pocs/utils/logger.py @@ -1,3 +1,4 @@ +import json import os import sys import time @@ -42,6 +43,12 @@ def error(self, fmt, *args, **kwargs): self.logger.error(self._process_str(fmt, *args, **kwargs)) +# We don't want to create multiple root loggers that are "identical", +# so track the loggers in a dict keyed by a tuple of: +# (profile, json_serialized_logger_config). +all_loggers = {} + + def get_root_logger(profile='panoptes', log_config=None): """ Creates a root logger for PANOPTES used by the PanBase object Returns: @@ -51,11 +58,16 @@ def get_root_logger(profile='panoptes', log_config=None): # Get log info from config log_config = log_config if log_config else load_config('log').get('logger', {}) + # If we already created a logger for this profile and log_config, return that. + logger_key = (profile, json.dumps(log_config, sort_keys=True)) + logger_for_config = all_loggers.get(logger_key, None) + if logger_for_config: + return logger_for_config + # Alter the log_config to use UTC times if log_config.get('use_utc', True): for name, formatter in log_config['formatters'].items(): log_config['formatters'][name].setdefault('()', _UTCFormatter) - log_fname_datetime = datetime.datetime.utcnow().strftime('%Y%m%dT%H%M%SZ') else: log_fname_datetime = datetime.datetime.now().strftime('%Y%m%dT%H%M%SZ') @@ -100,7 +112,9 @@ def get_root_logger(profile='panoptes', log_config=None): except Exception: # pragma: no cover pass - return PanLogger(logger) + logger = PanLogger(logger) + all_loggers[logger_key] = logger + return logger class _UTCFormatter(logging.Formatter):