Skip to content

Commit

Permalink
Re-use PanLogger instances if the parameters haven't changed. (#192)
Browse files Browse the repository at this point in the history
* Re-use PanLogger instances if the parameters haven't changed.
* Remove global _logger and references to it, replace with calls to
get_root_logger.
  • Loading branch information
jamessynge authored and wtgee committed Dec 15, 2017
1 parent 1537b05 commit 2e029dc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
10 changes: 3 additions & 7 deletions pocs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def _check_config(temp_config):

# Global vars
_config = None
_logger = None


class PanBase(object):
Expand All @@ -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__

Expand Down
4 changes: 2 additions & 2 deletions pocs/utils/google/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from gcloud import storage

from pocs import _logger
import pocs.utils.logger


class PanStorage(object):
Expand All @@ -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

Expand Down
18 changes: 16 additions & 2 deletions pocs/utils/logger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import sys
import time
Expand Down Expand Up @@ -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:
Expand All @@ -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')
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 2e029dc

Please sign in to comment.