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中的列表推导浅析
Apr 26 Python
Python合并字符串的3种方法
May 21 Python
python制作爬虫并将抓取结果保存到excel中
Apr 06 Python
浅谈pandas用groupby后对层级索引levels的处理方法
Nov 06 Python
解决pycharm py文件运行后停止按钮变成了灰色的问题
Nov 29 Python
对python For 循环的三种遍历方式解析
Feb 01 Python
python对矩阵进行转置的2种处理方法
Jul 17 Python
react+django清除浏览器缓存的几种方法小结
Jul 17 Python
Python爬取YY评级分数并保存数据实现过程解析
Jun 01 Python
pycharm专业版远程登录服务器的详细教程
Sep 15 Python
用Python远程登陆服务器的步骤
Apr 16 Python
用Python爬取某乎手机APP数据
Jun 15 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安全的URL字符串base64编码和解码
2014/06/19 PHP
PHP 对象继承原理与简单用法示例
2020/04/21 PHP
Apply an AutoFormat to an Excel Spreadsheet
2007/06/12 Javascript
jquery ready()的几种实现方法小结
2010/06/18 Javascript
使用jquery实现div的tab切换实例代码
2013/05/27 Javascript
javascript中Number对象的toString()方法分析
2014/12/20 Javascript
jQuery实现默认是闭合的FAQ展开效果菜单
2015/09/14 Javascript
jquery validate.js表单验证入门实例(附源码)
2015/11/10 Javascript
JavaScript学习笔记整理之引用类型
2016/01/22 Javascript
原生javascript实现匀速运动动画效果
2016/02/26 Javascript
js表单验证实例讲解
2016/03/31 Javascript
js canvas实现放大镜查看图片功能
2017/06/08 Javascript
移动端Ionic App 资讯上下循环滚动的实现代码(跑马灯效果)
2017/08/29 Javascript
vue-router实现tab标签页(单页面)详解
2017/10/17 Javascript
jQuery中图片展示插件highslide.js的简单dom
2018/04/22 jQuery
webpack4 入门最简单的例子介绍
2018/09/05 Javascript
Vue中Quill富文本编辑器的使用教程
2018/09/21 Javascript
[46:38]完美世界DOTA2联赛PWL S2 Magma vs PXG 第三场 11.28
2020/12/02 DOTA
对于Python异常处理慎用“except:pass”建议
2015/04/02 Python
Python使用ftplib实现简易FTP客户端的方法
2015/06/03 Python
使用Python的urllib2模块处理url和图片的技巧两则
2016/02/18 Python
python 通过字符串调用对象属性或方法的实例讲解
2018/04/21 Python
django+xadmin+djcelery实现后台管理定时任务
2018/08/14 Python
Python操作json的方法实例分析
2018/12/06 Python
Python中关于浮点数的冷知识
2019/09/22 Python
PyCharm汉化安装及永久激活详细教程(靠谱)
2020/01/16 Python
Python 实现将大图切片成小图,将小图组合成大图的例子
2020/03/14 Python
使用keras时input_shape的维度表示问题说明
2020/06/29 Python
Pycharm及python安装详细教程(图解)
2020/07/31 Python
python中pivot()函数基础知识点
2021/01/03 Python
阿迪达斯俄罗斯官方商城:adidas俄罗斯
2017/03/08 全球购物
初中生个人学习的自我评价
2013/12/04 职场文书
土建专业毕业生自荐书
2014/07/04 职场文书
《周恩来的四个昼夜》观后思想汇报范文两篇
2014/09/10 职场文书
二审代理词范文
2015/05/25 职场文书
决心书格式范文
2015/09/23 职场文书