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 yield 使用方法浅析
May 20 Python
Python实现向服务器请求压缩数据及解压缩数据的方法示例
Jun 09 Python
python对配置文件.ini进行增删改查操作的方法示例
Jul 28 Python
Python爬虫实例扒取2345天气预报
Mar 04 Python
Python实现的KMeans聚类算法实例分析
Dec 29 Python
python用opencv批量截取图像指定区域的方法
Jan 24 Python
对Python3中列表乘以某一个数的示例详解
Jul 20 Python
opencv3/C++实现视频背景去除建模(BSM)
Dec 11 Python
Python selenium爬虫实现定时任务过程解析
Jun 08 Python
在django中实现choices字段获取对应字段值
Jul 12 Python
Python操作Word批量生成合同的实现示例
Aug 28 Python
python自动化操作之动态验证码、滑动验证码的降噪和识别
Aug 30 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单例模式应用详解
2013/06/03 PHP
基于MySQL到MongoDB简易对照表的详解
2013/06/03 PHP
php实现用于验证所有类型的信用卡类
2015/03/24 PHP
Linux基于php-fpm模式的lamp搭建phpmyadmin的方法
2018/10/25 PHP
js 禁用浏览器的后退功能的简单方法
2008/12/10 Javascript
基于JQuery框架的AJAX实例代码
2009/11/03 Javascript
jquery实现弹出窗口效果的实例代码
2013/11/28 Javascript
JS实现兼容性好,带缓冲的动感网页右键菜单效果
2015/09/18 Javascript
jQuery Easyui加载表格出错时在表格中间显示自定义的提示内容
2016/12/08 Javascript
vue2.0嵌套路由实现豆瓣电影分页功能(附demo)
2017/03/13 Javascript
深入理解Commonjs规范及Node模块实现
2017/05/17 Javascript
JavaScript之生成器_动力节点Java学院整理
2017/06/30 Javascript
Node.js 基础教程之全局对象
2017/08/06 Javascript
node.js支持多用户web终端实现及安全方案
2017/11/29 Javascript
node基于puppeteer模拟登录抓取页面的实现
2018/05/09 Javascript
Vue表单demo v-model双向绑定问题
2018/06/29 Javascript
JS与SQL方式随机生成高强度密码示例
2018/12/29 Javascript
JavaScript实现动态添加、移除元素或属性的方法分析
2019/01/03 Javascript
扫微信小程序码实现网站登陆实现解析
2019/08/20 Javascript
TensorFlow安装及jupyter notebook配置方法
2017/09/08 Python
使用Django启动命令行及执行脚本的方法
2018/05/29 Python
Python3实现转换Image图片格式
2018/06/21 Python
Flask框架各种常见装饰器示例
2018/07/17 Python
python实现换位加密算法的示例
2018/10/14 Python
记一次python 内存泄漏问题及解决过程
2018/11/29 Python
kafka监控获取指定topic的消息总量示例
2019/12/23 Python
Python实现文件压缩和解压的示例代码
2020/08/12 Python
大学生求职工作的自我评价
2014/02/13 职场文书
大学应届生的自我评价
2014/03/06 职场文书
教师年度考核评语
2014/04/28 职场文书
趣味运动会开幕词
2015/01/28 职场文书
幼儿园辞职信范文
2015/02/27 职场文书
刑事附带民事诉讼答辩状
2015/05/22 职场文书
电影小兵张嘎观后感
2015/06/03 职场文书
nginx里的rewrite跳转的实现
2021/03/31 Servers
Django操作cookie的实现
2021/05/26 Python