python之logging模块简单用法
前言:
python引入logging模块,用来记录自己想要的信息。print也可以输入日志,但是logging相对print来说更好控制输出在哪个地方。怎么输出以及控制消息级别来过滤掉那些不需要的信息。
日志级别:
代码:
# coding:utf-8
import logging # 引入logging模块 # 将信息打印到控制台上 # 如果需要显示低于WARNING级别的内容,可以引入NOTSET级别来显示
logging.basicConfig(level=logging.NOTSET) # 设置日志级别
logging.debug(u"小花")
logging.info(u"如果设置了日志级别为NOTSET,那么这里可以采取debug、info的级别的内容也可以显示在控制台上了") logging.warning(u"小丽")
logging.error(u"zxc")
logging.critical(u"qwe")
# 正常情况下只能看到后面三个能打印出来。因为:默认生成的root logger的level是logging.WARNING,低于该级别的就不输出了。级别排序:CRITICAL > ERROR > WARNING > INFO > DEBUG
执行结果:
DEBUG:root:小花
INFO:root:如果设置了日志级别为NOTSET,那么这里可以采取debug、info的级别的内容也可以显示在控制台上了
WARNING:root:小丽
ERROR:root:zxc
CRITICAL:root:qwe
日志输出-控制台:
代码:
# coding:utf-8
import logging # 引入logging模块 # 通过logging.basicConfig 函数进行配置了日志级别和日志内容输出格式
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') # logging.basicConfig函数对日志的输出格式及方式做相关配置
# 因为日志基本配置中级别设置为DEBUG,所以会将DEBUG级别以上的信息都输出显示再控制台上
logging.debug('this is a loggging debug message')
logging.info('this is a loggging info message')
logging.warning('this is loggging a warning message')
logging.error('this is an loggging error message')
logging.critical('this is a loggging critical message')
执行结果:
2018-07-20 10:17:54,727 - logging1.py[line:8] - INFO: this is a loggging info message
2018-07-20 10:17:54,805 - logging1.py[line:9] - DEBUG: this is a loggging debug message
2018-07-20 10:17:54,805 - logging1.py[line:10] - WARNING: this is loggging a warning message
2018-07-20 10:17:54,805 - logging1.py[line:11] - ERROR: this is an loggging error message
2018-07-20 10:17:54,805 - logging1.py[line:12] - CRITICAL: this is a loggging critical message
日志输出-控制台和文件、捕获异常:
代码:
# coding:utf-8
import logging # 引入logging模块
import time # 第一步,创建一个logger
logger = logging.getLogger() # 获取logger实例,如果参数为空,则返回root logger,即默认的logger名称是root
logger.setLevel(logging.INFO) # Log等级总开关. logger.setLevel()设置日志级别 # 第二步,创建一个handler,用于写入日志文件
rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
logfile = rq + ".log"
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG) # 输出到file的log等级的开关 # 同时输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING) # 第三步,定义handler的输出格式
# 配置日志的格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter) # 设置Formatter
ch.setFormatter(formatter) # 第四步,将logger添加到handler里面
logger.addHandler(fh) # 添加一个Handler
logger.addHandler(ch) # 使用logger.XX来记录错误,这里的"error"可以根据所需要的级别进行修改
try:
open('/path/to/does/not/exist', 'rb')
except (SystemExit, KeyboardInterrupt):
raise
except Exception as e:
logger.error('Failed to open file', exc_info=False) # 如果要求日志上报错误,可以将exc_info设置为True. # # 日志
# logger.debug('this is a logger debug message')
# logger.info('this is a logger info message')
# logger.warning('this is a logger warning message')
# logger.error('this is a logger error message')
# logger.critical('this is a logger critical message')
执行结果:
2018-07-20 10:45:11,936 - logging1.py[line:35] - ERROR: Failed to open file # 文件和控制台上是一致的
日志的输出顺序和模块的执行顺序是一致的。
注意:
“AttributeError:moudle 'logging' has no attribute 'basicConfig'”——原因为:把文件名命名为logging.py导致该错误。
参考链接:
https://www.cnblogs.com/CJOKER/p/8295272.html
https://blog.csdn.net/zyz511919766/article/details/25136485
python之logging模块简单用法的更多相关文章
- python中logging模块的用法
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- Python的logging模块基本用法
Python 的 logging 模块的简单用法 在服务器部署时,往往都是在后台运行.当程序发生特定的错误时,我希望能够在日志中查询.因此这里熟悉以下 logging 模块的用法. logging 模 ...
- logging模块简单用法
logging模块功能比较多,但一般情况下使用其简单功能就已经足够了. 最简单的用法如下: import logging logging.baiscConfig(level=logging.DEBUG ...
- Python中logging模块的基本用法
在 PyCon 2018 上,Mario Corchero 介绍了在开发过程中如何更方便轻松地记录日志的流程. 整个演讲的内容包括: 为什么日志记录非常重要 日志记录的流程是怎样的 怎样来进行日志记录 ...
- python的logging模块
python提供了一个日志处理的模块,那就是logging 导入logging模块使用以下命令: import logging logging模块的用法: 1.简单的将日志打印到屏幕上 import ...
- python基础--logging模块
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- python的logging模块之读取yaml配置文件。
python的logging模块是用来记录应用程序的日志的.关于logging模块的介绍,我这里不赘述,请参见其他资料.这里主要讲讲如何来读取yaml配置文件进行定制化的日志输出. python要读取 ...
- Python的logging模块详解
Python的logging模块详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.日志级别 日志级别指的是产生的日志的事件的严重程度. 设置一个级别后,严重程度 ...
- python之 logging 模块(上篇)
一.日志关概念 日志是一种可以追踪某些软件运行时所发生事件的方法.软件开发人员可以向他们的代码中调用日志记录相关的方法来表明发生了某些事情.一个事件可以用一个可包含可选变量数据的消息来描述.此外,事件 ...
随机推荐
- 从头开始学Android之(一)——— Android架构
从事Android开发已经两年多了,最近项目上特别清闲,刚开始时在闲暇的时候都不知道干嘛,整天混日子.有一天突然有个以前同学找到我,说要我帮忙做一个Android的需求,就是在后台截屏(涉及到服务以及 ...
- win10 powershell 验证下载的包的MD5/sha1的签名值
巧用Win10自带的PowerShell命令校验文件的Hash值(MD5.SHA1/256等) 发表于2017年3月8日由MS酋长 通常为了保证我们从网上下载的文件的完整性和可靠性,我们把文件下载下来 ...
- input 文本框禁止输入表情
js在用户输入表情时自动过滤掉 <input type="text" id="input" maxlength="10"/> v ...
- swift-for循环遍历,遍历字典,循环生成数组
// Playground - noun: a place where people can play import UIKit //--------------------------------- ...
- Binder系列8—如何使用Binder(转)
一.Native层Binder 源码结构: ClientDemo.cpp: 客户端程序 ServerDemo.cpp:服务端程序 IMyService.h:自定义的MyService服务的头文件 IM ...
- css中使input输入框与img(图片)在同一行居中对齐
input,img{vertical-align:middle;},同时设置input和img的vertical-align属性,兼容ie7
- 【Mongodb教程 第一课 补加课】 Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接
1:启动MongoDB 2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errn ...
- 蓝牙4.0BLE cc2540 usb-dongle的 SmartRF Packet Sniffer 抓取数据方法 【原创,多图】
蓝牙4.0BLE cc2540 usb-dongle的 SmartRF Packet Sniffer 抓取数据方法 [原创,多图] spm=a1z10.1.w4004-5319414070.11.Zd ...
- mac for smartSVN9 (8,9)破解方法 附smartSvn_keygen工具图文
mac for smartSVN9 (8,9)破解方法 附smartSvn_keygen工具 工具文件下载: http://files.cnblogs.com/files/xueshanshan/s ...
- SpringMVC_基本配置 --跟海涛学SpringMVC(和自己在项目中的实际使用的对比)
☆依赖jar包: 1.Spring框架jar 包: 为了简单,将spring-framework-3.1.1.RELEASE-with-docs.zip/dist/下的所有jar包拷贝到项目的WEB- ...