Home  >  Article  >  Backend Development  >  Several ways to configure logs in python

Several ways to configure logs in python

不言
不言Original
2018-04-18 14:22:521250browse

This article mainly introduces several ways to explain the configuration log of Python in detail. Now I will share it with you and give you a reference. Let’s take a look together

As developers, we can configure logging in the following 3 ways:

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 contents of the file;

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

It should be noted that logging.basicConfig() also belongs to the first method, which is only for loggers , the configuration functions of handlers and formatters are encapsulated. In addition, the advantage of the second configuration method over the first configuration method is that it separates the configuration information and code, which reduces the maintenance cost of the log and also allows non-developers to easily Modify log configuration.

1. Use Python code to implement log configuration

The code is as follows:

# 创建一个日志器logger并设置其日志级别为DEBUG
logger = logging.getLogger('simple_logger')
logger.setLevel(logging.DEBUG)

# 创建一个流处理器handler并设置其日志级别为DEBUG
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)

# 创建一个格式器formatter并将其添加到处理器handler
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)

# 为日志器logger添加上面创建的处理器handler
logger.addHandler(handler)

# 日志输出
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

Run output:

2017-05-15 11:30:50,955 - simple_logger - DEBUG - debug message
2017-05-15 11:30:50,955 - simple_logger - INFO - info message
2017-05-15 11:30:50,955 - simple_logger - WARNING - warn message
2017-05-15 11:30:50,955 - simple_logger - ERROR - error message
2017-05-15 11:30:50,955 - simple_logger - CRITICAL - critical message

2. Use the configuration file and fileConfig() function to implement log configuration

Now we use the configuration file to achieve the same function as above:

# 读取日志配置文件内容
logging.config.fileConfig('logging.conf')

# 创建一个日志器logger
logger = logging.getLogger('simpleExample')

# 日志输出
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

The content of the configuration file logging.conf is as follows:

[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=

Run output:

2017-05-15 11:32:16,539 - simpleExample - DEBUG - debug message
2017-05-15 11:32:16,555 - simpleExample - INFO - info message
2017-05-15 11:32:16,555 - simpleExample - WARNING - warn message
2017-05-15 11:32:16,555 - simpleExample - ERROR - error message
2017-05-15 11:32:16,555 - simpleExample - CRITICAL - critical message

1. About fileConfig () Function description:

This function is actually an encapsulation of the configparser module. For an introduction to the configparser module, please refer to <.

Function definition:

This function is defined under the logging.config module:


Copy code The code is as follows:


logging.config.fileConfig(fname, defaults=None, disable_existing_loggers=True)

Parameters:

  1. fname: Represents the file name or file object of the configuration file

  2. defaults: Specifies the default value passed to ConfigParser

  3. disable_existing_loggers: This is A Boolean value, the default value is True (for backward compatibility) to disable existing loggers unless they or their ancestors explicitly appear in the logging configuration; if the value is False, keep existing loggers enabled.

2. Configuration file format description:

As mentioned above, the fileConfig() function is the ConfigParser/configparser The encapsulation of the module, that is to say, the fileConfig() function is based on the ConfigParser/configparser module to understand the log configuration file. In other words, the basic format of the configuration file that the fileConfig() function can understand is consistent with the ConfigParser/configparser module, but only the contained in the file is modified on this basis. section and option have some regulations and restrictions, such as:

1) The configuration file must contain sections such as loggers, handlers, and formatters, which are specified through the keys option. The loggers, handlers and formatters that have been defined in the configuration file are separated by commas; in addition, the keys in the loggers section must contain the value root;

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

3) The section defining logger must specify the two options level and handlers. The possible values ​​​​of level are DEBUG, INFO, WARNING, ERROR, CRITICAL, NOTSET, where NOTSET indicates 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 [handlers] section, and The corresponding handler must have a corresponding section definition in the configuration file;

4) For non-root logger, in addition to the two options of level and handlers, some additional options are needed, where qualname is An option that must be provided, which represents the name in the logger hierarchy. The logger can be obtained through this name in the application code; propagate is optional, and its default is 1, which means that the message will be passed to the handler of the high-level logger. Usually we need Specify its value as 0. You can see the following example for this. In addition, if the level of a non-root logger is set to NOTSET, the system will look for higher-level loggers to determine the effective level of this logger.

5)定义handler的section中必须指定class和args这两个option,level和formatter为可选option;class表示用于创建handler的类名,args表示传递给class所指定的handler类初始化方法参数,它必须是一个元组(tuple)的形式,即便只有一个参数值也需要是一个元组的形式;level与logger中的level一样,而formatter指定的是该处理器所使用的格式器,这里指定的格式器名称必须出现在formatters这个section中,且在配置文件中必须要有这个formatter的section定义;如果不指定formatter则该handler将会以消息本身作为日志消息进行记录,而不添加额外的时间、日志器名称等信息;

6)定义formatter的sectioin中的option都是可选的,其中包括format用于指定格式字符串,默认为消息字符串本身;datefmt用于指定asctime的时间格式,默认为'%Y-%m-%d %H:%M:%S';class用于指定格式器类名,默认为logging.Formatter;

说明:

配置文件中的class指定类名时,该类名可以是相对于logging模块的相对值,如:FileHandlerhandlers.TimeRotatingFileHandler;也可以是一个绝对路径值,通过普通的import机制来解析,如自定义的handler类mypackage.mymodule.MyHandler,但是mypackage需要在Python可用的导入路径中--sys.path。

3. 对于propagate属性的说明

实例1:

我们把logging.conf中simpleExample这个handler定义中的propagate属性值改为1,或者删除这个option(默认值就是1):

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=1

现在来执行同样的代码:

# 读取日志配置文件内容
logging.config.fileConfig(&#39;logging.conf&#39;)

# 创建一个日志器logger
logger = logging.getLogger(&#39;simpleExample&#39;)

# 日志输出
logger.debug(&#39;debug message&#39;)
logger.info(&#39;info message&#39;)
logger.warn(&#39;warn message&#39;)
logger.error(&#39;error message&#39;)
logger.critical(&#39;critical message&#39;)

我们会发现,除了在控制台有输出信息时候,在logging.log文件中也有内容输出:

2017-05-15 16:06:25,366 - simpleExample - ERROR - error message
2017-05-15 16:06:25,367 - simpleExample - CRITICAL - critical message

这说明simpleExample这个logger在处理完日志记录后,把日志记录传递给了上级的root logger再次做处理,所有才会有两个地方都有日志记录的输出。通常,我们都需要显示的指定propagate的值为0,防止日志记录向上层logger传递。

实例2:

现在,我们试着用一个没有在配置文件中定义的logger名称来获取logger:

# 读取日志配置文件内容
logging.config.fileConfig(&#39;logging.conf&#39;)

# 用一个没有在配置文件中定义的logger名称来创建一个日志器logger
logger = logging.getLogger(&#39;simpleExample1&#39;)

# 日志输出
logger.debug(&#39;debug message&#39;)
logger.info(&#39;info message&#39;)
logger.warn(&#39;warn message&#39;)
logger.error(&#39;error message&#39;)
logger.critical(&#39;critical message&#39;)

运行程序后,我们会发现控制台没有任何输出,而logging.log文件中又多了两行输出:

2017-05-15 16:13:16,810 - simpleExample1 - ERROR - error message
2017-05-15 16:13:16,810 - simpleExample1 - CRITICAL - critical message

这是因为,当一个日志器没有被设置任何处理器是,系统会去查找该日志器的上层日志器上所设置的日志处理器来处理日志记录。simpleExample1在配置文件中没有被定义,因此logging.getLogger(simpleExample1)这行代码这是获取了一个logger实例,并没有给它设置任何处理器,但是它的上级日志器--root logger在配置文件中有定义且设置了一个FileHandler处理器,simpleExample1处理器最终通过这个FileHandler处理器将日志记录输出到logging.log文件中了。

三、使用字典配置信息和dictConfig()函数实现日志配置

Python 3.2中引入的一种新的配置日志记录的方法--用字典来保存logging配置信息。这相对于上面所讲的基于配置文件来保存logging配置信息的方式来说,功能更加强大,也更加灵活,因为我们可把很多的数据转换成字典。比如,我们可以使用JSON格式的配置文件、YAML格式的配置文件,然后将它们填充到一个配置字典中;或者,我们也可以用Python代码构建这个配置字典,或者通过socket接收pickled序列化后的配置信息。总之,你可以使用你的应用程序可以操作的任何方法来构建这个配置字典。

这个例子中,我们将使用YAML格式来完成与上面同样的日志配置。

首先需要安装PyYAML模块:

pip install PyYAML

Python代码:

import logging
import logging.config
import yaml

with open(&#39;logging.yml&#39;, &#39;r&#39;) as f_conf:
  dict_conf = yaml.load(f_conf)
logging.config.dictConfig(dict_conf)

logger = logging.getLogger(&#39;simpleExample&#39;)
logger.debug(&#39;debug message&#39;)
logger.info(&#39;info message&#39;)
logger.warn(&#39;warn message&#39;)
logger.error(&#39;error message&#39;)
logger.critical(&#39;critical message&#39;)

logging.yml配置文件的内容:
version: 1
formatters:
 simple:
  format: &#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;
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]

输出结果:

2017-05-21 14:19:31,089 - simpleExample - DEBUG - debug message
2017-05-21 14:19:31,089 - simpleExample - INFO - info message
2017-05-21 14:19:31,089 - simpleExample - WARNING - warn message
2017-05-21 14:19:31,089 - simpleExample - ERROR - error message
2017-05-21 14:19:31,090 - simpleExample - CRITICAL - critical message

1. 关于dictConfig()函数的说明:

该函数实际上是对configparser模块的封装。

函数定义:

该函数定义在loging.config模块下:

logging.config.dictConfig(config)

该函数可以从一个字典对象中获取日志配置信息,config参数就是这个字典对象。关于这个字典对象的内容规则会在下面进行描述。

2. 配置字典说明

无论是上面提到的配置文件,还是这里的配置字典,它们都要描述出日志配置所需要创建的各种对象以及这些对象之间的关联关系。比如,可以先创建一个名额为“simple”的格式器formatter;然后创建一个名为“console”的处理器handler,并指定该handler输出日志所使用的格式器为"simple";然后再创建一个日志器logger,并指定它所使用的处理器为"console"。

传递给dictConfig()函数的字典对象只能包含下面这些keys,其中version是必须指定的key,其它key都是可选项:


key名称 描述
version 必选项,其值是一个整数值,表示配置格式的版本,当前唯一可用的值就是1
formatters 可选项,其值是一个字典对象,该字典对象每个元素的key为要定义的格式器名称,value为格式器的配置信息组成的dict,如format和datefmt
filters 可选项,其值是一个字典对象,该字典对象每个元素的key为要定义的过滤器名称,value为过滤器的配置信息组成的dict,如name
handlers 可选项,其值是一个字典对象,该字典对象每个元素的key为要定义的处理器名称,value为处理器的配置信息组成的dcit,如class、level、formatter和filters,其中class为必选项,其它为可选项;其他配置信息将会传递给class所指定的处理器类的构造函数,如下面的handlers定义示例中的stream、filename、maxBytes和backupCount等
loggers 可选项,其值是一个字典对象,该字典对象每个元素的key为要定义的日志器名称,value为日志器的配置信息组成的dcit,如level、handlers、filters 和 propagate(yes
root 可选项,这是root logger的配置信息,其值也是一个字典对象。除非在定义其它logger时明确指定propagate值为no,否则root logger定义的handlers都会被作用到其它logger上
incremental 可选项,默认值为False。该选项的意义在于,如果这里定义的对象已经存在,那么这里对这些对象的定义是否应用到已存在的对象上。值为False表示,已存在的对象将会被重新定义。
disable_existing_loggers 可选项,默认值为True。该选项用于指定是否禁用已存在的日志器loggers,如果incremental的值为True则该选项将会被忽略

handlers定义示例:

handlers:
 console:
  class : logging.StreamHandler
  formatter: brief
  level  : INFO
  filters: [allow_foo]
  stream : ext://sys.stdout
 file:
  class : logging.handlers.RotatingFileHandler
  formatter: precise
  filename: logconfig.log
  maxBytes: 1024
  backupCount: 3

3. 关于外部对象的访问

需要说明的是,上面所使用的对象并不限于loggging模块所提供的对象,我们可以实现自己的formatter或handler类。另外,这些类的参数也许需要包含sys.stderr这样的外部对象。如果配置字典对象是使用Python代码构造的,可以直接使用sys.stdout、sys.stderr;但是当通过文本文件(如JSON、YAML格式的配置文件)提供配置时就会出现问题,因为在文本文件中,没有标准的方法来区分sys.stderr和字符串 'sys.stderr'。为了区分它们,配置系统会在字符串值中查找特定的前缀,例如'ext://sys.stderr'中'ext://'会被移除,然后import sys.stderr



The above is the detailed content of Several ways to configure logs in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn