相关疑难解决方法(0)

Python日志记录:以时间格式使用毫秒

默认情况下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 time logging

141
推荐指数
7
解决办法
8万
查看次数

Python日志记录模块发出错误的时区信息

我遇到了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偏移量(如果对象是天真的,则为空字符串)".

所以,我希望得到以下结果之一:

  1. 记录模块想要以UTC身​​份登录,所以我会得到类似10:21.44和+0000偏移量的东西;
  2. 日志记录模块想要登录本地时间,所以我会得到类似11:21:44和+01000偏移量的东西;
  3. 日志记录模块提供了天真的日期时间对象,我们在偏移量上完全没有.

相反,在这里,我似乎得到了一个不可预测的 - 而且显然是错误的 - 结果.这是怎么回事?是否有可能配置日志模块,做我想做的无覆盖logging.Formatter对象的转换器功能(其中,当然,我可以做任何我喜欢,但是这看起来显然是个错误给我)?

python logging datetime python-2.7

9
推荐指数
2
解决办法
8553
查看次数

标签 统计

logging ×2

python ×2

datetime ×1

python-2.7 ×1

time ×1