Python Logging Module

Source: Internet
Author: User

One (simple application)

Import Logging  logging.debug (' Debug message ')  logging.info (' info message ')  logging.warning (' Warning Message ')  logging.error (' error message ')  logging.critical (' critical message ')  

Output:

WARNING:root:warning message
ERROR:root:error message
CRITICAL:root:critical message

Visible, 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 message.

Two flexible configuration log level, log format, output location

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= '/tmp/test.log ',                      filemode= ' W ')    logging.debug (' Debug message ')  logging.info (' Info message ') logging.warning (' warning message ') logging.error ('  error message ')  logging.critical (' Critical message ')

To view the output:
Cat/tmp/test.log
Mon, 16:29:53 test_logging.py[line:9] Debug Debug message
Mon, 16:29:53 test_logging.py[line:10] Info Info message
Mon, 16:29:53 test_logging.py[line:11] WARNING WARNING message
Mon, 16:29:53 test_logging.py[line:12] Error error message
Mon, 16:29:53 test_logging.py[line:13] CRITICAL CRITICAL message

The Logging.basicconfig () function can be seen to change the default behavior of the logging module through specific parameters, with the parameters available
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

Three Logger objects

In the above examples we have learned about Logging.debug (), Logging.info (), logging.warning (), Logging.error (), logging.critical () (used to record different levels of log information), Logging.basicconfig () (using the default log format (Formatter) to establish a default stream processor (Streamhandler) for the log system, Set the base configuration (such as log level, etc.) and add to root logger (root logger) These several logging module-level functions, plus a module-level function is Logging.getlogger ([name]) (return a Logger object, If no name is specified will return root logger)

Let's look at one of the simplest processes:

Import Logginglogger = Logging.getlogger () # Creates a handler that is used to write to the log file FH = logging. Filehandler (' Test.log ') # again creates a handler for output to the console ch = logging. Streamhandler () formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ') Fh.setformatter (Formatter) ch.setformatter (Formatter ) Logger.addhandler (FH) #logger对象可以添加多个fh和ch对象logger. AddHandler (CH) 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 ')

For a brief introduction, the logging library provides multiple components: Logger, Handler, Filter, Formatter. The Logger object provides an interface that the application can use directly, handler sends logs to the appropriate destination, and filter provides a way to filter the log information formatter specify the log display format.

(1)

Logger is a tree-like hierarchy, and the output information is preceded by a Logger (which is automatically created and uses the root Logger if none of the displayed fetches, as shown in the first example).
Logger = Logging.getlogger () returns a default logger, root logger, and applies the default log level, handler, and formatter settings.
Of course, you can also specify the lowest log level through Logger.setlevel (LEL), with the available log levels logging. DEBUG, Logging.info, logging. WARNING, logging. ERROR, logging. CRITICAL.
Logger.debug (), Logger.info (), logger.warning (), Logger.error (), logger.critical () output different levels of logging, Only logs with log levels greater than or equal to the set log level will be output.

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 ')  

Only the output
2014-05-06 12:54:43,222-root-warning-logger WARNING Message
2014-05-06 12:54:43,223-root-error-logger ERROR Message
2014-05-06 12:54:43,224-root-critical-logger CRITICAL Message
From this output you can see that logger = Logging.getlogger () returns the logger named Root. There is no use of logger.setlevel (logging. Debug) to set the log level for logger, so use the default log level warniing, so the results only output information greater than or equal to warniing level.

(2) If we create a further two logger objects:

################################################# #logger1 = Logging.getlogger (' MyLogger ') logger1.setlevel ( Logging. DEBUG) Logger2 = Logging.getlogger (' MyLogger ') logger2.setlevel (logging.info) Logger1.addhandler (FH) Logger1.addhandler (CH) logger2.addhandler (FH) logger2.addhandler (CH) logger1.debug (' logger1 debug Message ') Logger1.info (' logger1 info message ') logger1.warning (' logger1 warning message ') logger1.error (' logger1 error message ') Logger1.critical (' logger1 critical message ')  logger2.debug (' logger2 debug Message ') Logger2.info (' Logger2 info Message ') logger2.warning (' logger2 warning message ') logger2.error (' logger2 error message ') logger2.critical (' Logger2 Critical message ')

Results:

Here are two questions:

<1> we clearly passed logger1.setlevel (logging. Debug) Set the log level of Logger1 to debug, why does not display the debug level of log information at the time of display, but from the info level of the log start to display it?

The original logger1 and Logger2 correspond to the same logger instance, The logger instance returned is the same as long as the name parameter in Logging.getlogger (name) is the same, and only one, that is, name corresponds to logger instance one by one. The log level for MyLogger is set to Logging.info in the Logger2 instance through Logger2.setlevel (Logging.info), so the output of the last Logger1 follows the log level set later.

<2> why is each output of Logger1 and Logger2 displayed two times?
This is because we created the root Logger through Logger = Logging.getlogger (), and logger1 = Logging.getlogger (' MyLogger ') created the child of root Logger (root .) Mylogger,logger2 the same. And the child, grandson, great-grandchild ... The processing of messages distributed to his handler is also passed on to all ancestor logger processing.

OK, so now we put

# Logger.addhandler (FH)

# Logger.addhandler (CH) out, let's see the effect:

because we annotated the location of the Logger object, we used the default mode, which is the standard output mode. because its parent does not have a way to set the file display, it is printed only once.

Children, grandchildren, great-grandchild ... The log level, Handler, and filter settings from ancestors can be inherited by layer, or through Logger.setlevel (LEL), Logger.addhandler (HDLR), Logger.removehandler (HDLR), Logger.addfilter (filt), Logger.removefilter (filt). Set your own special log level, Handler, Filter. If not set, the inherited value is used.

<3>Filter
Limits only the logs that satisfy the filtering rules will be output.
For example, we define filter = logging. Filter (' A.B.C '), and add this filter to a handler, then only A.B.C with the logger prefix in the logger of the handler will be able to output its log.

Filter = logging. Filter (' MyLogger ')

Logger.addfilter (Filter)

This is only a filter for the Logger object

If you want to filter on all objects, then:

Filter = logging. Filter (' MyLogger ')

Fh.addfilter (Filter)

Ch.addfilter (Filter)

In this way, all logger objects that add FH or CH will be screened.

Full Code 1:

View Code

full code 2:

View Code

Application:

View Code

Python Logging Module

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.