Use of the logging log module

Source: Internet
Author: User

Five levels of logging mode
Debug      details, which are typically interesting when debugging a problem. INFO             proves that things work as expected. WARNING          indicates that there have been some surprises, or that problems will occur in the near future (such as ' Disk full '). The software is still working properly. ERROR            because of a more serious problem, the software is unable to perform some functions. CRITICAL         A critical error that indicates that the software is no longer able to run.
Two ways to configure
The logging.basicconfig () function can change the default behavior of the logging module through specific parameters, with the available parameters: FileName: Creates a filedhandler with the specified file name so that the log is stored in the specified files. FileMode: File is opened by using this parameter when filename is specified, and the default value is "a" and can be specified as "W". Format: Specifies the log display format used by handler. DATEFMT: Specifies the date time format. Level: Set Rootlogger (The following explains the specific concept) stream: Create Streamhandler with the specified stream. You can specify output to Sys.stderr,sys.stdout or file (f=open (' Test.log ', ' W ')), default is Sys.stderr. If you list both the filename and stream two parameters, the stream parameter is ignored. formatting strings that may be used in the format parameter:%(name) s Logger's name%(Levelno) s log level in digital form%(levelname) s log level in text form%(pathname) s The full pathname of the module that invokes the log output function, possibly without%(filename) s The file name of the module that invokes the log output function%module Name of the log output function called by (module) s%(funcName) s function name of the call log output function%(Lineno) d The line of code where the statement that invokes the log output function%(created) F current time, represented by the UNIX standard floating-point number representing the time%(relativecreated) d when the log information is output, the number of milliseconds since logger was created% (asctime) s The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds%(thread) d thread ID. Probably not .%(threadname) s thread name. Probably not .%(process) d ID. Probably not .% (message) s user-output message
Configuration ParametersSimple configuration
ImportLogging Logging.basicconfig ( level=logging. DEBUG,#level of outputformat='% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s',#format of the outputdatefmt='%a,%d%b%Y%h:%m:%s',#the time format of the output (the output format above contains the output time)Filename='/tmp/test.log',#path to file writtenFilemode='W')#The format of the writeLogging.debug ('Debug Message') Logging.info ('Info Message') logging.warning ('warning Message') Logging.error ('error Message') logging.critical ('Critical Message')
Detailed configuration
ImportLogginglogger=Logging.getlogger ()#create a handler and set the output levelFH = logging. Filehandler ('Test.log', encoding='Utf-8') SH=logging. Streamhandler () Fh.setlevel (logging. WARNING) Sh.setlevel (logging. WARNING)#Create a formatFormatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s')#bind format to handler and tie handler to logger instanceFh.setformatter (Formatter) sh.setformatter (formatter) logger.addhandler (FH) logger.addhandler (SH) logger.debug ('Logger Debug Message') Logger.info ('Logger Info Message') logger.warning ('Logger warning message') Logger.error ('Logger error message') logger.critical ('Logger Critical Message')

Use of the logging log module

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.