我使用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/