How to Change the time zone in Python logging?

前端 未结 7 1244
耶瑟儿~
耶瑟儿~ 2021-01-31 18:30

I would like to change the timestamp in the log file so that it reflects my current time zone so that i can debug errors at a faster rate,

is it possible that i can cha

相关标签:
7条回答
  • 2021-01-31 19:23
    #!/usr/bin/python
    
    from datetime import datetime
    from pytz import timezone
    import logging
    
    def timetz(*args):
        return datetime.now(tz).timetuple()
    
    tz = timezone('Asia/Shanghai') # UTC, Asia/Shanghai, Europe/Berlin
    
    logging.Formatter.converter = timetz
    
    logging.basicConfig(
        format="%(asctime)s %(levelname)s: %(message)s",
        level=logging.INFO,
        datefmt="%Y-%m-%d %H:%M:%S",
    )
    
    logging.info('Timezone: ' + str(tz))
    

    Using pytz to define a timezone relative to UTC.
    Based on the example by: secsilm

    0 讨论(0)
提交回复
热议问题