Python log configuration_byseyOrd

Several ways to configure Python

1) Use Python code to explicitly create loggers, handlers and formatters and call their configuration functions respectively;

2) Create a log configuration file, and then use the fileConfig () function to read the content of the file;

3) Create a dict containing configuration information, and then pass it a dictConfig () function;

It should be noted that it logging.basicConfig()also belongs to the first method, which only encapsulates the configuration functions of loggers, handlers and formatters.

The advantage of the second configuration method over the first configuration method is that it separates the configuration information from the code

Use Python code to implement log configuration

1  # Create a logger logger and set its log level to DEBUG 
2 logger = logging.getLogger ( ' simple_logger ' )
 3  logger.setLevel (logging.DEBUG)
 4   
5  # Create a stream processor handler and set its log level to DEBUG 
. 6 Handler = logging.StreamHandler (sys.stdout)
 . 7  handler.setLevel (logging.DEBUG)
 . 8   
. 9  # Create a formatter formatter and add it to the processor Handler 
10 formatter = logging.Formatter ( " % (the asctime) S - % (name) s-% (levelname) s-% (message) s " )
 11  handler.setFormatter (formatter)
 12   
13  #The above is added to create a processor logger logger Handler 
14  logger.addHandler (Handler)
 15   
16  # log output 
. 17 logger.debug ( ' Debug Message ' )
 18 is logger.info ( ' info Message ' )
 . 19 logger.warn ( ' The warn message ' )
 20 logger.error ( ' error message ' )
 21 logger.critical ( ' critical message ' )

Use configuration file and fileConfig () function to realize log configuration

# Reads the configuration file for the log 
logging.config.fileConfig ( ' logging.conf ' ) 
 
# Create a logger Logger 
Logger = logging.getLogger ( ' SimpleExample ' ) 
 
# log output 
logger.debug ( ' Debug Message ' ) 
logger.info ( ' info message ' ) 
logger.warn ( ' warn message ' ) 
logger.error ( ' error message ' ) 
logger.critical ( ' critical message ' )

logging.conf

[loggers]
keys=root,simpleExample
 
[handlers]
keys=fileHandler,consoleHandler
 
[formatters]
keys=simpleFormatter
 
[logger_root]
level=DEBUG
handlers=fileHandler
 
[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0
 
[handler_consoleHandler]
class=StreamHandler
args=(sys.stdout,)
level=DEBUG
formatter=simpleFormatter
 
[handler_fileHandler]
class=FileHandler
args=('logging.log', 'a')
level=ERROR
formatter=simpleFormatter
 
[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt= "%Y-%m-%d %H:%M:%S"

  Description of the configuration file

  1) The loggers, handlers, and formatters must be included in the configuration file. They use the keys option to specify the loggers, handlers, and formatters that have been defined in the configuration file. Multiple values ​​are separated by commas; in addition, the loggers section The keys in must contain the value of root;

  2) The loggers, processors, and formatters specified in loggers, handlers, and formatters all need to be defined in separate sections below. The naming rules of seciton are [logger_loggerName], [formatter_formatterName], [handler_handlerName]

  3) The section that defines the logger must specify the two options of level and handlers, the possible values ​​of level are DEBUG, INFO, WARNING, ERROR, CRITICAL, NOTSET, where NOTSET means that all levels of log messages must be recorded, including user-defined levels ; The value of handlers is a comma-separated list of handler names. The handlers that appear here must appear in the section [handlers], and the corresponding handlers must have a corresponding section definition in the configuration file;

  4) For non-root loggers, in addition to the two options of level and handlers, some additional options are required, where qualname is an option that must be provided, which represents the name in the logger hierarchy, which is passed in the application code The name gets the logger; propagate is optional. The default is 1, which means that the message will be passed to the handler of the high-level logger. Usually we need to specify its value as 0. This can be seen in the following example; in addition, for non-root logger If the level is set to NOTSET, the system will find a high-level logger to determine the effective level of this logger.

  5) Two options, class and args, must be specified in the section that defines the handler, level and formatter are optional options; class represents the class name used to create the handler, and args represents the parameter passed to the handler class initialization method specified by class Must be in the form of a tuple, even if only one parameter value needs to be in the form of a tuple; level is the same as the level in the logger, and the formatter specifies the formatter used by the processor, specified here The formatter name must appear in the formatters section, and there must be a section definition for the formatter in the configuration file; if the formatter is not specified, the handler will record the message as a log message without adding extra time, Logger name and other information;

  6) The options in the sectioin that define the formatter are all optional, including format for specifying the format string, which defaults to the message string itself; datefmt for specifying the time format of asctime, which defaults to '% Y-% m- % d% H:% M:% S '; class is used to specify the formatter class name, the default is logging.Formatter;  

  7) Change the value of the propagate attribute in the simpleExample handler definition in logging.conf to 1, or delete the option (the default value is 1), which shows that the simpleExample logger passes the log record to the superior after processing the log record The root logger does the processing again, so there are only two places where the output of the log is recorded. Usually, we need to display the specified value of propagate to prevent log records from passing to the upper logger.

  8) When a logger is not set to any processor, the system will look for the log processor set on the upper logger of the logger to process log records

Using a dictionary to implement log configuration (dictconfig)

When using dictionary configuration, it can convert a lot of data into a dictionary. For example, we can use JSON format configuration files, YAML format configuration files, and then fill them into a configuration dictionary

import logging
import logging.config
import yaml
 
with open('logging.yml', 'r') as f_conf:
  dict_conf = yaml.load(f_conf)
logging.config.dictConfig(dict_conf)
 
logger = logging.getLogger('simpleExample')
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

yaml file

version: 1
formatters:
 simple:
  format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
 console:
  class: logging.StreamHandler
  level: DEBUG
  formatter: simple
  stream: ext://sys.stdout
 console_err:
  class: logging.StreamHandler
  level: ERROR
  formatter: simple
  stream: ext://sys.stderr
loggers:
 simpleExample:
  level: DEBUG
  handlers: [console]
  propagate: yes
root:
 level: DEBUG
 handlers: [console_err]

  Description of dictionary configuration information

The key name describes the required 
version. Its value is an integer value, indicating the version of the configuration format. The only available value is the 1
formatters option, and its value is a dictionary object. The key of each element of the dictionary object is the one to be defined. Formatter name, value is a dict composed of formatter configuration information, such as format and datefmt
filters are optional, the value is a dictionary object, the key of each element of the dictionary object is the name of the filter to be defined, and value is the filter The dict composed of the configuration information, such as the name
handlers option, the value is a dictionary object, the key of each element of the dictionary object is the name of the processor to be defined, and the value is the dcit composed of the processor configuration information, such as class, Level, formatter, and filters,
        where class is required and others are optional; other configuration information will be passed to the constructor of the processor class specified by class, such as stream, filename, maxBytes, and backupCount in the following handler definition example and other
loggers options, whose value is a dictionary object, key object for each element of the dictionary is to be given Log name, value is dcit configuration information logger composition, such as level, handlers, filters and the Propagate
root option, which is the root logger configuration information, and its value is a dictionary object. Unless you explicitly specify propagate value as no when defining other loggers, the handlers defined by root logger will be applied to other loggers
incremental is optional, the default value is False. The significance of this option is that if the objects defined here already exist, then whether the definitions of these objects apply to the existing objects. A value of False means that existing objects will be redefined.
disable_existing_loggers is optional, and the default value is True. This option is used to specify whether to disable the existing loggers loggers, if the incremental value is True, this option will be ignored

 

This article is transferred from the blog

 

Guess you like

Origin www.cnblogs.com/seyOrd/p/12690904.html