python 通过logging写入日志到文件和控制台的实例


Posted in Python onApril 28, 2018

如下所示:

import logging 
 
# 创建一个logger 
logger = logging.getLogger('mylogger') 
logger.setLevel(logging.DEBUG) 
 
# 创建一个handler,用于写入日志文件 
fh = logging.FileHandler('test.log') 
fh.setLevel(logging.DEBUG) 
 
# 再创建一个handler,用于输出到控制台 
ch = logging.StreamHandler() 
ch.setLevel(logging.DEBUG) 
 
# 定义handler的输出格式 
formatter = logging.Formatter('[%(asctime)s][%(thread)d][%(filename)s][line: %(lineno)d][%(levelname)s] ## %(message)s')
fh.setFormatter(formatter) 
ch.setFormatter(formatter) 
 
# 给logger添加handler 
logger.addHandler(fh) 
logger.addHandler(ch) 
 
# 记录一条日志 
logger.info('foorbar')

关于formatter的配置,采用的是%(<dict key>)s的形式,就是字典的关键字替换。提供的关键字包括:

Format Description
%(name)s Name of the logger (logging channel).
%(levelno)s Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).
%(levelname)s Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL').
%(pathname)s Full pathname of the source file where the logging call was issued (if available).
%(filename)s Filename portion of pathname.
%(module)s Module (name portion of filename).
%(funcName)s Name of function containing the logging call.
%(lineno)d Source line number where the logging call was issued (if available).
%(created)f Time when the LogRecord was created (as returned by time.time()).
%(relativeCreated)d Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
%(asctime)s Human-readable time when the LogRecord was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time).
%(msecs)d Millisecond portion of the time when the LogRecord was created.
%(thread)d Thread ID (if available).
%(threadName)s Thread name (if available).
%(process)d Process ID (if available).
%(message)s The logged message, computed as msg % args.

以上这篇python 通过logging写入日志到文件和控制台的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用第三方库xlrd来写入Excel文件示例
Apr 05 Python
python append、extend与insert的区别
Oct 13 Python
解决PyCharm的Python.exe已经停止工作的问题
Nov 29 Python
python+pyqt5编写md5生成器
Mar 18 Python
python查询文件夹下excel的sheet名代码实例
Apr 02 Python
Python字符串对象实现原理详解
Jul 01 Python
Python 如何批量更新已安装的库
May 26 Python
pandas分批读取大数据集教程
Jun 06 Python
opencv 阈值分割的具体使用
Jul 08 Python
Python 通过正则表达式快速获取电影的下载地址
Aug 17 Python
如何利用python之wxpy模块玩转微信
Aug 17 Python
Python Opencv实现单目标检测的示例代码
Sep 08 Python
Python实现合并同一个文件夹下所有PDF文件的方法示例
Apr 28 #Python
用TensorFlow实现多类支持向量机的示例代码
Apr 28 #Python
详谈python在windows中的文件路径问题
Apr 28 #Python
TensorFlow实现随机训练和批量训练的方法
Apr 28 #Python
对python中的logger模块全面讲解
Apr 28 #Python
详解PyTorch批训练及优化器比较
Apr 28 #Python
Python使用matplotlib实现的图像读取、切割裁剪功能示例
Apr 28 #Python
You might like
CodeIgniter使用phpcms模板引擎
2013/11/12 PHP
php获取随机数组列表的方法
2014/11/13 PHP
php获取POST数据的三种方法实例详解
2016/12/20 PHP
thinkPHP5实现的查询数据库并返回json数据实例
2017/10/23 PHP
javascript 动态调整图片尺寸实现代码
2009/12/28 Javascript
JavaScript DOM 学习第三章 内容表格
2010/02/19 Javascript
JS模拟面向对象全解(一、类型及传递)
2011/07/13 Javascript
js动态在form上插入enctype=multipart/form-data的问题
2012/05/24 Javascript
js生成动态表格并为每个单元格添加单击事件的方法
2014/04/14 Javascript
js控制鼠标事件移动及移出效果显示
2014/10/19 Javascript
jQuery调用ajax请求的常见方法汇总
2015/03/24 Javascript
JavaScript控制输入框中只能输入中文、数字和英文的方法【基于正则实现】
2017/03/03 Javascript
JavaScript JSON数据处理全集(小结)
2019/08/15 Javascript
vue跳转页面的几种方法(推荐)
2020/03/26 Javascript
JS如何实现封装列表右滑动删除收藏按钮
2020/07/23 Javascript
[04:13]2014DOTA2国际邀请赛 专访DC目前形势不容乐观
2014/07/12 DOTA
[00:37]食人魔魔法师轮盘吉兆顺应全新至宝将拥有额外款式
2019/12/19 DOTA
python数字图像处理之骨架提取与分水岭算法
2018/04/27 Python
python使用Paramiko模块实现远程文件拷贝
2019/04/30 Python
python3发送邮件需要经过代理服务器的示例代码
2019/07/25 Python
详解如何使用CSS3中的结构伪类选择器和伪元素选择器
2020/01/06 HTML / CSS
英国在线药房:Chemist.co.uk
2019/03/26 全球购物
《雾凇》教学反思
2014/02/17 职场文书
党课知识竞赛主持词
2014/04/01 职场文书
小学班长竞选演讲稿
2014/04/24 职场文书
未受刑事制裁公证证明
2014/09/20 职场文书
群众路线四风对照检查材料
2014/11/04 职场文书
先进个人事迹材料范文
2014/12/30 职场文书
财务负责人岗位职责
2015/02/03 职场文书
2015年农村党员干部主题教育活动总结
2015/03/25 职场文书
企业办公室主任岗位职责
2015/04/01 职场文书
师德师风主题教育活动总结
2015/05/07 职场文书
消防安全月活动总结
2015/05/08 职场文书
2015年小学远程教育工作总结
2015/07/28 职场文书
团结友爱主题班会
2015/08/13 职场文书
幼儿体育课教学反思
2016/02/16 职场文书