@@ -23,8 +23,7 @@ def handle_exceptions(logger):
2323 """Handle exceptions using the provided logger."""
2424
2525 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 ))
2827
2928 def sys_exception_handler (etype , value , traceback ):
3029 exception_handler ("sys" , etype , value , traceback )
@@ -33,21 +32,19 @@ def threading_exception_handler(args):
3332 if args .exc_type == SystemExit and args .exc_value .code == 0 :
3433 # `sys.exit(0)` is considered "successful termination":
3534 # 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+ )
4040 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 )
4342
4443 sys .excepthook = sys_exception_handler
4544 threading .excepthook = threading_exception_handler
4645
4746
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 ):
5148 """Create a new structlog logger.
5249
5350 Use the logger returned from this in indicator code using the standard
@@ -73,10 +70,7 @@ def get_structured_logger(name=__name__,
7370 else :
7471 log_level = logging .INFO
7572
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 ()])
8074
8175 def add_pid (_logger , _method_name , event_dict ):
8276 """Add current PID to the event dict."""
@@ -131,7 +125,7 @@ def add_pid(_logger, _method_name, event_dict):
131125 return logger
132126
133127
134- class LoggerThread () :
128+ class LoggerThread :
135129 """
136130 A construct to use a logger from multiprocessing workers/jobs.
137131
@@ -149,15 +143,15 @@ class LoggerThread():
149143 docs.python.org/3/howto/logging-cookbook.html#logging-to-a-single-file-from-multiple-processes
150144 """
151145
152- class SubLogger () :
146+ class SubLogger :
153147 """MP-safe logger-like interface to convey log messages to a listening LoggerThread."""
154148
155149 def __init__ (self , queue ):
156150 """Create SubLogger with a bound queue."""
157151 self .queue = queue
158152
159153 def _log (self , level , * args , ** kwargs ):
160- kwargs_plus = {' sub_pid' : multiprocessing .current_process ().pid }
154+ kwargs_plus = {" sub_pid" : multiprocessing .current_process ().pid }
161155 kwargs_plus .update (kwargs )
162156 self .queue .put ([level , args , kwargs_plus ])
163157
@@ -181,7 +175,6 @@ def critical(self, *args, **kwargs):
181175 """Log a CRITICAL level message."""
182176 self ._log (logging .CRITICAL , * args , ** kwargs )
183177
184-
185178 def get_sublogger (self ):
186179 """Retrieve SubLogger for this LoggerThread."""
187180 return self .sublogger
@@ -195,25 +188,22 @@ def __init__(self, logger, q=None):
195188 self .msg_queue = multiprocessing .Queue ()
196189
197190 def logger_thread_worker ():
198- logger .info (' thread started' )
191+ logger .info (" thread started" )
199192 while True :
200193 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" )
203196 break
204197 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 ]:
207199 logger .log (level , * args , ** kwargs )
208200 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 ))
211202 break
212- logger .debug (' stopping thread' )
203+ logger .debug (" stopping thread" )
213204
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" )
217207 self .thread .start ()
218208
219209 self .sublogger = LoggerThread .SubLogger (self .msg_queue )
@@ -222,13 +212,13 @@ def logger_thread_worker():
222212 def stop (self ):
223213 """Terminate this LoggerThread."""
224214 if not self .running :
225- self .logger .warning (' thread already stopped' )
215+ self .logger .warning (" thread already stopped" )
226216 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" )
229219 self .thread .join ()
230220 self .running = False
231- self .logger .info (' thread stopped' )
221+ self .logger .info (" thread stopped" )
232222
233223
234224@contextlib .contextmanager
0 commit comments