logging.config — 日志配置

源代码: Lib/logging/config.py


本节描述配置日志模块的 API。

配置函数

下列函数用于配置 logging 模块。它们位于 logging.config module. Their use is optional — you can configure the logging module using these functions or by making calls to the main API (defined in logging itself) and defining handlers which are declared either in logging or logging.handlers .

logging.config. dictConfig ( config )

Takes the logging configuration from a dictionary. The contents of this dictionary are described in 配置字典模式 下文。

If an error is encountered during configuration, this function will raise a ValueError , TypeError , AttributeError or ImportError with a suitably descriptive message. The following is a (possibly incomplete) list of conditions which will raise an error:

  • A level which is not a string or which is a string not corresponding to an actual logging level.

  • A propagate value which is not a boolean.

  • An id which does not have a corresponding destination.

  • A non-existent handler id found during an incremental call.

  • An invalid logger name.

  • Inability to resolve to an internal or external object.

Parsing is performed by the DictConfigurator class, whose constructor is passed the dictionary used for configuration, and has a configure() 方法。 logging.config module has a callable attribute dictConfigClass which is initially set to DictConfigurator . You can replace the value of dictConfigClass with a suitable implementation of your own.

dictConfig() 调用 dictConfigClass passing the specified dictionary, and then calls the configure() method on the returned object to put the configuration into effect:

def dictConfig(config):
    dictConfigClass(config).configure()