Python------Logging Module

Source: Internet
Author: User

Tag: ASI Digital SED Family Current time Open open mode change Ted

Logging

Functional Simple Configuration

Import Logging
Logging.debug ('debug')
Logging.info ('info')
Logging.warning (' wrong ')
logging.error ('error message'

Logging.critical ('critical message'

Default logging level is set to Warning (critical>error>warning>info>debug)

Flexible configuration log level, log format, output location:

ImportLogginglogging.debug ('Debug') Logging.info ('Info') logging.warning ('something went wrong.') Logging.basicconfig ( level=logging. DEBUG) Logging.debug ('Debug') Logging.info ('Info') logging.warning ('something went wrong.') Logging.error ('Error')Print('% (name) s')

Configuration parameters

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
parameter Configuration

Logging Object Configuration

ImportLoggingdefMy_logger (Filename,file=true,stream =True): Logger=Logging.getlogger () Formatter= Logging. Formatter (fmt='% (name) s% (asctime) s [% (Lineno) d]--% (message) s', Datefmt='%d/%m/%y%h:%m:%s') Logging.setlevel (logging. DEBUG)ifFile:file_handler= Logging. Filehandler (filename,encoding='Utf-8') File_handler.setformatter (formatter)#file Stream file operatorsLogger.addhandler (File_handler)ifStream:stream_handler=logging. Streamhandler () Stream_handler.setformatter (formatter)#screen flow screen operatorLogger.addhandler (Stream_handler)returnLoggerlogger= My_logger ('Logging', file=False) logger.warning ("something went wrong.") logging. DEBUG ('Debug')

Python------Logging 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.