python-3.x - 如何在 Python 中使用时区记录当前时间?

标签 python-3.x logging timezone

我使用具有以下格式化程序的日志记录配置(YAML 文件):

formatters:
    detailed:
        class: logging.Formatter
        format: '[%(asctime)s]:[%(levelname)s]: %(message)s'

asctime 给出类似 2018-04-10 07:00:39,526 的字符串,但我也想获取当前时区,例如2018-04-10 07:00:39,526+02:00。如果没有 work-around from Python 2.X 可能吗? ?

最佳答案

您可以创建自定义日志记录类,如下所示:

import logging
class FormatterWithTimezone(logging.Formatter):
    def formatTime(self, record, datefmt=None):
        return (
            time.strftime('%Y-%m-%d %H:%M:%S,%%03d%z', self.converter(record.created))
            % record.msecs)

然后设置这个类来代替 logging.Formatter

关于python-3.x - 如何在 Python 中使用时区记录当前时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49750753/

相关文章:

python-3.x - ImportError : cannot import name from __init__. py

通过 QueueHandler 的 Python 多处理日志记录

java - 计算差异顺便说一句。不同地点/时区的时间(考虑夏令时)

python - 如何在 VSCode 中启动 django unittest?

python - 如何复制列表中的项目(保留第一个和最后一个项目)并将列表转换为两个项目的列表列表

Python:合并两个字典

java - 空消息工厂的 Log4j 2.6 警告

javascript - Chrome/Firefox console.log总是在命令行后添加 'undefined'

php - momentjs 和 php 中的时区不一致

oracle - 如何获取 Oracle 上 SYSDATE 的 UTC 值