The following function functions configure the logging module. They are located in the Logging.config module. Their use is optional-you can use these function functions to configure the logging module, or by calling the main API (defined in logging) and defining handlers defined in logging or logging.handlers.
Logging.config.dictConfig (config):
Gets the logging configuration from the dictionary.
Logging.config.fileConfig (fname, Defaults=none, disable_existing_loggers=true):
Read the logging configuration from a file named fname in the Configparser format.
Configuration dictionary:
The dictionary passed to Dictconfig () must contain the following keywords:
Version: The integer value that is set to represent the mode version, and the only valid value currently is 1
Formatters: The value is a dictionary type, where each key is a formatter ID, each value is a dictionary and describes how to configure the corresponding formatter instance
Filters: The value is a dictionary type, where each key is a filter, each value is a dictionary and describes how to configure the appropriate filter instance
Handlers: The value is a dictionary type, each key is a handler ID, and each value is a dictionary that describes how to configure the corresponding handler instance
The Handlers configuration dictionary searches for the following key:
Class (mandatory): The full description name of the handler class
Level (optional): Handler rating
Formatter (optional): Formatter ID for the handler
Filters (optional): List of filter IDs for this handler
Loggers: The value is a dictionary type, where each key is a logger name, and each value is a dictionary that describes the configuration of the corresponding logger instance
Root:root the configuration of the logger. The configured processing will be the same as any logger except that propagate does not apply
Incremental: Whether the configuration is interpreted as an increment of the already existing configuration. False by default
Disable_existing_loggers: Whether any existing logger are forbidden. By default true, if incremental is true, the value is ignored
Look at an actual logging configuration file:
version: 1formatters: simple: format: '% (asctime) s - % ( Name) s - % (levelname) s - % (message) s ' handlers: console: Class: logging. streamhandler level: debug formatter: simple stream: ext://sys.stdout info_file_handler: class: logging.handlers.RotatingFileHandler level: INFO formatter: simple filename: info.log maxbytes: 10485760 backupcount: 3 encoding: utf8 error_file_handler: class: logging.handlers.rotatingfilehandler level: ERROR formatter: simple filename: Error.log maxbytes: 10485760 backupcount: 3 encoding: utf8loggers: simpleExample: level: DEBUG handlers: [console] propagate: noroot: level: debug handlers: [console, info_file_handler, error_file_handler]
Defines a formatter called ' simple '.
Defines three handler: ' Console ', ' Info_file_handler ', ' Error_file_handler '
Defines a logger: ' Simpleexample '
Defines a root
The default logger uses root configuration, such as Common Logging.getloggeer (__name__), because __name__ corresponding logger is not defined in the configuration file, so create a logger instance of the default root configuration, It includes three Handler:console,info_file_handler,error_file_handler, and the corresponding logs are also exported to three destinations defined by handler. If you call Logging.getloggeer (' Simpleexample '), the logger instance that matches the name ' Simpleexample ' in the configuration file is created, and its handler is console, which is the output log on the console.
Logging.handlers.RotatingFileHandler can implement automatic log file coverage, if you need to implement log file changes (such as deleted) automatically generated, you need to use Logging.handlers.WatchedFileHandler, There is no handler to find the two functions at the same time.
A brief analysis of Python logging module configuration method