Skip to content

Commit 9693b66

Browse files
author
Bob Bui
committed
root logger to use custom formatter fix #50
1 parent 29fc74b commit 9693b66

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55
The format is based on [Keep a Changelog](http://keepachangelog.com/).
66

7-
## 1.2.0 - 2020-07-14
7+
## 1.2.2 - 2020-07-14
8+
- fix #50
9+
10+
## 1.2.1 - 2020-07-14
811
- fix #49
912

1013
## 1.2.0 - 2020-04-10

json_logging/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
_current_framework = None
3030
_logger = get_library_logger(__name__)
3131
_request_util = None
32+
_default_formatter = None
3233

3334

3435
def get_correlation_id(request=None):
@@ -79,13 +80,16 @@ def config_root_logger():
7980
& remove duplicate handlers for request instrumentation logging.
8081
Please made sure that you call this after you called "logging.basicConfig() or logging.getLogger('root')
8182
"""
83+
global _default_formatter
84+
8285
if ENABLE_JSON_LOGGING:
8386
ENABLE_JSON_LOGGING_DEBUG and _logger.debug("Update root logger to using JSONLogFormatter")
87+
8488
if len(logging.root.handlers) > 0:
8589
if _current_framework is None or _current_framework == '-':
86-
util.update_formatter_for_loggers([logging.root], JSONLogFormatter)
90+
util.update_formatter_for_loggers([logging.root], _default_formatter)
8791
else:
88-
util.update_formatter_for_loggers([logging.root], JSONLogWebFormatter)
92+
util.update_formatter_for_loggers([logging.root], _default_formatter)
8993
# remove all handlers for request logging
9094
request_logger = _current_framework['app_request_instrumentation_configurator']().get_request_logger()
9195
if request_logger:
@@ -114,6 +118,7 @@ def __init(framework_name=None, custom_formatter=None, enable_json=False):
114118

115119
global _current_framework
116120
global ENABLE_JSON_LOGGING
121+
global _default_formatter
117122
if _current_framework is not None:
118123
raise RuntimeError("Can not call init more than once")
119124

@@ -136,22 +141,22 @@ def __init(framework_name=None, custom_formatter=None, enable_json=False):
136141
if ENABLE_JSON_LOGGING and _current_framework['app_configurator'] is not None:
137142
_current_framework['app_configurator']().config()
138143

139-
formatter = custom_formatter if custom_formatter else JSONLogWebFormatter
144+
_default_formatter = custom_formatter if custom_formatter else JSONLogWebFormatter
140145
else:
141-
formatter = custom_formatter if custom_formatter else JSONLogFormatter
146+
_default_formatter = custom_formatter if custom_formatter else JSONLogFormatter
142147

143148
if not enable_json and not ENABLE_JSON_LOGGING:
144149
_logger.warning(
145150
"JSON format is not enable, normal log will be in plain text but request logging still in JSON format! "
146151
"To enable set ENABLE_JSON_LOGGING env var to either one of following values: ['true', '1', 'y', 'yes']")
147152
else:
148153
ENABLE_JSON_LOGGING = True
149-
logging._defaultFormatter = formatter()
154+
logging._defaultFormatter = _default_formatter()
150155

151156
# go to all the initialized logger and update it to use JSON formatter
152157
ENABLE_JSON_LOGGING_DEBUG and _logger.debug("Update all existing logger to using JSONLogFormatter")
153158
existing_loggers = list(map(logging.getLogger, logging.Logger.manager.loggerDict))
154-
util.update_formatter_for_loggers(existing_loggers, formatter)
159+
util.update_formatter_for_loggers(existing_loggers, _default_formatter)
155160

156161

157162
def init_request_instrument(app=None, custom_formatter=None):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name="json-logging",
15-
version='1.2.1',
15+
version='1.2.2',
1616
packages=find_packages(exclude=['contrib', 'docs', 'tests*', 'example', 'dist', 'build']),
1717
license='Apache License 2.0',
1818
description="JSON Python Logging",

0 commit comments

Comments
 (0)