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实现的百度站长自动URL提交小工具
Jun 27 Python
Python的randrange()方法使用教程
May 15 Python
使用python绘制常用的图表
Aug 27 Python
python机器学习库常用汇总
Nov 15 Python
Python反射的用法实例分析
Feb 11 Python
python 3.7.0 安装配置方法图文教程
Aug 27 Python
python3.8与pyinstaller冲突问题的快速解决方法
Jan 16 Python
python无序链表删除重复项的方法
Jan 17 Python
在pytorch 中计算精度、回归率、F1 score等指标的实例
Jan 18 Python
Python chardet库识别编码原理解析
Feb 18 Python
PyTorch中Tensor的数据类型和运算的使用
Sep 03 Python
Python机器学习之逻辑回归
May 11 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下封装较好的数字分页方法
2010/11/23 PHP
php编写批量生成不重复的卡号密码代码
2015/05/14 PHP
DEDECMS首页调用图片集里的多张图片
2015/06/05 PHP
YII中Ueditor富文本编辑器文件和图片上传的配置图文教程
2017/03/15 PHP
PHP使用星号替代用户名手机和邮箱的实现代码
2018/02/07 PHP
PHP与Web页面的交互示例详解一
2020/08/04 PHP
几个有趣的Javascript Hack
2010/07/24 Javascript
myEvent.js javascript跨浏览器事件框架
2011/10/24 Javascript
a标签click和href执行顺序探讨
2014/06/23 Javascript
JS实现网页游戏中滑块响应鼠标点击移动效果
2015/10/19 Javascript
浅谈JS正则表达式的RegExp对象和括号的使用
2016/07/28 Javascript
bootstrap daterangepicker汉化以及扩展功能
2017/06/15 Javascript
详解微信第三方小程序代开发
2017/06/23 Javascript
用js实现每隔一秒刷新时间的实例(含年月日时分秒)
2017/10/25 Javascript
JavaScript交换两个变量方法实例
2019/11/25 Javascript
[03:40]DOTA2抗疫特别篇《英雄年代》
2020/02/28 DOTA
用Python的SimPy库简化复杂的编程模型的介绍
2015/04/13 Python
基于python实现微信模板消息
2015/12/21 Python
基于Python函数的作用域规则和闭包(详解)
2017/11/29 Python
Django教程笔记之中间件middleware详解
2018/08/01 Python
浅谈Pandas Series 和 Numpy array中的相同点
2019/06/28 Python
Python中字符串List按照长度排序
2019/07/01 Python
PIL图像处理模块paste方法简单使用详解
2019/07/17 Python
在SQLite-Python中实现返回、查询中文字段的方法
2019/07/17 Python
python+tkinter实现学生管理系统
2019/08/20 Python
python爬虫爬取幽默笑话网站
2019/10/24 Python
Python 实现递归法解决迷宫问题的示例代码
2020/01/12 Python
浅谈python 中的 type(), dtype(), astype()的区别
2020/04/09 Python
解决Python3.7.0 SSL低版本导致Pip无法使用问题
2020/09/03 Python
瑞典廉价机票预订网站:Seat24
2018/06/19 全球购物
The North Face官方旗舰店:美国著名户外品牌
2020/09/28 全球购物
行政助理岗位职责范文
2013/12/03 职场文书
担保书怎么写
2014/04/01 职场文书
大专毕业生求职信
2014/07/05 职场文书
2015年银行信贷员工作总结
2015/05/19 职场文书
MySQL8.0的WITH查询详情
2021/08/30 MySQL