29
29
_current_framework = None
30
30
_logger = get_library_logger (__name__ )
31
31
_request_util = None
32
+ _default_formatter = None
32
33
33
34
34
35
def get_correlation_id (request = None ):
@@ -79,13 +80,16 @@ def config_root_logger():
79
80
& remove duplicate handlers for request instrumentation logging.
80
81
Please made sure that you call this after you called "logging.basicConfig() or logging.getLogger('root')
81
82
"""
83
+ global _default_formatter
84
+
82
85
if ENABLE_JSON_LOGGING :
83
86
ENABLE_JSON_LOGGING_DEBUG and _logger .debug ("Update root logger to using JSONLogFormatter" )
87
+
84
88
if len (logging .root .handlers ) > 0 :
85
89
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 )
87
91
else :
88
- util .update_formatter_for_loggers ([logging .root ], JSONLogWebFormatter )
92
+ util .update_formatter_for_loggers ([logging .root ], _default_formatter )
89
93
# remove all handlers for request logging
90
94
request_logger = _current_framework ['app_request_instrumentation_configurator' ]().get_request_logger ()
91
95
if request_logger :
@@ -114,6 +118,7 @@ def __init(framework_name=None, custom_formatter=None, enable_json=False):
114
118
115
119
global _current_framework
116
120
global ENABLE_JSON_LOGGING
121
+ global _default_formatter
117
122
if _current_framework is not None :
118
123
raise RuntimeError ("Can not call init more than once" )
119
124
@@ -136,22 +141,22 @@ def __init(framework_name=None, custom_formatter=None, enable_json=False):
136
141
if ENABLE_JSON_LOGGING and _current_framework ['app_configurator' ] is not None :
137
142
_current_framework ['app_configurator' ]().config ()
138
143
139
- formatter = custom_formatter if custom_formatter else JSONLogWebFormatter
144
+ _default_formatter = custom_formatter if custom_formatter else JSONLogWebFormatter
140
145
else :
141
- formatter = custom_formatter if custom_formatter else JSONLogFormatter
146
+ _default_formatter = custom_formatter if custom_formatter else JSONLogFormatter
142
147
143
148
if not enable_json and not ENABLE_JSON_LOGGING :
144
149
_logger .warning (
145
150
"JSON format is not enable, normal log will be in plain text but request logging still in JSON format! "
146
151
"To enable set ENABLE_JSON_LOGGING env var to either one of following values: ['true', '1', 'y', 'yes']" )
147
152
else :
148
153
ENABLE_JSON_LOGGING = True
149
- logging ._defaultFormatter = formatter ()
154
+ logging ._defaultFormatter = _default_formatter ()
150
155
151
156
# go to all the initialized logger and update it to use JSON formatter
152
157
ENABLE_JSON_LOGGING_DEBUG and _logger .debug ("Update all existing logger to using JSONLogFormatter" )
153
158
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 )
155
160
156
161
157
162
def init_request_instrument (app = None , custom_formatter = None ):
0 commit comments