python logging文件配置示例
2017-05-18 17:57
141 查看
python logging模块用来打印日志,类似于Java的log4j,下面列出logging模块文件配置示例代码
配置文件“logging.conf”
上述“logging.conf”文件与代码文件处于同级目录
示例代码
配置文件“logging.conf”
# logging.conf [loggers] # 打印对象, keys指定不同对象的调用时的名称 keys=root, debug, info, error, file [logger_root] level=DEBUG # 指定级别, 错误、调试、信息等 handlers=debughandler, infohandler, errorhandler, filehandler [logger_debug] handlers=debughandler qualname=debug propagate=0 [logger_info] handlers=infohandler qualname=info propagate=0 [logger_error] handlers=errorhandler qualname=error propagate=0 [logger_file] # 同时输出到文件与控制台的日志对象 handlers=debughandler, filehandler qualname=debug propagate=0 ############################################### [handlers] # handler用于指定日志输出位置, 可以使控制台、文件等 keys=debughandler, infohandler, errorhandler, filehandler [handler_debughandler] class=StreamHandler level=DEBUG formatter=form01 args=(sys.stdout,) # 输出到控制台 [handler_infohandler] class=StreamHandler level=INFO formatter=form01 args=(sys.stdout,) [handler_errorhandler] class=FileHandler level=ERROR formatter=form02 args=('logging.log', 'a') # 输出到文件,追加模式,每次不重写文件 [handler_filehandler] class=FileHandler level=DEBUG formatter=form02 # 选择输出格式化对象 args=('logging.log', 'w') # 输出到文件,每次重写文件 ############################################### [formatters] # 指定输出格式,可以设置多种格式,每种输出位置选定不同输出格式 keys=form01, form02 [formatter_form01] # 格式: 时间 文件名 代码行数 输出级别 - 输出信息 format=%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s - %(message)s datefmt=%Y-%m-%d %H:%M:%S [formatter_form02] format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s - %(message)s datefmt=%Y-%m-%d %H:%M:%S
上述“logging.conf”文件与代码文件处于同级目录
示例代码
#! /usr/bin/env python # -*- coding: utf-8 -*- """ @time: 2017/5/12 9:52 @description: TODO """ import logging from logging.config import fileConfig fileConfig('logging.conf') logger = logging.getLogger('file') # 同时输出到文件与控制台 # logger = logging.getLogger('debug') # 输出到控制台 logger.debug('test')
相关文章推荐
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例
- python logging文件配置示例