logging

这个模块定义实现灵活的事件记录系统的应用程序和库类的功能。有一个标准库模块API提供的记录关键的好处是,所有的Python模块可以参与记录,所以你的应用程序日志可以包括您自己的信息,从第三方模块信息集成。该模块提供了很多的功能和灵活性。如果你不熟悉logging,掌握它的最好的方式是看教程。在模块中定义的基本类型,以及它们的功能,如下。

loggers暴露直接应用程序代码中使用的接口。

handlers发送日志记录到适当的目的地。

Filter提供更细输出日志记录。

Formatters在最终输出指定的日志记录的布局

logging基础

logging是一些软件时运行发生一种跟踪事件。当某些事件发生时该软件的开发者添加记录调用他们的代码表明。一个事件是由一个描述性的信息,可以选择包含可变数据描述(即数据,每次发生的事件可能不同)

日志文件

一个非常常见的情况是在一个记录logging事件,可以参考下面的例子(注:确定是在一个新的python解释器中,不要在上一个会话中继续)


importlogginglogging.basicConfig(filename='example.log',level=logging.DEBUG)

logging.debug('This message should go to the log file')

logging.info('So should this')

logging.warning('And this, too')

在消息中显示日期时间

为了在一个事件中显示时间,要在format中加入‘%(asctime)s’ :

import logging
logging.basicConfig(format='%(asctime)s %(message)s')
logging.warning('is when this event was logged.')
which should print something like this:

记入的logging如下所示

2010-12-12 11:41:42,612 is when this event was logged.

默认显示时间的形式是ISO8601,如果你需要更多的日期和时间的形式,basicConfig提供一个datafmt的参数,示例如下:

import logginglogging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')logging.warning('is when this event was logged.')

log中会写入类似下面的东西:

12/12/2010 11:46:36 AM is when this event was logged.

dateFmt参数

The format of the datefmt argument is the same as supported by time.strftime()
.

你可能感兴趣的:(logging)