Python Logging Module logging timestamp to include microsecond

后端 未结 4 1734
猫巷女王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:03

    I didnt find a easy way to print out microsecond,but %(created).6f could be a temp solution, which will be the result of time.time(),like 1517080746.007748.

    Didnt find a way to remove unnecessary part, so if you really need microsecond, but dont want to change your code too much, one easy way will be

    logging.basicConfig(level=logging.INFO,format="%(asctime)s.%(msecs)03d[%(levelname)-8s]:%(created).6f %(message)s", datefmt="%Y-%m-%d %H:%M:%S")

    It will give you below output,

    2018-01-28 04:19:06.807[INFO    ]:1517080746.807794 buy order issued
    2018-01-28 04:19:07.007[INFO    ]:1517080747.007806 buy order issued
    2018-01-28 04:19:07.207[INFO    ]:1517080747.207817 buy order issued
    2018-01-28 04:19:07.407[INFO    ]:1517080747.407829 buy order issued
    2018-01-28 04:19:07.607[INFO    ]:1517080747.607840 buy order issued
    

提交回复
热议问题