Logging module in "python,logging" python

Source: Internet
Author: User

This article is transferred from Kenby's blog, more comprehensive move, turn to stay for later use.

http://kenby.iteye.com/blog/1162698

First, starting from a usage scenario

ImportLogging#Create a loggerLogger = Logging.getlogger ('MyLogger') Logger.setlevel (logging. DEBUG)#create a handler to write to the log fileFH = logging. Filehandler ('Test.log') Fh.setlevel (logging. DEBUG)#create another handler for output to the consoleCH =logging. Streamhandler () Ch.setlevel (logging. DEBUG)#define the output format of the handlerFormatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s') Fh.setformatter (formatter) ch.setformatter (formatter)#Add handler to loggerLogger.addhandler (FH) logger.addhandler (CH)#record a logLogger.info ('Foorbar')

After running, there is a log in both the console and the log file:

2011-08-31 19:18:29,816-mylogger-info-foorbar  

Second, the Logging module API

In combination with the above example, we'll say a few of the most commonly used APIs

logging. GetLogger ([name]) Returns a logger instance that returns root logger if no name is specified. As long as name is the same, the returned logger instances are the same and only one, that is, the name and the logger instance are one by one corresponding. This means that there is no need to pass logger instances in each module. As long as you know name, you can get the same logger instance


Logger. SetLevel (lvl) sets the level of logger, which has the following levels:

Import Logging Print (logging. NOTSET) 0print(logging. DEBUG)print (logging.info)print(logging. WARNING)print(logging. ERROR)print(logging. CRITICAL)50

NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL
If the Looger level is set to info, then logs less than the info level are not output, and logs that are greater than or equal to the info level are output

Logger.debug ("Foobar")#No outputLogger.info ("Foobar")#OutputLogger.warning ("Foobar")#OutputLogger.error ("Foobar")#OutputLogger.critical ("Foobar")#Output

logger. addhandler ( hdlr ) logger can hire handler to help it with the logs, handler mainly in the following ways: streamhandler: Output to console filehandler:   output to file Handler can also set its own level and output format.
logging. basicconfig ( [ **kwargs ] ) * This function is used to configure root logger to create a streamhandler,    default format for root logger. * These functions: Logging.debug (), Logging.info (), logging.warning (),    logging.error (), logging.critical ()   If the call is found root logger does not have any    handler, will automatically call Basicconfig add a handler* if root logger already have handler, this function does not do anything
Use Basicconfig to configure the output format and level of the root logger:

Import logginglogging.basicconfig (Format='% (levelname) s:% (message) s', level=  Logging. DEBUG) logging.debug ('Thismessage should appear on the console')

Third, about root logger and logger father-son relationship
The previous mentions of root logger, in fact logger instances there is a parent-child relationship, root logger is the topmost logger, it is all logger ancestors. Such as:

Root logger is the default logger
If you do not create logger instances, call the Functions Logging.debug (), Logging.info () logging.warning (), Logging.error (), logging.critical () directly,
Then the logger used is root logger, which can be created automatically and is a single instance.

How to get root logger
The root logger instance is obtained by Logging.getlogger () or Logging.getlogger ("").

The default Level
Root logger The default level is logging. WARNING

How to represent a parent-child relationship
The naming of the logger name can represent a parent-child relationship between logger. Like what:
Parent_logger = Logging.getlogger (' foo ')
Child_logger = Logging.getlogger (' Foo.bar ')

What is effective level
Logger has a concept called effective level. If a logger does not display the level, then it
With the father's level. If the father does not display the level, just use the father's level to push ....
At the end of the root logger, you must set the level. The default is logging. WARNING
After the child loggers gets the message, both the message is distributed to its handler processing, which is also passed to all ancestors logger processing,

Take a look at an example

ImportLogging#Set Root LoggerR =logging.getlogger () ch=logging. Streamhandler () Ch.setlevel (logging. DEBUG) Formatter= Logging. Formatter ('% (asctime) s-% (levelname) s-% (message) s') Ch.setformatter (formatter) r.addhandler (CH)#Create a logger as a fatherp = Logging.getlogger ('Foo') P.setlevel (logging. DEBUG) Ch=logging. Streamhandler () Ch.setlevel (logging. DEBUG) Formatter= Logging. Formatter ('% (asctime) s-% (message) s') Ch.setformatter (formatter) p.addhandler (CH)#Create a child loggerc = Logging.getlogger ('Foo.bar') C.debug ('Foo')

The output is as follows:

2011-08-31 21:04:29,893- foo2011-08-31 21:04:29,893-debug-foo

As can be seen, the child logger without any handler, so the message is not processed. But it forwarded the message to its father and root logger. Output two logs at the end of the line.

Logging module in "python,logging" python

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.