码迷,mamicode.com
首页 > 编程语言 > 详细

python之打印日志logging

时间:2017-08-13 00:16:34      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:filename   ogg   bsp   文件   and   not   python   was   inf   

 

 1 import logging
 2 
 3 
 4 # 简单打印日志举例
 5 logging.basicConfig(level=logging.DEBUG)  # 设置日志级别,WARN
 6 logging.warning(Watch out!)  # will print a message to the console
 7 logging.info(I told you so)  # will not print anything
 8 
 9 
10 # 打印日志到文件,注意要新起一个文件,否则不能保存文件
11 def log_to_file(logs_dir="D:\\test_data\\logs\\log_DEBUG.txt"):
12     logging.basicConfig(filename=logs_dir, level=logging.DEBUG)
13     logging.debug(This message should go to the log file)
14     logging.info(So should this)
15     logging.warning(And this, too)
16 
17 
18 log_to_file()
19 
20 # 多参数日志
21 logging.warning(%s before you %s, Look, leap!)
22 
23 # 日志中打印时间
24 logging.basicConfig(format=%(asctime)s %(message)s)
25 logging.warning(is when this event was logged.)
26 
27 # 指定时间格式
28 logging.basicConfig(format=%(asctime)s %(message)s, datefmt=%Y-%m-%d %I:%M:%S %p)
29 logging.warning(is when this event was logged.)
注意:logging的参数设置只在第一次运行之前有效,只能设置一次,后续设置无效。

指定时间格式运行结果:
1 # 指定时间格式
2 2017-08-12 10:47:07 PM is when this event was logged.

 

python之打印日志logging

标签:filename   ogg   bsp   文件   and   not   python   was   inf   

原文地址:http://www.cnblogs.com/gongxr/p/7351988.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!