Python日志模块logging简介
logging分为4个模块: loggers,handlers,filters,and formatters. ●loggers: 提供应用程序调用的接口 Logger Logger.setLevel() 设置日志级别 Handler setLevel() 设置日志级别 Formatter 默认形式为: %Y-%m-%d %H:%M:%S. 日志配置管理 硬编码形式 复制代码 代码如下: import logging # create logger # create console handler and set level to debug # create formatter # add formatter to ch # add ch to logger # 'application' code 输出 复制代码 代码如下: $ python simple_logging_module.py 2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message 2005-03-19 15:10:26,620 - simple_example - INFO - info message 2005-03-19 15:10:26,695 - simple_example - WARNING - warn message 2005-03-19 15:10:26,697 - simple_example - ERROR - error message 2005-03-19 15:10:26,773 - simple_example - CRITICAL - critical message 通过文件配置管理日志 代码: 复制代码 代码如下: import logging import logging.config logging.config.fileConfig('logging.conf') # create logger # 'application' code 配置文件: 复制代码 代码如下: [loggers] keys=root,simpleExample [handlers] [formatters] [logger_root] [logger_simpleExample] [handler_consoleHandler] [formatter_simpleFormatter] 输出: 复制代码 代码如下: $ python simple_logging_config.py 2005-03-19 15:38:55,977 - simpleExample - DEBUG - debug message 2005-03-19 15:38:55,979 - simpleExample - INFO - info message 2005-03-19 15:38:56,054 - simpleExample - WARNING - warn message 2005-03-19 15:38:56,055 - simpleExample - ERROR - error message 2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message 日志格式 %(levelno)s: 打印日志级别的数值 流程图 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |