Python日志模块logging简介


Posted in Python onApril 13, 2015

logging分为4个模块: loggers, handlers, filters, and formatters.

●loggers: 提供应用程序调用的接口
●handlers: 把日志发送到指定的位置
●filters: 过滤日志信息
●formatters: 格式化输出日志

Logger

Logger.setLevel() 设置日志级别
Logger.addHandler()和Logger.removeHandler() 增加和删除日志处理器
Logger.addFilter()和Logger.removeFilter() 增加和删除过滤器
Logger.debug(), Logger.info(), Logger.warning(), Logger.error(), and Logger.critical() 创建不同的级别的日志
getLogger() 获取日志的根实例

Handler

setLevel() 设置日志级别
setFormatter() 设置输出格式
addFilter() and removeFilter() 增加和删除过滤器

Formatter

默认形式为: %Y-%m-%d %H:%M:%S.
格式为: %()s

日志配置管理

硬编码形式

import logging
# create logger

logger = logging.getLogger('simple_example')

logger.setLevel(logging.DEBUG)
# create console handler and set level to debug

ch = logging.StreamHandler()

ch.setLevel(logging.DEBUG)
# create formatter

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch

ch.setFormatter(formatter)
# add ch to logger

logger.addHandler(ch)
# 'application' code

logger.debug('debug message')

logger.info('info message')

logger.warn('warn message')

logger.error('error message')

logger.critical('critical message')

输出
$ 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

logger = logging.getLogger('simpleExample')
# 'application' code

logger.debug('debug message')

logger.info('info message')

logger.warn('warn message')

logger.error('error message')

logger.critical('critical message')

配置文件:
[loggers]

keys=root,simpleExample
[handlers]

keys=consoleHandler
[formatters]

keys=simpleFormatter
[logger_root]

level=DEBUG

handlers=consoleHandler
[logger_simpleExample]

level=DEBUG

handlers=consoleHandler

qualname=simpleExample

propagate=0
[handler_consoleHandler]

class=StreamHandler

level=DEBUG

formatter=simpleFormatter

args=(sys.stdout,)
[formatter_simpleFormatter]

format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

datefmt=

输出:
$ 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: 打印日志级别的数值
%(levelname)s: 打印日志级别名称
%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s: 打印当前执行程序名
%(funcName)s: 打印日志的当前函数
%(lineno)d: 打印日志的当前行号
%(asctime)s: 打印日志的时间
%(thread)d: 打印线程ID
%(threadName)s: 打印线程名称
%(process)d: 打印进程ID
%(message)s: 打印日志信息

流程图

Python日志模块logging简介

Python 相关文章推荐
Python实例之wxpython中Frame使用方法
Jun 09 Python
Python中的map()函数和reduce()函数的用法
Apr 27 Python
Python3安装Scrapy的方法步骤
Nov 23 Python
python队列queue模块详解
Apr 27 Python
Python 数据可视化pyecharts的使用详解
Jun 26 Python
如何利用Pyecharts可视化微信好友
Jul 04 Python
python调用其他文件函数或类的示例
Jul 16 Python
Django工程的分层结构详解
Jul 18 Python
python实现把二维列表变为一维列表的方法分析
Oct 08 Python
Python实现画图软件功能方法详解
Jul 28 Python
python爬虫线程池案例详解(梨视频短视频爬取)
Feb 20 Python
python 经纬度求两点距离、三点面积操作
Jun 03 Python
Python命令行参数解析模块optparse使用实例
Apr 13 #Python
初步介绍Python中的pydoc模块和distutils模块
Apr 13 #Python
Python随机生成数模块random使用实例
Apr 13 #Python
Python字典操作简明总结
Apr 13 #Python
Python单元测试框架unittest使用方法讲解
Apr 13 #Python
Python配置文件解析模块ConfigParser使用实例
Apr 13 #Python
深入Python函数编程的一些特性
Apr 13 #Python
You might like
PHP与javascript对多项选择的处理
2006/10/09 PHP
PHP字符编码问题之GB2312 VS UTF-8解决方法
2011/06/23 PHP
php的ajax简单实例
2014/02/27 PHP
php版微信公众平台接口开发之智能回复开发教程
2016/09/22 PHP
Laravel-添加后台模板AdminLte的实现方法
2019/10/08 PHP
node.js中的events.emitter.removeAllListeners方法使用说明
2014/12/10 Javascript
js实现网站最上边可关闭的浮动广告条代码
2015/09/04 Javascript
checkbox 选中一个另一个checkbox也会选中的实现代码
2016/07/09 Javascript
基于JS实现仿百度百家主页的轮播图效果
2017/03/06 Javascript
使用BootStrap实现标签切换原理解析
2017/03/14 Javascript
JavaScript限定范围拖拽及自定义滚动条应用(3)
2017/05/17 Javascript
javascript checkbox/radio onchange不能兼容ie8处理办法
2017/06/13 Javascript
node.js支持多用户web终端实现及安全方案
2017/11/29 Javascript
基于Vue中点击组件外关闭组件的实现方法
2018/03/06 Javascript
Vue 中mixin 的用法详解
2018/04/23 Javascript
vue下拉菜单组件(含搜索)的实现代码
2018/11/25 Javascript
OpenLayers3实现测量功能
2020/09/25 Javascript
Python 3.x 安装opencv+opencv_contrib的操作方法
2018/04/02 Python
Python线程之定位与销毁的实现
2019/02/17 Python
python实现QQ空间自动点赞功能
2019/04/09 Python
使用python socket分发大文件的实现方法
2019/07/08 Python
用Python实现二叉树、二叉树非递归遍历及绘制的例子
2019/08/09 Python
解决python脚本中error: unrecognized arguments: True错误
2020/04/20 Python
CSS3制作ajax loader icon实现思路及代码
2013/08/25 HTML / CSS
解决img标签上下出现间隙的方法
2016/12/14 HTML / CSS
狗狗玩具、零食和咀嚼物的月度送货服务:Super Chewer
2018/08/22 全球购物
June Jacobs尊积帕官网:知名的spa水疗护肤品牌
2019/03/21 全球购物
CSS实现fullpage.js全屏滚动效果的示例代码
2021/03/24 HTML / CSS
实习生体会的自我评价范文
2013/11/28 职场文书
函授毕业自我鉴定
2014/02/04 职场文书
大学第二课堂活动总结
2014/07/08 职场文书
毕业设计指导教师评语
2014/12/30 职场文书
市场总监岗位职责
2015/02/11 职场文书
2015年医院工作总结范文
2015/04/09 职场文书
教师节倡议书2015
2015/04/27 职场文书
深入讲解Vue中父子组件通信与事件触发
2022/03/22 Vue.js