The logging of Python module learning

Source: Internet
Author: User

FileName: Creates a filedhandler with the specified file name (the concept of handler is explained in the back) so that the log is stored in the specified file. 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 the output to Sys.stderr,sys.stdout or to a file, and the default is Sys.stderr. If you list both the filename and stream two parameters, the stream parameter is ignored. Formatted string that may be used in the format parameter:% (name) s logger name% (Levelno) s number form log level% (levelname) s text form log level% (pathname) s The full pathname of the module that calls the log output function, The file name of the module that may not have% (filename) s call log output function% (module) s call log output function module name% (funcName) s call log output function name% (Lineno) d Call Log output function The statement that is located in the line% ( Created) F current time, the current time in the number of milliseconds (asctime) s string as a percentage of the time that the UNIX standard represents a floating-point (relativecreated) d output log information, since logger was created. The default format is "2003-07-08 16:49:45,896". The comma is followed by the millisecond% (thread) d thread ID. There may not be a% (threadname) s thread name. There may not be a% (process) d process ID. Messages that may not have a% (message) s user output

Example:

Import Logging  logging.basicconfig (level=logging. DEBUG,          format= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ',          datefmt= '%Y,%b%d%a%H :%m:%s ',          filename= ' Test.log ',          filemode= ' W ')  logging.debug (' 1 ')  logging.info (' 2 '  ) Logging.warning (' 3 ')  logging.error (' 4 ')  logging.critical (' 5 ')  results: Jul Wed 11:45:50 E01.py[line : 8] DEBUG 12017, Jul Wed 11:45:50 e01.py[line:9] INFO 22017, Jul Wed 11:45:50 e01.py[line:10] WARNING 32017, Jul 19 Wed 11:45:50 E01.py[line:11] ERROR 42017, Jul Wed 11:45:50 E01.py[line:12] CRITICAL 5

Common usage:

Logging configuration file

Logging.conf

    [Loggers]      Keys=root,simpleexample [            handlers]      keys=consolehandler            [formatters]      keys=simpleformatter            [ Logger_root]      level=debug      handlers=consolehandler            [logger_simpleexample]      level=debug      Handlers=consolehandler      qualname=simpleexample      propagate=0            [Handler_consolehandler]      class= Streamhandler      level=debug      formatter=simpleformatter      args= (sys.stdout,)            [Formatter_ Simpleformatter]      format=% (asctime) s-% (name) s-% (levelname) s-% (message) s      datefmt=  

Program Examples:

    Import Logging        import logging.config                logging.config.fileConfig ("logging.conf")    # with config file                 # Create Logger         logger = Logging.getlogger ("Simpleexample")                # "Application" code         logger.debug ("Debug Message")        logger.info ("info message")        Logger.warn ("Warn message")        logger.error ("error message        ") Logger.critical ("critical Message")  

The logging of Python module learning

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.