How to Change the time zone in Python logging?

前端 未结 7 1246
耶瑟儿~
耶瑟儿~ 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:15

    import logging, time
    from datetime import datetime, timedelta
    logger = logging.getLogger(__name__)
    converter = lambda x, y: (datetime.utcnow() - timedelta(
        hours=7 if time.localtime().tm_isdst else 6)
    ).timetuple()
    logging.Formatter.converter = converter
    

    Edited as Elias points out the original answer didn't check for DST.

提交回复
热议问题