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
Show file tree
Hide file tree
Changes from all commits
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
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