为什么要做日志:

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

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...

  2. Oracle 查看表空间的大小及使用情况sql语句

    --1.查看表空间的名称及大小 )), ) ts_size FROM dba_tablespaces t, dba_data_files d WHERE t.tablespace_name = d.t ...

  3. tensorflow4

    参考:tensorflow_manual_cn.pdf 一.图像的四维张量和参数的四维张量貌似不同: 二.流程回顾 1.数据准备 2.Page 63 三.状态可视化 四.保存检查点(保存参数) 五.评 ...

  4. [原创] 用两个stack实现queue的功能

    #include <iostream> #include <stack> using namespace std; class doubleStackToQueue { pri ...

  5. Charles初体验

      背景: 谈起HTTP调试代理工具, 很多人第一反应就会提到Fiddler. 可惜Fiddler由C#编写, 对Mac电脑的支持并不友好(存在Mac版Fiddler--mono fiddler, 不 ...

  6. 【转载】 postman使用教程

    一 接口请求流程     二 postman使用   从流程图中我们可以看出,一个接口请求需要设置:请求URL,请求方法,请求头,请求参数.同样的,在postman中,我们也只需要设置这四项即可完成一 ...

  7. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. Python 基础语法(三)

    Python 基础语法(三) --------------------------------------------接 Python 基础语法(二)------------------------- ...

  9. 如果在代码中使用JS

    protected void Page_Load(object sender, EventArgs e)    {        //获得.js文件        string myscript = ...

  10. Redis客户端之Spring整合Jedis,ShardedJedisPool集群配置

    Jedis设计 Jedis作为推荐的java语言redis客户端,其抽象封装为三部分: 对象池设计:Pool,JedisPool,GenericObjectPool,BasePoolableObjec ...