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模 ...
随机推荐
- apache2服务器mod_rewrite模块 开启方法[linux, ubuntu]
在UBUNTU系统中要启用mod_rewrite的方法有两种: 第一种: 在终端中执行 sudo a2enmod rewrite 指 令后,即启用了 Mod_rewrite 模块, apache2服务 ...
- React Native中的网络请求fetch和简单封装
React Native中的网络请求fetch使用方法最为简单,但却可以实现大多数的网络请求,需要了解更多的可以访问: https://segmentfault.com/a/1190000003810 ...
- swift项目中使用OC/C的方法
假如有个OC类OCViewController : UIViewController类里有两个方法 //swift调用oc或c的混编是比较常用的,反过来的调用很少.这里只写了swift调用oc和c的方 ...
- M3: 将页面元素制作为图片
本小节将介绍如何将页面元素保存为图片,在前一小节中,我们加入了名称为gridMsg的Grid Control,现在我们将使用RenderTargetBitmap把gridMsg这个页面元素保存为一张图 ...
- Android关联源码support-v4,v7,v13源码(转)
在Android实际开发过程中往往会遇到使用v4,v7或v13兼容包中的一些类如ViewPager,Fragment等,但却无法关联源码. 在网上搜索之后,有很多办法,这里只向大家介绍一种,我用的觉得 ...
- 一个基于ANTLR 4的布尔表达式语句解释器的实现
Reference The Definitive ANTLR 4 Reference, 2nd Edition. 0 Features labeled grammar definition, i.e. ...
- position置顶或某固定位置 兼容ie6ie7
用absolute来模拟fixed效果: /*相当于正常的position:fixed;top:0 */.sl_fixed_top{bottom:auto;top:0;_bottom:auto;_to ...
- jquery选择器之子元素
HTML代码: :first-child 匹配第一个子元素,每个父元素的第一个子元素 :last-child 匹配最后一个子元素,每个父元素的最后一个子元素 <!DOCTYPE html> ...
- SQL级联删除——删除主表同时删除从表——同时删除具有主外键关系的表
create table a(id varchar(20) primary key,password varchar(20) not null) create table b(id int iden ...
- 利用硬链接和truncate降低drop table对线上环境的影响
众所周知drop table会严重的消耗服务器IO性能,如果被drop的table容量较大,甚至会影响到线上的正常. 首先,我们看一下为什么drop容量大的table会影响线上服务 直接执行drop ...