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 不关闭控制台的实现方法
Oct 23 Python
python实现自动登录人人网并访问最近来访者实例
Sep 26 Python
python实现从ftp服务器下载文件的方法
Apr 30 Python
Python易忽视知识点小结
May 25 Python
浅谈tensorflow中几个随机函数的用法
Jul 27 Python
详解Numpy数组转置的三种方法T、transpose、swapaxes
May 27 Python
python使用opencv对图像mask处理的方法
Jul 05 Python
Django使用Jinja2模板引擎的示例代码
Aug 09 Python
为什么说Python可以实现所有的算法
Oct 04 Python
Python3 实现爬取网站下所有URL方式
Jan 16 Python
通过cmd进入python的步骤
Jun 16 Python
Python数据库封装实现代码示例解析
Sep 05 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
php环境无法上传文件的解决方法
2014/04/30 PHP
PHP临时文件的安全性分析
2014/07/04 PHP
jQuery 绑定事件到动态创建的元素上的方法实例
2013/08/18 Javascript
JavaScript获取路径设计源码
2014/05/22 Javascript
如何编写高质量JS代码(续)
2015/02/25 Javascript
JQuery实现超链接鼠标提示效果的方法
2015/06/10 Javascript
jQuery实现的手机发送验证码倒计时效果代码分享
2015/08/24 Javascript
JS中的forEach、$.each、map方法推荐
2016/04/05 Javascript
React Native实现进度条弹框的示例代码
2017/07/17 Javascript
AngularJS实现的省市二级联动功能示例【可对选项实现增删】
2017/10/26 Javascript
javascript少儿编程关于返回值的函数内容
2018/05/27 Javascript
详解vuex状态管理模式
2018/11/01 Javascript
NodeJs之word文件生成与解析的实现代码
2019/04/01 NodeJs
微信网页登录逻辑与实现方法
2019/04/29 Javascript
JS如何在数组指定位置插入元素
2020/03/10 Javascript
[34:47]完美世界DOTA2联赛PWL S2 Magma vs LBZS 第一场 11.18
2020/11/18 DOTA
Python实现多线程下载文件的代码实例
2014/06/01 Python
Python可跨平台实现获取按键的方法
2015/03/05 Python
python docx 中文字体设置的操作方法
2018/05/08 Python
python求最大连续子数组的和
2018/07/07 Python
PyQt5 多窗口连接实例
2019/06/19 Python
详解pandas删除缺失数据(pd.dropna()方法)
2019/06/25 Python
python实现对服务器脚本敏感信息的加密解密功能
2019/08/13 Python
python函数调用,循环,列表复制实例
2020/05/03 Python
浅谈keras中Dropout在预测过程中是否仍要起作用
2020/07/09 Python
python--shutil移动文件到另一个路径的操作
2020/07/13 Python
测绘工程个人的自我评价
2013/11/23 职场文书
理工类毕业自我鉴定
2014/02/20 职场文书
中药专业自荐信范文
2014/03/18 职场文书
公司授权委托书范本
2014/04/03 职场文书
施工安全标语
2014/06/07 职场文书
机械电子工程专业求职信
2014/06/22 职场文书
收入证明怎么写
2015/06/12 职场文书
人为什么会“幸灾乐祸”?
2019/08/06 职场文书
秀!学妹看见都惊呆的Python小招数!【详细语言特性使用技巧】
2021/04/27 Python
一次Mysql update sql不当引起的生产故障记录
2022/04/01 MySQL