2929_current_framework = None
3030_logger = get_library_logger (__name__ )
3131_request_util = None
32+ _default_formatter = None
3233
3334
3435def 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
157162def init_request_instrument (app = None , custom_formatter = None ):
0 commit comments