Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-use PanLogger instances if the parameters haven't changed. #192

Merged
merged 2 commits into from
Dec 15, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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