@@ -23,8 +23,7 @@ def handle_exceptions(logger):
23
23
"""Handle exceptions using the provided logger."""
24
24
25
25
def exception_handler (scope , etype , value , traceback ):
26
- logger .exception ("Top-level exception occurred" ,
27
- scope = scope , exc_info = (etype , value , traceback ))
26
+ logger .exception ("Top-level exception occurred" , scope = scope , exc_info = (etype , value , traceback ))
28
27
29
28
def sys_exception_handler (etype , value , traceback ):
30
29
exception_handler ("sys" , etype , value , traceback )
@@ -33,21 +32,19 @@ def threading_exception_handler(args):
33
32
if args .exc_type == SystemExit and args .exc_value .code == 0 :
34
33
# `sys.exit(0)` is considered "successful termination":
35
34
# https://docs.python.org/3/library/sys.html#sys.exit
36
- logger .debug ("normal thread exit" , thread = args .thread ,
37
- stack = "" .join (
38
- format_exception (
39
- args .exc_type , args .exc_value , args .exc_traceback )))
35
+ logger .debug (
36
+ "normal thread exit" ,
37
+ thread = args .thread ,
38
+ stack = "" .join (format_exception (args .exc_type , args .exc_value , args .exc_traceback )),
39
+ )
40
40
else :
41
- exception_handler (f"thread: { args .thread } " ,
42
- args .exc_type , args .exc_value , args .exc_traceback )
41
+ exception_handler (f"thread: { args .thread } " , args .exc_type , args .exc_value , args .exc_traceback )
43
42
44
43
sys .excepthook = sys_exception_handler
45
44
threading .excepthook = threading_exception_handler
46
45
47
46
48
- def get_structured_logger (name = __name__ ,
49
- filename = None ,
50
- log_exceptions = True ):
47
+ def get_structured_logger (name = __name__ , filename = None , log_exceptions = True ):
51
48
"""Create a new structlog logger.
52
49
53
50
Use the logger returned from this in indicator code using the standard
@@ -73,10 +70,7 @@ def get_structured_logger(name=__name__,
73
70
else :
74
71
log_level = logging .INFO
75
72
76
- logging .basicConfig (
77
- format = "%(message)s" ,
78
- level = log_level ,
79
- handlers = [logging .StreamHandler ()])
73
+ logging .basicConfig (format = "%(message)s" , level = log_level , handlers = [logging .StreamHandler ()])
80
74
81
75
def add_pid (_logger , _method_name , event_dict ):
82
76
"""Add current PID to the event dict."""
@@ -131,7 +125,7 @@ def add_pid(_logger, _method_name, event_dict):
131
125
return logger
132
126
133
127
134
- class LoggerThread () :
128
+ class LoggerThread :
135
129
"""
136
130
A construct to use a logger from multiprocessing workers/jobs.
137
131
@@ -149,15 +143,15 @@ class LoggerThread():
149
143
docs.python.org/3/howto/logging-cookbook.html#logging-to-a-single-file-from-multiple-processes
150
144
"""
151
145
152
- class SubLogger () :
146
+ class SubLogger :
153
147
"""MP-safe logger-like interface to convey log messages to a listening LoggerThread."""
154
148
155
149
def __init__ (self , queue ):
156
150
"""Create SubLogger with a bound queue."""
157
151
self .queue = queue
158
152
159
153
def _log (self , level , * args , ** kwargs ):
160
- kwargs_plus = {' sub_pid' : multiprocessing .current_process ().pid }
154
+ kwargs_plus = {" sub_pid" : multiprocessing .current_process ().pid }
161
155
kwargs_plus .update (kwargs )
162
156
self .queue .put ([level , args , kwargs_plus ])
163
157
@@ -181,7 +175,6 @@ def critical(self, *args, **kwargs):
181
175
"""Log a CRITICAL level message."""
182
176
self ._log (logging .CRITICAL , * args , ** kwargs )
183
177
184
-
185
178
def get_sublogger (self ):
186
179
"""Retrieve SubLogger for this LoggerThread."""
187
180
return self .sublogger
@@ -195,25 +188,22 @@ def __init__(self, logger, q=None):
195
188
self .msg_queue = multiprocessing .Queue ()
196
189
197
190
def logger_thread_worker ():
198
- logger .info (' thread started' )
191
+ logger .info (" thread started" )
199
192
while True :
200
193
msg = self .msg_queue .get ()
201
- if msg == ' STOP' :
202
- logger .debug (' received stop signal' )
194
+ if msg == " STOP" :
195
+ logger .debug (" received stop signal" )
203
196
break
204
197
level , args , kwargs = msg
205
- if level in [logging .DEBUG , logging .INFO , logging .WARNING ,
206
- logging .ERROR , logging .CRITICAL ]:
198
+ if level in [logging .DEBUG , logging .INFO , logging .WARNING , logging .ERROR , logging .CRITICAL ]:
207
199
logger .log (level , * args , ** kwargs )
208
200
else :
209
- logger .error ('received unknown logging level! exiting...' ,
210
- level = level , args_kwargs = (args , kwargs ))
201
+ logger .error ("received unknown logging level! exiting..." , level = level , args_kwargs = (args , kwargs ))
211
202
break
212
- logger .debug (' stopping thread' )
203
+ logger .debug (" stopping thread" )
213
204
214
- self .thread = threading .Thread (target = logger_thread_worker ,
215
- name = "LoggerThread__" + logger .name )
216
- logger .debug ('starting thread' )
205
+ self .thread = threading .Thread (target = logger_thread_worker , name = "LoggerThread__" + logger .name )
206
+ logger .debug ("starting thread" )
217
207
self .thread .start ()
218
208
219
209
self .sublogger = LoggerThread .SubLogger (self .msg_queue )
@@ -222,13 +212,13 @@ def logger_thread_worker():
222
212
def stop (self ):
223
213
"""Terminate this LoggerThread."""
224
214
if not self .running :
225
- self .logger .warning (' thread already stopped' )
215
+ self .logger .warning (" thread already stopped" )
226
216
return
227
- self .logger .debug (' sending stop signal' )
228
- self .msg_queue .put (' STOP' )
217
+ self .logger .debug (" sending stop signal" )
218
+ self .msg_queue .put (" STOP" )
229
219
self .thread .join ()
230
220
self .running = False
231
- self .logger .info (' thread stopped' )
221
+ self .logger .info (" thread stopped" )
232
222
233
223
234
224
@contextlib .contextmanager
0 commit comments