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相似模块用例
Mar 04 Python
Python生成器定义与简单用法实例分析
Apr 30 Python
把JSON数据格式转换为Python的类对象方法详解(两种方法)
Jun 04 Python
关于jupyter打开之后不能直接跳转到浏览器的解决方式
Apr 13 Python
Python数据正态性检验实现过程
Apr 18 Python
基于tf.shape(tensor)和tensor.shape()的区别说明
Jun 30 Python
python给视频添加背景音乐并改变音量的具体方法
Jul 19 Python
使用python-cv2实现Harr+Adaboost人脸识别的示例
Oct 27 Python
用pushplus+python监控亚马逊到货动态推送微信
Jan 29 Python
python切片作为占位符使用实例讲解
Feb 17 Python
Django migrate报错的解决方案
May 20 Python
python垃圾回收机制原理分析
Apr 13 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调用shell的方法
2014/11/05 PHP
PHP判断JSON对象是否存在的方法(推荐)
2016/07/06 PHP
关于Yii2框架跑脚本时内存泄漏问题的分析与解决
2019/12/01 PHP
PHP实现微信公众号验证Token的示例代码
2019/12/16 PHP
JS在textarea光标处插入文本的小例子
2013/03/22 Javascript
Nodejs关于gzip/deflate压缩详解
2015/03/04 NodeJs
详解JavaScript表单验证(E-mail 验证)
2016/03/31 Javascript
一些实用性较高的js方法
2016/04/19 Javascript
jQuery实现响应鼠标事件的图片透明效果【附demo源码下载】
2016/06/16 Javascript
JS仿京东移动端手指拨动切换轮播图效果
2020/04/10 Javascript
简单谈谈Javascript函数中的arguments
2017/02/09 Javascript
ES6中箭头函数的定义与调用方式详解
2017/06/02 Javascript
JavaScript实现修改伪类样式
2017/11/27 Javascript
JavaScript数组,JSON对象实现动态添加、修改、删除功能示例
2018/05/26 Javascript
js replace 全局替换的操作方法
2018/06/12 Javascript
node.js中express模块创建服务器和http模块客户端发请求
2019/03/06 Javascript
es6 filter() 数组过滤方法总结
2019/04/03 Javascript
通过实例解析vuejs如何实现调试代码
2020/07/16 Javascript
用vue写一个日历
2020/11/02 Javascript
python字符串中的单双引
2017/02/16 Python
解决python通过cx_Oracle模块连接Oracle乱码的问题
2018/10/18 Python
python使用writerows写csv文件产生多余空行的处理方法
2019/08/01 Python
Python类中self参数用法详解
2020/02/13 Python
图解Python中深浅copy(通俗易懂)
2020/09/03 Python
Pycharm安装Qt Design快捷工具的详细教程
2020/11/18 Python
澳洲本土太阳镜品牌:Quay Australia
2019/07/29 全球购物
Tuckernuck官网:经典的美国品质服装、鞋子和配饰
2021/01/11 全球购物
国外软件测试工程师面试题
2016/12/09 面试题
12月小学生校园广播稿
2014/02/04 职场文书
《姥姥的剪纸》教学反思
2014/02/25 职场文书
房屋公证委托书
2014/04/03 职场文书
大学生求职信范文
2014/05/24 职场文书
2014年生产管理工作总结
2014/12/23 职场文书
谢师宴学生答谢词
2015/09/30 职场文书
2016年教师节特级教师获奖感言
2015/12/09 职场文书
java高级用法JNA强大的Memory和Pointer
2022/04/19 Java/Android