logging日志模块
为什么要做日志:
- 审计跟踪:但错误发生时,你需要清除知道该如何处理,通过对日志跟踪,你可以获取该错误发生的具体环境,你需要确切知道什么是什么引起该错误,什么对该错误不会造成影响。
- 跟踪应用的警告和错误:为了识别错误,我们将日志分为警告和错误信息,这些都是可以跟踪并予以解决的
- 跟踪崩溃bug:在开发过程中,日志可以帮助开发者和软件测试人员跟踪程序崩溃的原因。
- 跟踪性能下降的问题范围:产品所反映出来的性能问题,很难在开发过程中暴露出来,这需要进行全方位的测试跟踪,而通过日志提供的详细执行时间记录可以很方便的找出应用的性能瓶颈。
标准日志格式:
[2012-03-02T20:20:49.003+02:00][43gg84][info] Bootstrapping application (v 2.1b)
[2012-03-02T20:20:49.013+02:00][43gg84][info] Request cycle startup
[2012-03-02T20:20:49.123+02:00][43gg84][info] Requested URI '/fu/bar/index'
[2012-03-02T20:20:49.273+02:00][43gg84][info] Sending HTTP headers
[2012-03-02T20:20:49.283+02:00][43gg84][erro] Cannot modify header information – headers already sent by X on line Y
[2012-03-02T20:20:49.293+02:00][43gg84][dbug] Stack trace:
[2012-03-02T20:20:49.293+02:00][43gg84][dbug] 1. {main}() /tmp/fu/bar/httpdocs/index.php:0
[2012-03-02T20:20:49.293+02:00][43gg84][dbug] 2. Zend_Application->run() /tmp/fu/bar/httpdocs/index.php:31
[2012-03-02T20:20:49.293+02:00][43gg84][dbug] 3. Zend_Application_Bootstrap_Bootstrap->run() /tmp/fu/bar/library/Zend/Application.php:366
[2012-03-02T20:20:49.293+02:00][43gg84][dbug] 4. Zend_Controller_Front->dispatch() /tmp/fu/bar/library/Zend/Application/Bootstrap/Bootstrap.php:97
[2012-03-02T20:20:49.293+02:00][43gg84][dbug] 5. Zend_Controller_Dispatcher_Standard->dispatch() /tmp/fu/bar/library/Zend/Controller/Front.php:954
[2012-03-02T20:20:49.293+02:00][43gg84][dbug] 6. Zend_Controller_Action->dispatch() /tmp/fu/bar/library/Zend/Controller/Dispatcher/Standard.php:295
[2012-03-02T20:20:49.293+02:00][43gg84][dbug] 7. IndexController->indexAction() /tmp/fu/bar/library/Zend/Controller/Action.php:513
[2012-03-02T20:20:49.313+02:00][43gg84][info] Sending HTTP Body
[2012-03-02T20:20:49.323+02:00][43gg84][info] Request cycle shutdown
[2012-03-02T20:20:49.333+02:00][43gg84][info] Request cycle completed in 0.330 seconds.
日志级别:
日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,当然也可以自己定义日志级别。
python的logging日志模块:
思路:
1,打印到屏幕
2,打印到文件
3,同时打印到屏幕和文件
打印到屏幕
import logging
logging.info("user logging info")
logging.error("user logging error")
logging.warning("user logging warning")
logging.critical("user logging critical") 默认打印的最低级别warning
打印到文件
import logging logging.basicConfig(filename="example.log",
filemode="w",
format="%(asctime)s,%(levelname)s,%(name)s,%(funcName)s,%(pathname)s,%(filename)s,%(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.INFO) #定义输出到文件
def func1():
'''此函数打印日志'''
logging.info("user logging info")
logging.warning("user logging warning")
logging.critical("user logging critical")
func1() example.log中:
2016-08-20 16:28:34,INFO,root,func1,D:/visual doc/pyProject/ģ���о�ר��/jsonר��/loggingPro.py,loggingPro.py,user logging info
2016-08-20 16:28:34,WARNING,root,func1,D:/visual doc/pyProject/ģ���о�ר��/jsonר��/loggingPro.py,loggingPro.py,user logging warning
2016-08-20 16:28:34,CRITICAL,root,func1,D:/visual doc/pyProject/ģ���о�ר��/jsonר��/loggingPro.py,loggingPro.py,user logging critical
basicConfig 参数:
filename 文件名
filemode 操作模式
format 日志格式
datefmt 时间格式,传给format的$(asctime)
# style
level 日志级别
stream 设置流句柄
# handlers
format格式参照:找到formatter类,就有解释:
%(name)s logger名,root
%(levelno)s 日志级别数值
%(levelname)s 日志级别名称
%(pathname)s 脚本文件路径
%(filename)s 脚本文件名
%(module)s Module (name portion of filename)
%(lineno)d 打印行号
%(funcName)s 打印
%(created)f Time when the LogRecord was created (time.time()
return value)
%(asctime)s 格式时间,用来接收datafmt参数
%(msecs)d Millisecond portion of the creation time
%(relativeCreated)d Time in milliseconds when the LogRecord was created,
relative to the time the logging module was loaded
(typically at application startup time)
%(thread)d 线程ID
%(threadName)s 线程名
%(process)d 进程ID
%(message)s 输出信息
同时输出到屏幕和文件:
思路:
做好输出到文件->定义输出流句柄->设置输出的级别->添加句柄
#输出流句柄StreamHandler,这些级别,格式都在这里设置
console=logging.StreamHandler()
console.setLevel(logging.INFO)
fmt=logging.Formatter("%(asctime)s,%(levelname)s,%(message)s")
console.setFormatter(fmt)
logging.getLogger('').addHandler(console) #输出到屏幕
参考资料:
logging日志模块的更多相关文章
- logging 日志模块学习
logging 日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪,所以还是灰常重要滴,下面我就来从入门到放弃的系统学习一下日志既可以在屏幕上显示,又可以在文件中体现. ...
- python 自动化之路 logging日志模块
logging 日志模块 http://python.usyiyi.cn/python_278/library/logging.html 中文官方http://blog.csdn.net/zyz511 ...
- day31 logging 日志模块
# logging 日志模块 ****** # 记录用户行为或者代码执行过程 # print 来回注释比较麻烦的 # logging # 我能够“一键”控制 # 排错的时候需要打印很多细节来帮助我排错 ...
- logging日志模块的使用
logging日志模块的使用 logging模块中有5个日志级别: debug 10 info 20 warning 30 error 40 critical 50 通常使用日志模块,是用字典进行配置 ...
- Python入门之logging日志模块以及多进程日志
本篇文章主要对 python logging 的介绍加深理解.更主要是 讨论在多进程环境下如何使用logging 来输出日志, 如何安全地切分日志文件. 1. logging日志模块介绍 python ...
- Python 中 logging 日志模块在多进程环境下的使用
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Python 中 logging 日志模块在多进程环境下的使用 使用 Pytho ...
- Python logging(日志)模块
python日志模块 内容简介 1.日志相关概念 2.logging模块简介 3.logging模块函数使用 4.logging模块日志流处理流程 5.logging模块组件使用 6.logging配 ...
- 约束、自定义异常、hashlib模块、logging日志模块
一.约束(重要***) 1.首先我们来说一下java和c#中的一些知识,学过java的人应该知道,java中除了有类和对象之外,还有接口类型,java规定,接口中不允许在方法内部写代码,只能约束继承它 ...
- pyhton——logging日志模块的学习
https://www.cnblogs.com/yyds/p/6901864.html 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模 ...
随机推荐
- OD调试篇6--对一些真正的小程序进行一点点的修改
先打开这个程序看看,提醒你这是一个未注册版本的软件.会发现只能添加4个联系人,这显然是我不想看见的,于是我要对这个程序进行一些修改,嘿嘿... 通过OD载入这个程序 有一些(SEH)也就是异常,我们可 ...
- FatMouse' Trade_贪心
Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...
- Java数据结构和算法之哈希表
五.哈希表 一般的线性表.树中,记录在结构中的相对位置是随机的即和记录的关键字之间不存在确定的关系,在结构中查找记录时需进行一系列和关键字的比较.这一类查找方法建立在“比较”的基础上,查找的效率与比较 ...
- 学习笔记:The Log(我所读过的最好的一篇分布式技术文章)
前言 这是一篇学习笔记. 学习的材料来自Jay Kreps的一篇讲Log的博文. 原文很长,但是我坚持看完了,收获颇多,也深深为Jay哥的技术能力.架构能力和对于分布式系统的理解之深刻所折服.同时也因 ...
- jq获取当前点击的li是ul中的第几个?
<script> var navulsize = $('.navul li').size(); var navulwidth = $('.navul li').wid ...
- UILabel 的高度根据文字内容调整
1.UILabel 对文字的自适应有两种方法. 1)将label的numberOfLines设为0;并添加自适应方法[titleLabel sizeToFit],但是这种方法并不理想. 2)根据文字的 ...
- mysql添加外键
语法:alter table 表名 add constraint FK_ID foreign key(你的外键字段名) REFERENCES 外表表名(对应的表的主键字段名); 例: alter ta ...
- 关于Mac下的SSH客户端iterm2等配置
linux后台开发的同学们晓得,在windows下有xshell\securecrt这样优秀的ssh客户端软件.mac下查找了下,有securecrt mac版,网上也有破解的,试用了一段时间,一个问 ...
- 2016HUAS_ACM暑假集训2D - 敌兵布阵
刚开始接触线段树,不得不说,每次接触到一个新的数据结构,都会是一场头脑风暴的“盛宴”.希望我能继续痛苦并快乐着学下去.我相信,有各路大神的博客相助,我还是能坚持下去的. 这个题目是HDU的1166,只 ...
- jquery选择器之属性选择器
[attribute] 匹配指定属性名的所有元素 [attribute=value] 匹配给定的属性名是某个特定值的属性 [attribute!=value] 匹配给定的属性名不是某个特定值的属性 ...