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 while、for、生成器、列表推导等语句的执行效率测试
Jun 03 Python
TensorFlow实现AutoEncoder自编码器
Mar 09 Python
Python排序算法之选择排序定义与用法示例
Apr 29 Python
Python3基于sax解析xml操作示例
May 22 Python
pycharm在调试python时执行其他语句的方法
Nov 29 Python
Django 表单模型选择框如何使用分组
May 16 Python
django框架forms组件用法实例详解
Dec 10 Python
Pytorch之parameters的使用
Dec 31 Python
使用Pycharm在运行过程中,查看每个变量的操作(show variables)
Jun 08 Python
Python中如何添加自定义模块
Jun 09 Python
Python深度学习之实现卷积神经网络
Jun 05 Python
python实现Nao机器人的单目测距
Sep 04 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文件
2007/01/04 PHP
基于empty函数的判断详解
2013/06/17 PHP
PHP实现邮件群发的源码
2013/06/18 PHP
PHP编程中尝试程序并发的几种方式总结
2016/03/21 PHP
解决 FireFox 下[使用event很麻烦] 的问题.
2006/08/22 Javascript
修改jQuery Validation里默认的验证方法
2012/02/14 Javascript
node.js中的fs.linkSync方法使用说明
2014/12/15 Javascript
JavaScript控制按钮可用或不可用的方法
2015/04/03 Javascript
Spring mvc 接收json对象
2015/12/10 Javascript
详解javascript高级定时器
2015/12/31 Javascript
javascript设计模式之module(模块)模式
2016/08/19 Javascript
基于Bootstrap 3 JQuery及RegExp的表单验证功能
2017/02/16 Javascript
JavaScript数组去重的多种方法(四种)
2017/09/19 Javascript
Javascript 编码约定(编码规范)
2018/03/11 Javascript
Vue2.0 事件的广播与接收(观察者模式)
2018/03/14 Javascript
JS实现的DOM插入节点操作示例
2018/04/04 Javascript
bootstrap下拉分页样式 带跳转页码
2018/12/29 Javascript
React 父子组件通信的实现方法
2019/12/05 Javascript
介绍Python的@property装饰器的用法
2015/04/28 Python
Python获取二维矩阵每列最大值的方法
2018/04/03 Python
在SQLite-Python中实现返回、查询中文字段的方法
2019/07/17 Python
Python如何定义接口和抽象类
2020/07/28 Python
Python字符串三种格式化输出
2020/09/17 Python
使用pandas读取表格数据并进行单行数据拼接的详细教程
2021/03/03 Python
美特斯邦威官方商城:邦购网
2016/10/13 全球购物
Sofmap官网:日本著名的数码电器专卖店
2017/05/19 全球购物
Foot Locker澳洲官网:美国运动服和鞋类零售商
2019/10/11 全球购物
俄罗斯女装店:12storeez
2019/10/25 全球购物
世界上最好的野生海鲜和有机食品:Vital Choice
2020/01/16 全球购物
大学生个人求职口试自我评价
2014/02/16 职场文书
公司总经理工作职责管理办法
2014/02/28 职场文书
服务标语大全
2014/06/18 职场文书
工作保证书怎么写
2015/02/28 职场文书
2015年幼儿园德育工作总结
2015/05/25 职场文书
深入理解java.lang.String类的不可变性
2021/06/27 Java/Android
使用 CSS 构建强大且酷炫的粒子动画效果
2022/08/14 HTML / CSS