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) #输出到屏幕

参考资料:

上一篇:Android多媒体框架对音乐播放器的支持


下一篇:WPF:如何为程序添加splashScreen(初始屏幕)