[Turn] python in the log level information

Before the discovery of a log output when using python logging package comes with some logging statement has no output, felt rather strange went to check a bit logging documents. And then found its logging level when setting the reference and can affect the final output. The default logging package output is divided into six levels: A

score here would represent the high and low levels corresponding keyword appears, NOTSET lowest, CRITICAL highest. The statement will cover the high-grade low-level statements.
Generally, we will configure a logging class format in the main program with the logging time, then a direct reference to the subroutine ( for example ). If we set the default format when the level set after the call is to be higher than that, then logging statement calls will not output; on the contrary, the default level will be called when the output level of coverage statement.
The following describes a simple procedure under (More specific examples can be found in the official documentation ):

import logging
import sys

def test_log_level(): # set default logging configuration logger = logging.getLogger() # initialize logging class logger.setLevel(logging.DEBUG) # default log level format = logging.Formatter("%(asctime)s - %(message)s") # output format sh = logging.StreamHandler(stream=sys.stdout) # output to standard output sh.setFormatter(format) logger.addHandler(sh) # use logging to generate log ouput logger.info("this is info") logger.debug("this is debug") logger.warning("this is warning") logging.error("this is error") logger.critical("this is critical") test_log_level() 

Export

2016-09-13 18:53:05,966 INFO - this is info 2016-09-13 18:53:05,966 DEBUG - this is debug 2016-09-13 18:53:05,966 WARNING - this is warning 2016-09-13 18:53:05,967 ERROR - this is error 2016-09-13 18:53:05,967 CRITICAL - this is critical 

If we raise the level of the logger, such as the INFO

logger.setLevel(logging.INFO)

Then the output will be:

2016-09-13 18:52:37,492 INFO - this is info 2016-09-13 18:52:37,492 WARNING - this is warning 2016-09-13 18:52:37,492 ERROR - this is error 2016-09-13 18:52:37,492 CRITICAL - this is critical 

Call logging.debug did not produce a log, that is above that level no more than invoke the default level will be no output; while others call level than the default level when logging is call level.

Summary : because the general log system calls the most is logging.INFO, it is generally the default level setting is not too high or INFO to select DEBUG. Check labels can help us quickly through the different levels of logging calls to find out the bug. So personally I feel that the logging module still need to take a look at the document to understand and grasp.

 
Tags:  Python logging

Before the discovery of a log output when using python logging package comes with some logging statement has no output, felt rather strange went to check a bit logging documents. And then found its logging level when setting the reference and can affect the final output. The default logging package output is divided into six levels: A

score here would represent the high and low levels corresponding keyword appears, NOTSET lowest, CRITICAL highest. The statement will cover the high-grade low-level statements.
Generally, we will configure a logging class format in the main program with the logging time, then a direct reference to the subroutine ( for example ). If we set the default format when the level set after the call is to be higher than that, then logging statement calls will not output; on the contrary, the default level will be called when the output level of coverage statement.
The following describes a simple procedure under (More specific examples can be found in the official documentation ):

import logging
import sys

def test_log_level(): # set default logging configuration logger = logging.getLogger() # initialize logging class logger.setLevel(logging.DEBUG) # default log level format = logging.Formatter("%(asctime)s - %(message)s") # output format sh = logging.StreamHandler(stream=sys.stdout) # output to standard output sh.setFormatter(format) logger.addHandler(sh) # use logging to generate log ouput logger.info("this is info") logger.debug("this is debug") logger.warning("this is warning") logging.error("this is error") logger.critical("this is critical") test_log_level() 

Export

2016-09-13 18:53:05,966 INFO - this is info 2016-09-13 18:53:05,966 DEBUG - this is debug 2016-09-13 18:53:05,966 WARNING - this is warning 2016-09-13 18:53:05,967 ERROR - this is error 2016-09-13 18:53:05,967 CRITICAL - this is critical 

If we raise the level of the logger, such as the INFO

logger.setLevel(logging.INFO)

Then the output will be:

2016-09-13 18:52:37,492 INFO - this is info 2016-09-13 18:52:37,492 WARNING - this is warning 2016-09-13 18:52:37,492 ERROR - this is error 2016-09-13 18:52:37,492 CRITICAL - this is critical 

Call logging.debug did not produce a log, that is above that level no more than invoke the default level will be no output; while others call level than the default level when logging is call level.

Summary : because the general log system calls the most is logging.INFO, it is generally the default level setting is not too high or INFO to select DEBUG. Check labels can help us quickly through the different levels of logging calls to find out the bug. So personally I feel that the logging module still need to take a look at the document to understand and grasp.

Guess you like

Origin www.cnblogs.com/waimen/p/12630649.html