Python Exception information Write log

Source: Internet
Author: User

In the process of development, you may often encounter a requirement, you need to export the Python exception information to the log file
Online methods are not very practical, the following is a practical, from the Python 2.7 source code to buckle out of
Nonsense not to say directly on the code, the code is not many, more than the comments

1 Importsys, Traceback2 3Traceback_template =" "Traceback (most recent):4 File "% (filename) S", Line% (Lineno) s, in% (name) s5 % (type) s:% (message) s\n" " #skipping the "actual line" item6 7 #Also note:we don ' t walk all the the-through the frame stack in this example8 #See hg.python.org/cpython/file/8dffb76faacc/lib/traceback.py#l2809 #(Imagine if the 1/0, below, were replaced by a call-to-test () which did 1/0.)Ten  One Try: A1/0 - except: -     #Http://docs.python.org/2/library/sys.html#sys.exc_info theExc_type, exc_value, exc_traceback = Sys.exc_info ()#Most recent (if any) by default -  -     " " - Reason This _can_ is bad:if an (unhandled) exception happens after this, + or if we do not delete the labels on (not much) older versions of Py, the - reference we created can linger. +  A Traceback.format_exc/print_exc do the very thing, but note this creates a at Temp scope within the function. -     " " -  -Traceback_details = { -                          'filename': Exc_traceback.tb_frame.f_code.co_filename, -                          'Lineno': Exc_traceback.tb_lineno, in                          'name': Exc_traceback.tb_frame.f_code.co_name, -                          'type': Exc_type.__name__, to                          'message': Exc_value.message,#or see Traceback._some_str () +                         } -  the     del(Exc_type, Exc_value, Exc_traceback)#So we don ' t leave our local labels/objects dangling *     #This still isn ' t "completely safe", though! $     #"Best (Recommended) Practice:replace all Exc_type, Exc_value, Exc_tracebackPanax Notoginseng     #With sys.exc_info () [0], Sys.exc_info () [1], Sys.exc_info () [2] -  the  +     ## Modify this to place the traceback anywhere, or store it in a file. A     PrintTraceback_template% Traceback_details

Welcome to join the operation and maintenance development technology to share QQ group: 365534424; Hundreds of industry experts and peers are waiting for you to communicate

Python Exception information Write log

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.