Python Basic-logging module

Source: Internet
Author: User

Level of Log
ImportLogginglogging.debug ('Debug Message') Logging.info ('Info Message') logging.warning ('warning Message') Logging.error ('error Message') logging.critical ('Critical Message') Execution Result: WARNING:root:warning messageERROR:root:error messageCRITICAL:root:critical Message
    By default, Python's logging module prints the logs to standard output and only displays logs that are greater than or equal to the warning level, indicating that the default logging level is set to
WARNING (log level level Critical>error>warning>info>debug>notset),
The default log format is:
Log level: Logger name: User output information

Log level configuration, log format, output location
# custom Log Levels
Import logging
Logging.basicconfig (level=logging. DEBUG, Format='% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s', Datefmt='%a,%d%b%Y%h:%m:%s', filename='Test.log', FileMode='a') Logging.debug ('Debug Message') Logging.info ('Info Message') logging.warning ('warning Message') Logging.error ('error Message') logging.critical ('Critical Message'The execution results will generate a Test.log file in the working directory, as follows: Tue,OCT 14:08:22 test.py[line:13] Debug Debug Messagetue,OCT 14:08:22 test.py[line:14] Info Info messagetue,OCT 14:08:22 test.py[line:15] WARNING WARNING messagetue,OCT 14:08:22 test.py[line:16] Error Error messagetue,OCT 14:08:22 test.py[line:17] CRITICAL CRITICAL message
The parameters and descriptions in the Logging.basicconfig () function are as follows:
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 the log levels for Rootlogger (which will explain the concepts behind)
Stream: Creates a streamhandler with the specified stream. You can specify the output to Sys.stderr,sys.stdout or the file (F=open (' Test.log ', ' W ')), and the 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 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

Output logs to screen and file at the same time
Logger =Logging.getlogger () FH= Logging. Filehandler ('Test.log') Ch=logging. Streamhandler () Formatter= Logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s') Fh.setformatter (formatter) ch.setformatter (formatter) logger.setlevel (logging. DEBUG) Logger.addhandler (FH) logger.addhandler (CH) logging.debug ('Debug Message') Logging.info ('Info Message') logging.warning ('warning Message') Logging.error ('error Message') logging.critical ('Critical Message')

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