Python context processing error, logging

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.