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基于scrapy采集数据时使用代理服务器的方法
Apr 16 Python
python异常和文件处理机制详解
Jul 19 Python
简单谈谈Python中函数的可变参数
Sep 02 Python
python 实现红包随机生成算法的简单实例
Jan 04 Python
python中yaml配置文件模块的使用详解
Apr 27 Python
详解python里的命名规范
Jul 16 Python
python 处理string到hex脚本的方法
Oct 26 Python
值得收藏,Python 开发中的高级技巧
Nov 23 Python
pandas.cut具体使用总结
Jun 24 Python
浅析python redis的连接及相关操作
Nov 07 Python
Python数据可视化之用Matplotlib绘制常用图形
Jun 03 Python
拒绝盗图!教你怎么用python给图片加水印
Jun 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中的常用魔术方法总结
2013/08/02 PHP
PHP正则替换函数preg_replace和preg_replace_callback使用总结
2014/09/22 PHP
php版微信公众平台开发之验证步骤实例详解
2016/09/23 PHP
javascript document.images实例
2008/05/27 Javascript
javascript中window.event事件用法详解
2012/12/11 Javascript
asp.net刷新本页面的六种方法总结
2014/01/07 Javascript
JavaScript设计模式之抽象工厂模式介绍
2014/12/28 Javascript
jQuery插件slicebox实现3D动画图片轮播切换特效
2015/04/12 Javascript
JavaScript实现标题栏文字轮播效果代码
2015/10/24 Javascript
深入分析node.js的异步API和其局限性
2016/09/05 Javascript
layui-table对返回的数据进行转变显示的实例
2019/09/04 Javascript
JavaScript实现图片放大预览效果
2020/11/02 Javascript
[54:26]完美世界DOTA2联赛PWL S3 Forest vs Rebirth 第一场 12.10
2020/12/12 DOTA
python开发环境PyScripter中文乱码问题解决方案
2016/09/11 Python
Python使用爬虫爬取静态网页图片的方法详解
2018/06/05 Python
Tensorflow 同时载入多个模型的实例讲解
2018/07/27 Python
django框架forms组件用法实例详解
2019/12/10 Python
Python调用钉钉自定义机器人的实现
2020/01/03 Python
python 实现多维数组(array)排序
2020/02/28 Python
Python bisect模块原理及常见实例
2020/06/17 Python
移动端html5模拟长按事件的实现方法
2018/09/30 HTML / CSS
送给他或她的礼物:FUN.com
2018/08/17 全球购物
香港中原电器网上商店:Chung Yuen
2019/06/26 全球购物
PHP开发工程师面试问题集锦
2012/11/01 面试题
开朗女孩的自我评价
2014/02/10 职场文书
致100米运动员广播稿
2014/02/14 职场文书
正风肃纪剖析材料
2014/02/18 职场文书
工程管理英文求职信
2014/03/18 职场文书
国庆节演讲稿范文2014
2014/09/19 职场文书
甜品店创业计划书
2014/09/21 职场文书
2014年度考核工作总结
2014/12/24 职场文书
泰山导游词
2015/02/02 职场文书
安全生产奖惩制度
2015/08/06 职场文书
Python实现简单的猜单词
2021/06/15 Python
解决redis批量删除key值的问题
2022/03/23 Redis
MySQL GTID复制的具体使用
2022/05/20 MySQL