S******y 发帖数: 1123 | 1 许多应用程序中都会有日志记录,用于记录系统在运行过程中的一些关键信息,以便于
对系统的运行状况进行跟踪。python为我们提供了简单易用、且功能强大的日志模块:
logging
The logging module defines a standard API for reporting errors and status
information from applications and libraries. The key benefit of having the
logging API provided by a standard library module is that all Python modules
can participate in logging, so an application’s log can include messages
from third-party modules.
logging模块支持将日志信息保存,如:保存到日志文件中;以邮件的形式发送日志信
息;以http get或post的方式提交日志到web服务器;等等。
举一个简单的例子:
import logging
logging.basicConfig(filename = os.path.join(os.getcwd(), 'log.txt'), level =
logging.DEBUG)
logging.debug('this is a message')
运行上面例子的代码,将会在程序的根目录下创建一个log.txt文件,打开该文件,里
面有一条日志记录:”DEBUG:root:this is a message”。
Verbosity Levels
Another useful feature of the logging API is the ability to produce
different messages at different log levels. This code to be instrumented
with debug messages, for example, while setting the log level down so that
those debug messages are not written on a production system.
--------------------
logger: 日志类,应用程序往往通过调用它提供的api来记录日志;
handler: 对日志信息处理,可以将日志发送(保存)到不同的目标域中;
filter: 对日志信息进行过滤;
formatter:日志的格式化;
-----------------------
详见 -
https://pymotw.com/2/logging/
http://victorlin.me/posts/2012/08/26/good-logging-practice-in-python |
|