picologging icon indicating copy to clipboard operation
picologging copied to clipboard

Add support for %f (format microseconds) to datefmt

Open tarasko opened this issue 7 months ago • 9 comments

Closes https://github.com/microsoft/picologging/issues/184

Since libc strftime doesn't support %f specifier the implementation works it around by

  1. first, creating a temporary format string for strftime where %f is replaced with actual microseconds.
  2. passing this temporary format string to strftime

The first step is optional, it will be only invoked if %f is detected by the Formatter constructor

import picologging as logging

logging.basicConfig(format="%(levelname)s - %(asctime)s - %(name)s - %(module)s - %(message)s", datefmt="%F %T.%f")
logger = logging.getLogger()
logger.setLevel(logging.INFO)

if __name__ == '__main__':
    for i in range(5):
        logger.info("Hello world! %d", i)
INFO - 2024-07-23 04:15:56.170199 - root - <unknown> - Hello world! 0
INFO - 2024-07-23 04:15:56.170242 - root - <unknown> - Hello world! 1
INFO - 2024-07-23 04:15:56.170255 - root - <unknown> - Hello world! 2
INFO - 2024-07-23 04:15:56.170265 - root - <unknown> - Hello world! 3
INFO - 2024-07-23 04:15:56.170274 - root - <unknown> - Hello world! 4

tarasko avatar Jul 23 '24 02:07 tarasko