Skip to content

Commit 7bea644

Browse files
committed
add method to support getting request logger by using method json_logging.get_request_logger(), fix #23
1 parent f3b8cdd commit 7bea644

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.md

+3
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.0.2 - 2019-07-20
8+
- add method to support getting request logger by using method json_logging.get_request_logger(), fix #23
9+
710
## 1.0.1 - 2019-07-20
811
- prevent log forging, fix #1
912

json_logging/__init__.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
COMPONENT_INSTANCE_INDEX = 0
2626

2727
_framework_support_map = {}
28+
_request_logger = None
2829
_current_framework = None
2930
_logger = get_library_logger(__name__)
3031
_request_util = None
@@ -162,11 +163,23 @@ def init_request_instrument(app=None):
162163

163164
configurator = _current_framework['app_request_instrumentation_configurator']()
164165
configurator.config(app)
165-
handlers = configurator.request_logger.handlers
166+
request_logger = configurator.request_logger
167+
handlers = request_logger.handlers
166168
for handler in handlers:
167169
handler.setFormatter(JSONRequestLogFormatter())
168170

169171

172+
def get_request_logger():
173+
global _request_logger
174+
if _current_framework is None or _current_framework == '-':
175+
raise RuntimeError("please init the logging first, call init(framework_name) first")
176+
177+
if _request_logger is None:
178+
raise RuntimeError("please init request instrument first, call init_request_instrument(app) first")
179+
180+
return _request_logger
181+
182+
170183
class RequestInfo(dict):
171184
"""
172185
class that keep HTTP request information for request instrumentation logging

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name="json-logging",
15-
version='1.0.1',
15+
version='1.0.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)