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 相关文章推荐
仅用50行代码实现一个Python编写的计算器的教程
Apr 17 Python
python中Apriori算法实现讲解
Dec 10 Python
Python数据抓取爬虫代理防封IP方法
Dec 23 Python
python 多个参数不为空校验方法
Feb 14 Python
python使用MQTT给硬件传输图片的实现方法
May 05 Python
python中文分词库jieba使用方法详解
Feb 11 Python
学习python需要有编程基础吗
Jun 02 Python
详解numpy.ndarray.reshape()函数的参数问题
Oct 13 Python
Python非单向递归函数如何返回全部结果
Dec 18 Python
查找适用于matplotlib的中文字体名称与实际文件名对应关系的方法
Jan 05 Python
Python中正则表达式对单个字符,多个字符和匹配边界等使用
Jan 27 Python
Python实现灰色关联分析与结果可视化的详细代码
Mar 25 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/09/24 PHP
PHP数组对比函数,存在交集则返回真,否则返回假
2011/02/03 PHP
PHP判断用户是否已经登录(跳转到不同页面或者执行不同动作)
2016/09/22 PHP
Maps Javascript
2007/01/22 Javascript
向fckeditor编辑器插入指定代码的方法
2007/05/25 Javascript
各浏览器对document.getElementById等方法的实现差异解析
2013/12/05 Javascript
js对table的td进行相同内容合并示例详解
2013/12/27 Javascript
JavaScript控制各种浏览器全屏模式的方法、属性和事件介绍
2014/04/03 Javascript
bootstrap modal弹出框的垂直居中
2016/12/14 Javascript
jQuery插件echarts设置折线图中折线线条颜色和折线点颜色的方法
2017/03/03 Javascript
用最简单的方法判断JavaScript中this的指向(推荐)
2017/09/04 Javascript
node.js 利用流实现读写同步,边读边写的方法
2017/09/11 Javascript
Layui事件监听的实现(表单和数据表格)
2019/10/17 Javascript
JavaScript计算正方形面积
2019/11/26 Javascript
小程序卡片切换效果组件wxCardSwiper的实现
2020/02/13 Javascript
用JS实现选项卡
2020/03/23 Javascript
[03:02]辉夜杯主赛事第二日 每日之星
2015/12/27 DOTA
Python导出DBF文件到Excel的方法
2015/07/25 Python
Python 中pandas.read_excel详细介绍
2017/06/23 Python
Python引用传值概念与用法实例小结
2017/10/07 Python
从CentOS安装完成到生成词云python的实例
2017/12/01 Python
TensorFlow实现MLP多层感知机模型
2018/03/09 Python
在python3中实现更新界面
2020/02/21 Python
html5模拟平抛运动(模拟小球平抛运动过程)
2013/07/25 HTML / CSS
印度领先的在线时尚商店:Koovs
2016/08/28 全球购物
床上用品全球在线购物:BeddingInn
2016/12/18 全球购物
英国布鲁姆精品店:Bloom Boutique
2018/03/01 全球购物
十佳大学生村官事迹
2014/01/09 职场文书
书香校园活动方案
2014/02/28 职场文书
协议书模板
2014/04/23 职场文书
授权委托书格式
2014/07/31 职场文书
银行员工考核评语
2014/12/31 职场文书
七年级地理教学计划
2015/01/22 职场文书
学期个人自我总结
2015/02/13 职场文书
2015新生加入学生会自荐书
2015/03/24 职场文书
浅谈Python中的函数(def)及参数传递操作
2021/05/25 Python