为什么要做日志:

  • 审计跟踪:但错误发生时,你需要清除知道该如何处理,通过对日志跟踪,你可以获取该错误发生的具体环境,你需要确切知道什么是什么引起该错误,什么对该错误不会造成影响。
  • 跟踪应用的警告和错误:为了识别错误,我们将日志分为警告和错误信息,这些都是可以跟踪并予以解决的
  • 跟踪崩溃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日志模块的更多相关文章

  1. logging 日志模块学习

    logging 日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪,所以还是灰常重要滴,下面我就来从入门到放弃的系统学习一下日志既可以在屏幕上显示,又可以在文件中体现. ...

  2. python 自动化之路 logging日志模块

    logging 日志模块 http://python.usyiyi.cn/python_278/library/logging.html 中文官方http://blog.csdn.net/zyz511 ...

  3. day31 logging 日志模块

    # logging 日志模块 ****** # 记录用户行为或者代码执行过程 # print 来回注释比较麻烦的 # logging # 我能够“一键”控制 # 排错的时候需要打印很多细节来帮助我排错 ...

  4. logging日志模块的使用

    logging日志模块的使用 logging模块中有5个日志级别: debug 10 info 20 warning 30 error 40 critical 50 通常使用日志模块,是用字典进行配置 ...

  5. Python入门之logging日志模块以及多进程日志

    本篇文章主要对 python logging 的介绍加深理解.更主要是 讨论在多进程环境下如何使用logging 来输出日志, 如何安全地切分日志文件. 1. logging日志模块介绍 python ...

  6. Python 中 logging 日志模块在多进程环境下的使用

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Python 中 logging 日志模块在多进程环境下的使用 使用 Pytho ...

  7. Python logging(日志)模块

    python日志模块 内容简介 1.日志相关概念 2.logging模块简介 3.logging模块函数使用 4.logging模块日志流处理流程 5.logging模块组件使用 6.logging配 ...

  8. 约束、自定义异常、hashlib模块、logging日志模块

    一.约束(重要***) 1.首先我们来说一下java和c#中的一些知识,学过java的人应该知道,java中除了有类和对象之外,还有接口类型,java规定,接口中不允许在方法内部写代码,只能约束继承它 ...

  9. pyhton——logging日志模块的学习

    https://www.cnblogs.com/yyds/p/6901864.html 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模 ...

随机推荐

  1. 黑马程序员——【Java基础】——正则表达式

    ---------- android培训.java培训.期待与您交流! ---------- 一.概述 1. 概念:符合一定规则的表达式. 2. 作用:用于专门操作字符串. 3. 特点:用一些特定的符 ...

  2. 黑马程序员——【Java基础】——集合框架

    ---------- android培训.java培训.期待与您交流! ---------- 一.集合框架概述 (一)集合框架中集合类关系简化图 (二)为什么出现集合类? 面向对象语言对事物的体现都是 ...

  3. Eclipse安装easyShell插件

    easyshell是一个用于快速打开文件目录.复制文件路径.cmd打开等等的eclipse插件工具. Eclipse下安装easyshell: 1.打开Eclipse商店 2.输入easyShell点 ...

  4. 从零开始学习Node.js例子四 多页面实现数学运算 续一(使用connect和express框架)

    1.使用connect框架 .use方法用于绑定中间件到connect服务器,它会配置一系列在接到请求时调用的中间件模块,此例中我们要配置的中间件有favicon logger static rout ...

  5. Sublime Text 2 中文 GBK 规范的配置 暨 解决中文乱码问题 简述

    首先通过 Package Control 安装 ConverToUTF8 插件,但是每次新建一个文本后并不是默认使用 GBK,要设置默认使用 GBK,请继续看,然后我们 Ctrl + O 打开文件,在 ...

  6. Spring4 + Quartz-2.2.0集成实例

    Spring3.0不支持Quartz2.0,因为org.quartz.CronTrigger在2.0从class变成了一个interface造成IncompatibleClassChangeError ...

  7. python 元组操作

    关于元组的常用操作,请参考:http://www.runoob.com/python/python-tuples.html 元组的元素不可修改 ,元组的元素的元素可修改 count(self,valu ...

  8. WCF实现客户端自动更新-GenerateFileList

    GenerateFileList using System; using System.Collections.Generic; using System.Diagnostics; using Sys ...

  9. iphone field test 源码

    Iphone工程模式读取周围BTS信息的路测程序:包括后台和界面.-iphone field test, used for reading the BTS infomation nearby. 下载地 ...

  10. Windows环境下使用Redis缓存工具的图文详细方法

    一.简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有序集合). ...