logging time format millisecond precision decimalsign

David Bolen db3l.net at gmail.com
Fri Jul 20 20:50:28 CEST 2012

"Alex van der Spek" <zdoor at xs4all.nl> writes:

> I use this formatter in logging:
> formatter = logging.Formatter(fmt='%(asctime)s \t %(name)s \t %(levelname)s
> \t %(message)s')
> Sample output:
> 2012-07-19 21:34:58,382   root   INFO   Removed - C:\Users\ZDoor\Documents
> The time stamp has millisecond precision but the decimal separator is a
> comma.
> Can I change the comma (,) into a period (.) and if so how?

I do it by:

  1. Replacing the default date format string to exclude ms.
  2. Including %(msecs)03d in the format string where appropriate.  Using 'd'
     instead of s truncates rather than shows the full float value.

So in your case, I believe that changing your formatter creation to:

  formatter = logging.Formatter(fmt='%(asctime)s.%(msecs)03d \t %(name)s \t %(levelname)s \t %(message)s', '%Y-%m-%d %H:%M:%S')

should work.  This uses the same date format as the default, but
without ms, though of course you could also opt to make any other date
format you prefer.

-- David

More information about the Python-list mailing list