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 相关文章推荐
仅利用30行Python代码来展示X算法
Apr 01 Python
详解Python中for循环的使用
Apr 14 Python
Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
Apr 24 Python
讲解Python中的标识运算符
May 14 Python
python中实现指定时间调用函数示例代码
Sep 08 Python
python自动裁剪图像代码分享
Nov 25 Python
python的dataframe和matrix的互换方法
Apr 11 Python
用Python实现筛选文件脚本的方法
Oct 27 Python
Django发送邮件和itsdangerous模块的配合使用解析
Aug 10 Python
python图像处理模块Pillow的学习详解
Oct 09 Python
序列化Python对象的方法
Aug 01 Python
python 将列表里的字典元素合并为一个字典实例
Sep 01 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查找字符串出现次数的方法
2014/12/01 PHP
PHP的邮件群发系统phplist配置方法详细总结
2016/03/30 PHP
IIS 7.5 asp Session超时时间设置方法
2017/04/17 PHP
php源码的使用方法讲解
2019/09/26 PHP
javascript 单选框,多选框美化代码
2008/08/01 Javascript
jQuery 剧场版 你必须知道的javascript
2009/05/27 Javascript
用JavaScript实现一个代码简洁、逻辑不复杂的多级树
2014/05/23 Javascript
js,jquery滚动/跳转页面到指定位置的实现思路
2014/06/03 Javascript
PHP实现的各种中文编码转换类分享
2015/01/23 Javascript
BootStrap Typeahead自动补全插件实例代码
2016/08/10 Javascript
webpack+vue.js实现组件化详解
2016/10/12 Javascript
微信小程序如何获知用户运行小程序的场景教程
2017/05/17 Javascript
ionic2中使用自动生成器的方法
2018/03/04 Javascript
在Vue项目中取消ESLint代码检测的步骤讲解
2019/01/27 Javascript
Vue入门学习笔记【基本概念、对象、过滤器、指令等】
2019/04/13 Javascript
Vue v-for循环之@click点击事件获取元素示例
2019/11/09 Javascript
[40:29]2018DOTA2亚洲邀请赛 4.7总决赛 LGD vs Mineski 第一场
2018/04/10 DOTA
[03:18]【TI9纪实】社区大触GL与木木
2019/08/25 DOTA
python查找指定具有相同内容文件的方法
2015/06/28 Python
Django框架搭建的简易图书信息网站案例
2019/05/25 Python
Python数组并集交集补集代码实例
2020/02/18 Python
Python3压缩和解压缩实现代码
2021/03/01 Python
使用canvas实现黑客帝国数字雨效果
2020/01/02 HTML / CSS
Joe Fresh官网:加拿大时尚品牌和零售连锁店
2016/11/30 全球购物
澳大利亚拥有最好的家具和家居用品在线目的地:Nestz
2019/02/23 全球购物
大学在校生求职信范文
2013/11/21 职场文书
信访工作者先进事迹
2014/01/17 职场文书
领班岗位职责范文
2014/02/06 职场文书
倡议书格式范文
2014/04/14 职场文书
学生会竞选演讲稿学习部
2014/08/25 职场文书
单身证明范本
2015/06/15 职场文书
2015年小学财务工作总结
2015/07/20 职场文书
安全事故隐患排查治理制度
2015/08/05 职场文书
2016道德模范先进事迹材料
2016/02/26 职场文书
Python 如何实现文件自动去重
2021/06/02 Python
纯CSS如何禁止用户复制网页的内容
2021/11/01 HTML / CSS