python logging文件配置示例

简介: python logging模块用来打印日志,类似于Java的log4j,下面列出logging模块文件配置示例代码 配置文件“logging.

python logging模块用来打印日志,类似于Java的log4j,下面列出logging模块文件配置示例代码
配置文件“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')
目录
相关文章
|
4天前
|
前端开发 Python
使用Python+openpyxl实现导出自定义样式的Excel文件
本文介绍了如何使用Python的openpyxl库导出具有自定义样式的Excel文件,包括设置字体、对齐方式、行列宽高、边框和填充等样式,并提供了完整的示例代码和运行效果截图。
8 1
使用Python+openpyxl实现导出自定义样式的Excel文件
|
6天前
|
Python
Python——批量将PDF文件转为图片
Python——批量将PDF文件转为图片
16 2
|
6天前
|
Python
Python——将PPT和Word转为PDF文件
Python——将PPT和Word转为PDF文件
22 1
|
3天前
|
运维 网络架构 Python
利用Python查询H3C网络设备示例,运维用了它,都称赞!
利用Python查询H3C网络设备示例,运维用了它,都称赞!
|
4天前
|
Shell 网络安全 Python
网络工程师如何在ensp模拟器上玩python自动化配置交换机。
网络工程师如何在ensp模拟器上玩python自动化配置交换机。
|
6天前
|
运维 监控 大数据
深入探讨网络自动化的魅力所在,以及如何利用Python这一强大工具,实现网络设备的批量配置与监控
在信息洪流的浪潮中,网络自动化如同一盏明灯,引领着我们穿越复杂网络管理的迷雾。它不仅简化了网络运维人员的工作,更是在大数据、云计算等技术飞速发展的背景下,成为了构建高效、稳定网络环境的关键。本文将深入探讨网络自动化的魅力所在,以及如何利用Python这一强大工具,实现网络设备的批量配置与监控,以此展现网络自动化在简化复杂网络管理中的重要作用。
18 0
|
6天前
|
Python
MPC - Python、MATLAB、CVXPY、YALMIP、Julia 示例
MPC - Python、MATLAB、CVXPY、YALMIP、Julia 示例
13 0
|
6天前
|
XML 监控 数据格式
ROS 2 - Python、XML 和 YAML 编写 Launch 文件
ROS 2 - Python、XML 和 YAML 编写 Launch 文件
17 0
|
6天前
|
存储 开发者 Python
六种方法实现Python文件之间的互动
六种方法实现Python文件之间的互动
7 0
|
2月前
|
存储 Python 内存技术
python WAV音频文件处理—— (1)读写WAV文件
python WAV音频文件处理—— (1)读写WAV文件
78 14