The Exception Log for the adorner version was previously sent, but it needs to be decorated on a function or method. This article uses context management to record errors with one with and does not need to be written as a function.
ImportTraceback
# pip Install Multiprocessing_log_manager fromMultiprocessing_log_managerImportLogmanagerclassExceptioncontextmanager ():"""timed with the context manager to make error-trapping of code fragments more delicate than adorners""" def __init__(Self, logger_name='Exceptioncontextmanager', verbose=100, donot_raise__exception=True,):""":p Aram _verbose: The depth of the print error, the limit for the Traceback object, the positive integer:p Aram donot_raise__exception: If the error is not re-thrown, the Fasle is thrown, and the TR UE is not thrown"""Self.logger=Logmanager (logger_name). Get_logger_and_add_handlers () Self._verbose=verbose self._donot_raise__exception=donot_raise__exceptiondef __enter__(self):return Selfdef __exit__(self, exc_type, Exc_val, EXC_TB):#print (exc_val) #print (Traceback.format_exc ())Exc_str = str (exc_type) +' : '+Str (exc_val) Exc_str_color='\033[0;30;45m%s\033[0m'%Exc_strifSelf._donot_raise__exception:self.logger.error ('\ n'. Join (TRACEBACK.FORMAT_TB (EXC_TB) [: self._verbose]) +Exc_str_color)returnSelf._donot_raise__exception#The __exit__ method must be Retuen true to not re-throw the errorif __name__=='__main__': defF1 ():1 +'2' defF2 (): F1 ()deff3 (): F2 ()deff4 (): F3 ()defrun (): F4 ()
The result of the operation is this:
Python context processing error, logging