문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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'

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top