How to Change the time zone in Python logging?

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

    just add this this pythonic line to your code (using pytz and datetime):

    from pytz import timezone
    from datetime import datetime
    
    logging.Formatter.converter = lambda *args: datetime.now(tz=timezone('tz string name')).timetuple()
    
    # quoting Ryan J McCall: to find the string name for your desired timezone...
    # print(pytz.all_timezones)
    # or print(pytz.common_timezones)
    

提交回复
热议问题