设为首页 加入收藏

TOP

Python 日志记录模块logging的使用
2015-02-13 18:23:41 来源: 作者: 【 】 浏览:51
Tags:Python 日志 记录 模块 logging 使用

shell当中记录log,满眼花花的重定向符合,看着很不爽有没有!我喜欢python,就是喜欢这种买个手电筒,备用电池都准备好了的感觉。logging模块很简单,导入模块,定义日志格式。代码中就可以通过logging.info(),logging.warning(),logging.debug()记录日志了。而且立刻感觉整个人好了很多……
import logging
logging.basicConfig(level=logging.DEBUG,
? ? ? ? ? ? ? ? format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
? ? ? ? ? ? ? ? datefmt='%a, %d %b %Y %H:%M:%S',
? ? ? ? ? ? ? ? filename='mylog.log',
? ? ? ? ? ? ? ? filemode='a')
? ? ? ?
logging.info("这是logging模块记录的info")
logging.debug("这是logging模块的debug信息")
logging.warning("这是logging模块的warning信息")


日志输出如下:
Thu, 05 Feb 2015 17:24:43 1.py[line:12] INFO 这是logging模块记录的info
Thu, 05 Feb 2015 17:24:43 1.py[line:13] DEBUG 这是logging模块的debug信息
Thu, 05 Feb 2015 17:24:43 1.py[line:14] WARNING 这是logging模块的warning信息


--------------------------------------分割线 --------------------------------------


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Python 正则表达式re模块使用 下一篇Python 调用系统命令

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: