Python Logging Module logging timestamp to include microsecond

后端 未结 4 1711
猫巷女王i
猫巷女王i 2021-01-01 18:21

I am using python\'s logging module for logs, but needed the timestamp to include microsecond. It seems the timestamp can only get as precise as millisecond. Here\'s my test

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-01 19:01

    According to the documentation, strftime() does not support %f. The logger provides milliseconds as a separate msecs attribute, so you could just add it yourself after the existing timestamp as follows:

    logging.basicConfig(format='%(asctime)s.%(msecs)03d %(levelname)s {%(module)s} [%(funcName)s] %(message)s', datefmt='%Y-%m-%d,%H:%M:%S', level=logging.INFO)
    

    This gave me the following output using your script:

    2015-07-10,09:21:16.841 INFO {test script} [get_started2] Logged2 Here
    

提交回复
热议问题