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

Do our best to ensure we don't add the same handler more than once. #101

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 9 additions & 2 deletions gcm/gcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class GCM(object):
BACKOFF_INITIAL_DELAY = 1000
MAX_BACKOFF_DELAY = 1024000
logger = None
logging_handler = None

def __init__(self, api_key, proxy=None, timeout=None, debug=False):
""" api_key : google api key
Expand Down Expand Up @@ -210,17 +211,23 @@ def enable_logging(level=logging.DEBUG, handler=None):
:return: the handler after adding it
"""
if not handler:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('[%(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(funcName)s()] %(message)s'))
# Use a singleton logging_handler instead of recreating it,
# so we can remove-and-re-add safely without having duplicate handlers
if GCM.logging_handler is None:
GCM.logging_handler = logging.StreamHandler()
GCM.logging_handler.setFormatter(logging.Formatter('[%(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(funcName)s()] %(message)s'))
handler = GCM.logging_handler

GCM.logger = logging.getLogger(__name__)
GCM.logger.removeHandler(handler)
GCM.logger.addHandler(handler)
GCM.logger.setLevel(level)
GCM.log('Added a stderr logging handler to logger: {0}', __name__)

# Enable requests logging
requests_logger_name = 'requests.packages.urllib3'
requests_logger = logging.getLogger(requests_logger_name)
requests_logger.removeHandler(handler)
requests_logger.addHandler(handler)
requests_logger.setLevel(level)
GCM.log('Added a stderr logging handler to logger: {0}', requests_logger_name)
Expand Down