The logging of the key modules of Python learning---

Source: Internet
Author: User

Log level

Log level critical > Error > Warning > Info > Debug, the default is to start printing from warning

Import logging# Log level  critical > Error > Warning > Info > debuglogging.debug (' Hello World ') logging.info (' H Ello World ') logging.warning ("Hello World") logging.error (' Hello World ') logging.critical (' Hello World ')
configuration of the log

The configuration of the Basicconfig file can only be output to a file, but with the stream parameter to achieve the screen/file output effect

Default Append Mode

The default is output to the screen, the filename is output to the file

Configure directly with logging
Import logging# configuration Basicconfig file can only be output to a file, but with the stream parameter can achieve screen/file output effect Logging.basicconfig (level=logging. DEBUG,  ormat= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ',                    datefmt= '%a,%d%b%Y%H: %m:%s ',                    #stream output stream                    filename= ' Test.log ', # is output to the screen by default, and filename is output to file                    filemode= ' a ')   # The default append mode logging.debug (' Debug Message ') Logging.info (' info message ') logging.warning (' warning message ') logging.error (' Error message ') logging.critical (' Critical message ')

formatting strings that may be used in the format parameter:

% (name) s logger name

% (Levelno) s log level in digital form

% (levelname) s log level in text form

% (pathname) s calls the full pathname of the module of the log output function and may not have

% (filename) s The file name of the module that called the log output function

% (module) s call the module name of the log output function

% (FuncName) s Call the function name of the log output function

% (Lineno) d The line of code where the statement of the log output function is called

% (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 process ID. Probably not.

% (message) s user-output message

Logger object: Can print the file, also can output the screen

Import logging# creates a logger object and is named log = logging. Logger (' User_logger ', level=logging.info) # Change the log level by default to Warning Level # Log.setlevel (logging. DEBUG) # File Output Object log_txt = logging. Filehandler (' Log.log ') # Screen Output Object log_str = logging. Streamhandler () Log_format = logging. Formatter ('% (asctime) s-% (name) s-% (LevelName) s-[line:% (Lineno) d]-% (message) s ') # screen output log_str.setformatter (log_ Format) # file output Log_txt.setformatter (Log_format) # Log Object Add screen output Log.addhandler (LOG_STR) # Log Object Add File Output Log.addhandler (log_ TXT) # output content, the default warning above log can be printed log.debug (' Log debug Message ') Log.info (' Log info message ') log.warning (' Log warning Message ') Log.error (' Log error message ') log.critical (' Log critical message ')

The logging of the key modules of Python learning---

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.