详解python之配置日志的几种方式
|
作为开发者,我们可以通过以下3中方式来配置logging: 1)使用Python代码显式的创建loggers,handlers和formatters并分别调用它们的配置函数; 2)创建一个日志配置文件,然后使用fileConfig()函数来读取该文件的内容; 3)创建一个包含配置信息的dict,然后把它传递个dictConfig()函数; 需要说明的是, 一、使用Python代码实现日志配置 代码如下:
# 创建一个日志器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')
运行输出: 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 二、使用配置文件和fileConfig()函数实现日志配置 现在我们通过配置文件的方式来实现与上面同样的功能:
# 读取日志配置文件内容
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')
配置文件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=
运行输出: 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. 关于fileConfig()函数的说明: 该函数实际上是对 函数定义: 该函数定义在loging.config模块下: 复制代码 代码如下: 参数:
2. 配置文件格式说明: 上面提到过, 1)配置文件中一定要包含loggers、handlers、formatters这些section,它们通过keys这个option来指定该配置文件中已经定义好的loggers、handlers和formatters,多个值之间用逗号分隔;另外loggers这个section中的keys一定要包含root这个值; 2)loggers、handlers、formatters中所指定的日志器、处理器和格式器都需要在下面以单独的section进行定义。seciton的命名规则为[logger_loggerName]、[formatter_formatterName]、[handler_handlerName] 3)定义logger的section必须指定level和handlers这两个option,level的可取值为DEBUG、INFO、WARNING、ERROR、CRITICAL、NOTSET,其中NOTSET表示所有级别的日志消息都要记录,包括用户定义级别;handlers的值是以逗号分隔的handler名字列表,这里出现的handler必须出现在[handlers]这个section中,并且相应的handler必须在配置文件中有对应的section定义; 4)对于非root logger来说,除了level和handlers这两个option之外,还需要一些额外的option,其中qualname是必须提供的option,它表示在logger层级中的名字,在应用代码中通过这个名字得到logger;propagate是可选项,其默认是为1,表示消息将会传递给高层次logger的handler,通常我们需要指定其值为0,这个可以看下下面的例子;另外,对于非root logger的level如果设置为NOTSET,系统将会查找高层次的logger来决定此logger的有效level。 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; 说明: 配置文件中的 3. 对于propagate属性的说明 实例1: 我们把logging.conf中simpleExample这个handler定义中的propagate属性值改为1,或者删除这个option(默认值就是1): [logger_simpleExample] level=DEBUG handlers=consoleHandler qualname=simpleExample propagate=1 现在来执行同样的代码:
# 读取日志配置文件内容
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')
我们会发现,除了在控制台有输出信息时候,在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('logging.conf')
# 用一个没有在配置文件中定义的logger名称来创建一个日志器logger
logger = logging.getLogger('simpleExample1')
# 日志输出
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
