参考:
Basic Tutorial:https://docs.python.org/2.7/howto/logging.html#logging-basic-tutorial
###########################################################################################
学习了一段时间python的logging模块,写下这篇文章
logging模块安装:
sudo pip install logging
logging模块可以让你在python开发中有效的管理,调试
本次基础教程达到目标:能够有效使用logging模块
logging模块的使用有助于开发者以及使用者了解软件运行的过程,默认提供了5个函数:
debug(), info(), warning(), error()和critical()
其表示重要程度从左到右递增,debug()表示最普通的信息,critical()表示最重要的信息
其官方推荐使用场景如下:
这5个函数对应于5个等级(level或severity),分别是:
DEBUG, INFO, WARNING, ERROR, CRITICAL
官方推荐使用场景如下:
默认等级是WARNNING,也就是说只有等于高于这个等级的日志才会被处理,而低于这个等级信息并不会被处理
消息处理的方式有很多种,最简单的就是输出到控制台,还有一种很常见的方式是打印到文件上。
######################################################
先进行一个简单的例子:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import logging
logging.debug("This is debug message")
logging.info("This is info message")
logging.warning("This is warning message")
logging.error("This is error message")
logging.critical("This is critical message")
由图中可知,仅仅输出了warning(),error()和critical()函数的信息,这是因为默认等级是WARNNING,可以通过函数basicConfig()的level参数来进行修改:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("This is debug message")
logging.info("This is info message")
logging.warning("This is warning message")
logging.error("This is error message")
logging.critical("This is critical message")
将处理信息的等级重设为DEBUG,就能输出所有的信息,所以可以自己根据需要调整level来输出信息
注意:basicConfig函数必须在所有logging函数之前使用
#######################################################
在上面输出的信息中除了我们输入的信息外,还会有其他的信息输出。我们也可以通过basicConfig函数的format参数来修改输出信息的格式:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import logging
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
logging.debug("This is debug message")
logging.critical("This is critical message")
现在你输出的仅有等级和信息。
可以配置很多属性,如下图所示:
在博客 - python之强大的日志模块:http://www.cnblogs.com/txw1958/archive/2011/10/21/2220636.html中找到一个比较不错的格式字符串:
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
asctime:输出时间
filename:输出当前执行程序的路径,就是sys.argv[0]
lineno:当前行号
levelname:等级
message:信息
效果如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import logging
logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', level=logging.DEBUG)
logging.debug("This is debug message")
logging.critical("This is critical message")
从上图中可以看出输出的时间格式并不好,默认的时间/日期格式是ISO8601。如果你想要更改时间格式,可以通过basicConfig函数的datafmt参数进行修改:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import logging
logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y/%m/%d %I:%M:%S %p', level=logging.DEBUG)
logging.debug("This is debug message")
logging.critical("This is critical message")
这样似乎更加易于理解
#################################################################3
有时候你想要在信息中输入一些变量,logging模块也支持这一特性:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import logging
logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y/%m/%d %I:%M:%S %p', level=logging.DEBUG)
logging.debug("This is %s message: %d", "debug", 32)
logging.critical("This is %.2f message: %s", 33.23, "critical")
这样就可以方便的将数据整合到信息中
同时logging模块也支持新的字符串连接函数,比如str.format或str.Template:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import logging
logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y/%m/%d %I:%M:%S %p', level=logging.DEBUG)
logging.debug("This is {} message: {}".format("debug", 32))
logging.critical("This is {} message: {}".format(33.23, "critical"))
#############################################
下面我们将日志信息输出到文件中,仅需在basicConfig函数中进行操作即可:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import logging
logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y/%m/%d %I:%M:%S %p', level=logging.DEBUG, filename='example.log')
logging.debug("This is {} message: {}".format("debug", 32))
logging.critical("This is {} message: {}".format(33.23, "critical"))
运行,生成文件example.log,其输出内容为:
2016/06/03 09:20:53 PM test.py[line:7] DEBUG This is debug message: 32
2016/06/03 09:20:53 PM test.py[line:8] CRITICAL This is 33.23 message: critical
但是当你再次运行上述程序时,example.log文件中内容为:
2016/06/03 09:20:53 PM test.py[line:7] DEBUG This is debug message: 32
2016/06/03 09:20:53 PM test.py[line:8] CRITICAL This is 33.23 message: critical
2016/06/03 09:23:08 PM test.py[line:7] DEBUG This is debug message: 32
2016/06/03 09:23:08 PM test.py[line:8] CRITICAL This is 33.23 message: critical
表明默认对文件操作是添加并不是重新创建,可以通过修改basicConfig函数中的filemode参数来进行设定:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import logging
logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y/%m/%d %I:%M:%S %p', level=logging.DEBUG, filename='example.log', filemode='w')
logging.debug("This is {} message: {}".format("debug", 32))
logging.critical("This is {} message: {}".format(33.23, "critical"))
经过修改后,不论重新运行程序多少次,example.log文件中内容并不会重复
#######################################
现在我们在多个模块中进行操作:
a.py内容如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import logging
import b
def main():
logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y/%m/%d %I:%M:%S %p', level=logging.DEBUG, filename='example.log', filemode='w')
logging.info("Started")
b.do_some()
logging.warning("Finished")
if __name__ == "__main__":
main()
b.py内容如下:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import logging
def do_some():
logging.debug("hello world")
运行程序a.py:
python a.py
生成文件example.log,打开:
2016/06/03 09:31:31 PM a.py[line:9] INFO Started
2016/06/03 09:31:31 PM b.py[line:7] DEBUG hello world
2016/06/03 09:31:31 PM a.py[line:11] WARNING Finished
这样就能成功在多个模块中运行
###################################################
现在你已经能够在你的python代码中简单的使用logging模块了,如果你想要进一步的了解和使用logging,继续查看下面的内容:
Advanced Tutorial:https://docs.python.org/2.7/howto/logging.html#logging-advanced-tutorial
python logging-高级:http://blog.csdn.net/u012005313/article/details/51588317