默认情况下logging.Formatter('%(asctime)s'),使用以下格式打印:
2011-06-09 10:54:40,638
Run Code Online (Sandbox Code Playgroud)
其中638是毫秒.我需要将逗号更改为点:
2011-06-09 10:54:40.638
Run Code Online (Sandbox Code Playgroud)
格式化我可以使用的时间:
logging.Formatter(fmt='%(asctime)s',datestr=date_format_str)
Run Code Online (Sandbox Code Playgroud)
但是文档没有指定如何格式化毫秒.我发现这个SO问题谈论微秒,但是a)我更喜欢毫秒和b)以下不适用于Python 2.6(我正在研究),因为%f:
logging.Formatter(fmt='%(asctime)s',datefmt='%Y-%m-%d,%H:%M:%S.%f')
Run Code Online (Sandbox Code Playgroud) 我遇到了Python 2.7日志记录模块的问题.我的系统是Ubuntu 14.04 64bit,我住在意大利(目前UTC + 1,没有夏令时); 系统正确配置.
我想在当前时区发出记录行,完成正确的时区偏移信息.
请考虑以下代码段:
#!/usr/bin/env python
import sys
print sys.version_info
import commands
print "System time: %s" % commands.getoutput("date --rfc-3339=seconds")
import logging
import datetime
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout,
format="%(asctime)s:" + logging.BASIC_FORMAT,
datefmt="%Y-%m-%dT%H:%M:%S%z")
logger = logging.getLogger()
logger.info("Something happened")
Run Code Online (Sandbox Code Playgroud)
结果如下:
sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
System time: 2015-01-09 11:21:44+01:00
2015-01-09T11:21:44+0000:INFO:root:Something happened
Run Code Online (Sandbox Code Playgroud)
因此,系统知道正确的时间和偏移量,而Python似乎错误的时间.
在datetime docs中,据说对于strftime中的%z,结果是"格式为+ HHMM或-HHMM的UTC偏移量(如果对象是天真的,则为空字符串)".
所以,我希望得到以下结果之一:
相反,在这里,我似乎得到了一个不可预测的 - 而且显然是错误的 - 结果.这是怎么回事?是否有可能配置日志模块,做我想做的无覆盖logging.Formatter对象的转换器功能(其中,当然,我可以做任何我喜欢,但是这看起来显然是个错误给我)?