0

I'm trying to use the Python logging framework. It works well except for one thing.

This sample code:

import logging

FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
logging.info('test')

produces this output:

2014-03-18 11:23:43,082 test

Note the comma rather than "." separating seconds from milliseconds. We are in the USA and there should be a setting somewhere which controls this.

What can I set so that the default locale info uses a period "." for a decimal separator?

Jason S
  • 184,598
  • 164
  • 608
  • 970

3 Answers3

4

The milliseconds are not part of the locale - you can't specify them using e.g. strftime. Hence the ,nnn with the milliseconds is tacked on explicitly as per ISO 8601.

Update: Although the standard allows for a dot or a comma, it implies that a comma is preferred. Currently, logging doesn't allow this to be configurable, so if you need this, refer to this answer in the duplicate question.

Community
  • 1
  • 1
Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
  • thanks for the reference but https://en.wikipedia.org/wiki/ISO_8601#Times says "A decimal mark, either a comma or a dot (without any preference as stated in resolution 10 of the 22nd General Conference CGPM in 2003,[12] but with a preference for a comma according to ISO 8601:2004)[13] is used as a separator between the time element and its fraction". – Jason S Mar 18 '14 at 23:57
  • The standard allows either a comma or a dot. Python documentation for basicConfig doesn't seem to say which. – Jason S Mar 18 '14 at 23:58
3

The following code snippet, after the logging.basicConfig() call, works for me in Python 3.4:

for h in logging.getLogger().handlers:
    h.formatter.default_msec_format = '%s.%03d'
sethp
  • 124
  • 1
  • 1
1

Try this at basicConfig:

logging.basicConfig(format=FORMAT, level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S")

instead of : or - in my code, you can use any character if you wish.

Output:

2014-03-19 00:48:22 test
venpa
  • 4,268
  • 21
  • 23
  • Yeah, I know I can explicitly set whatever date format I want, but that's not what I asked. I want to know how to influence the `%(asctime)` locale. – Jason S Mar 18 '14 at 19:36