python - 替换 Python 记录器中的时间戳

标签 python python-2.7 datetime logging timestamp

我使用Python 2.7标准logging为我们的系统进行日志记录的模块。有时,我们会运行对过去事件的模拟,如果不使用 datetime.now() 中的系统时间,这确实很有用。我将能够使用我自己的自定义系统时间。

我知道有一种方法可以使用Formatter更改时间字符串格式,但我实际上也可以更改记录的时间字符串的内容吗?

非常感谢

最佳答案

来自:https://docs.python.org/3/howto/logging.html#formatters

Formatters use a user-configurable function to convert the creation time of a record to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the converter attribute of the instance to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the converter attribute in the Formatter class (to time.gmtime for GMT display).

因此,将您的自定义系统时间定义为与 time.localtime() 签名匹配的函数,并将其分配给记录器。

关于python - 替换 Python 记录器中的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46531162/

相关文章:

python - tensorflow 中逻辑回归模型的系数

python - 如何在不锁定 Vim 的情况下运行当前的 python 脚本?

python - 如何将输入读取为数字?

python - 在 IOError 上打开不同的文件

Java - 从 JSpinner 返回分钟秒毫秒

python - 如何将包含日期的 csv 数据加载到 numpy 数组中?

python - 在 Debian 上安装第二个 python

仅在 Linux 上 AsyncHTTPClient 中的 Python Tornado 阻塞。不适用于 Windows 或 OSX

python - PyCharm 解析 - flask.ext.sqlalchemy 与 flask_sqlalchemy

sql - Oracle - 将数字 8 位数字转换为日期