Skip to content

Commit 4bb3d59

Browse files
author
Bob Bui
committed
unexpected log entry when logging configured with dictConfig fix #51
1 parent 9693b66 commit 4bb3d59

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ 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.4 - 2020-08-03
8+
- fix #51
9+
710
## 1.2.2 - 2020-07-14
811
- fix #50
912

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ If you're using Cloud Foundry, it might worth to check out the library [SAP/cf-p
3030
Install by running this command:
3131
> pip install json-logging
3232
33-
By default log will be emitted in normal format to ease the local development. To enable it on production set either **json_logging.ENABLE_JSON_LOGGING** or **ENABLE_JSON_LOGGING environment variable** to true.
33+
By default log will be emitted in normal format to ease the local development. To enable it on production set enable_json in init_\<framework name\>(enable_json=True) method call (set **json_logging.ENABLE_JSON_LOGGING** or **ENABLE_JSON_LOGGING environment variable** to true is not recommended and will be deprecated in future versions).
3434

3535
To configure, call **json_logging.init_< framework_name >()**. Once configured library will try to configure all loggers (existing and newly created) to emit log in JSON format.
3636
See following use cases for more detail.
@@ -41,8 +41,7 @@ This mode don't support **correlation-id**.
4141
import json_logging, logging, sys
4242

4343
# log is initialized without a web framework name
44-
json_logging.ENABLE_JSON_LOGGING = True
45-
json_logging.init_non_web()
44+
json_logging.init_non_web(enable_json=True)
4645

4746
logger = logging.getLogger("test-logger")
4847
logger.setLevel(logging.DEBUG)
@@ -183,6 +182,7 @@ logger.info("test log statement", extra = {'props' : {'extra_property' : 'extra_
183182
If you want to use root logger as main logger to emit log. Made sure you call **config_root_logger()** after initialize root logger (by logging.basicConfig() or logging.getLogger('root')) [\[2\]](#2-python-logging-propagate)
184183
```python
185184
logging.basicConfig()
185+
json_logging.init_<framework name >()
186186
json_logging.config_root_logger()
187187
```
188188

@@ -196,7 +196,6 @@ logging library can be configured by setting the value in json_logging, all conf
196196
Name | Description | Default value
197197
--- | --- | ---
198198
ENABLE_JSON_LOGGING | **DEPRECATED** Whether to enable JSON logging mode.Can be set as an environment variable, enable when set to to either one in following list (case-insensitive) **['true', '1', 'y', 'yes']** , this have no effect on request logger | false
199-
ENABLE_JSON_LOGGING_DEBUG | Whether to enable debug logging for this library for development purpose. | true
200199
CORRELATION_ID_HEADERS | List of HTTP headers that will be used to look for correlation-id value. HTTP headers will be searched one by one according to list order| ['X-Correlation-ID','X-Request-ID']
201200
EMPTY_VALUE | Default value when a logging record property is None | '-'
202201
CORRELATION_ID_GENERATOR | function to generate unique correlation-id | uuid.uuid1

example/non_web.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import json_logging
55

66
# log is initialized without a web framework name
7-
json_logging.ENABLE_JSON_LOGGING = True
8-
json_logging.init_non_web()
7+
json_logging.init_non_web(enable_json=True)
98

109
logger = logging.getLogger("test logger")
1110
logger.setLevel(logging.DEBUG)

json_logging/__init__.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,15 @@ def config_root_logger():
8282
"""
8383
global _default_formatter
8484

85+
if len(logging.root.handlers) > 0:
86+
_logger.error(
87+
"No logging handlers found for root logger. Please made sure that you call this after you called "
88+
"logging.basicConfig() or logging.getLogger('root')")
89+
8590
if ENABLE_JSON_LOGGING:
8691
ENABLE_JSON_LOGGING_DEBUG and _logger.debug("Update root logger to using JSONLogFormatter")
8792

88-
if len(logging.root.handlers) > 0:
89-
if _current_framework is None or _current_framework == '-':
90-
util.update_formatter_for_loggers([logging.root], _default_formatter)
91-
else:
92-
util.update_formatter_for_loggers([logging.root], _default_formatter)
93-
# remove all handlers for request logging
94-
request_logger = _current_framework['app_request_instrumentation_configurator']().get_request_logger()
95-
if request_logger:
96-
for handler in request_logger.handlers:
97-
request_logger.removeHandler(handler)
98-
else:
99-
_logger.error(
100-
"No logging handlers found for root logger. Please made sure that you call this after you called "
101-
"logging.basicConfig() or logging.getLogger('root')")
93+
util.update_formatter_for_loggers([logging.root], _default_formatter)
10294

10395

10496
def init_non_web(*args, **kw):
@@ -119,6 +111,7 @@ def __init(framework_name=None, custom_formatter=None, enable_json=False):
119111
global _current_framework
120112
global ENABLE_JSON_LOGGING
121113
global _default_formatter
114+
ENABLE_JSON_LOGGING = enable_json
122115
if _current_framework is not None:
123116
raise RuntimeError("Can not call init more than once")
124117

@@ -184,6 +177,7 @@ def init_request_instrument(app=None, custom_formatter=None):
184177
request_logger.setLevel(logging.DEBUG)
185178
request_logger.addHandler(logging.StreamHandler(sys.stdout))
186179
util.update_formatter_for_loggers([request_logger], formatter)
180+
request_logger.parent = None
187181

188182

189183
def get_request_logger():

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.2',
15+
version='1.2.4',
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)