First, starting from a usage scenario
Develop a log system that outputs logs to the console and to log files
Python code
- Import logging
- # Create a Logger
- Logger = Logging.getlogger (' MyLogger ')
- Logger.setlevel (logging. DEBUG)
- # Create a handler to write to the log file
- FH = logging. Filehandler (' Test.log ')
- Fh.setlevel (logging. DEBUG)
- # Create another handler for output to the console
- ch = logging. Streamhandler ()
- Ch.setlevel (logging. DEBUG)
- # define the output format of the handler
- Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')
- Fh.setformatter (Formatter)
- Ch.setformatter (Formatter)
- # Add Handler to Logger
- Logger.addhandler (FH)
- Logger.addhandler (CH)
- # Record a log
- Logger.info (' Foorbar ')
After running, there is a log in both the console and the log file:
Java code
- 2011-08-: 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:
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
Python code
- logger.debug (# not output
- logger.info (# output
- Logger.warning ( "Foobar") # output
- logger.error ( "Foobar") # output
- logger.critical (# output
Logger. AddHandler (hdlr)logger can hire handler to help it with the log, handler mainly have the following: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 as root Logger Create a Streamhandler, set the default format. * These functions: Logging.debug (), Logging.info (), logging.warning (), Logging.error (), logging.critical () If the call finds root Logger does not have any handler, will automatically call Basicconfig add a handler* if root logger already has handler, this function does not do anything
Use Basicconfig to configure the output format and level of the root logger:
Python code
- import logging
- Logging.basicconfig (Format= '% (levelname) s:% (message) s ', level=logging. DEBUG)
- logging.debug (
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 Logging.debug (), Logging.info () logging.warning (), Logging.error (), logging.critical () These functions, the logger used is root logger, which can be created automatically and is a single instance.
how to get root logger by Logging.getlogger () or Logging.getlogger ("") to get the root logger instance.
The defaultlevel root logger default level is logging. WARNING
how to represent the name of a parent-child relationship logger can represent a parent-child relationship between logger. For example: Parent_logger = Logging.getlogger (' foo ') Child_logger = Logging.getlogger (' Foo.bar ')
what is effectivelevel logger has a concept called effective level. If a logger does not display the level, then it uses the level of the father. 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. Warningchild Loggers received the message, both the message is distributed to its handler processing, also passed to all ancestors logger processing,
Take a look at an example
Python code
- Import logging
- # Set Root Logger
- R = 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 father
- p = 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 logger
- c = Logging.getlogger (' foo.bar ')
- C.debug (' foo ')
The output is as follows:
Python code
- 2011-08- :893-foo
- 2011-08- :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.
Using Python's logging module