This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mariocj89
Recipients mariocj89, p-ganssle, vinay.sajip
Date 2019-05-31.18:44:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1559328242.82.0.712821467925.issue37117@roundup.psfhosted.org>
In-reply-to
Content
Users that want to provide a custom template for the timestamp part of logging cannot customize the milliseconds part of asctime.

They can use the msecs attribute of the Logger but that effectively requires to break the formatting in two parts. Note that it is not necessary to provide msecs on the default template as there is code that handles it in https://github.com/python/cpython/blob/c8d5bf6c3fa09b43f6a5ee779d493d251dbcc53c/Lib/logging/__init__.py#L603

Something we can do to improve this situation is changing the default converter to produce a datetime rather than a timetuple, as strftime of datetime and time uses the same template format.

This will allow changing the format including milliseconds through the datefmt argument.

formatter = logging.Formatter("%(asctime)s %(message)s", datefmt="%Y%m%d %H:%M:%S.%f")

Compare this to the current soltution:

formatter = logging.Formatter("%(asctime)s%(msecs)d %(message)s", datefmt="%Y%m%d %H:%M:%S")

Note how you need to split the formatting of time into two different parts. This becomes even more diserse if you also want to add something after the time formatting (Example: a Z to note UTC).

One more reason why this is quite powerful is that once we get timezones added to the standard library we will be able to (in a backward compatible way) just pass the timezone when we do `datetime.fromtimestamp`, which will add the timezone information to the datetime and users will be able to use %z to add the offset of the logging timestamp (Hurray! timestamps with offsets).

Sample implementation: https://github.com/mariocj89/cpython/commit/5047d730c0a0dcd6276f40c5b66762071dfcb448

If it looks interesting I can update the docs, add some further tests and send the PR. Wanted to confirm it is before putting time into that.

I cannot come with any "downside" of not doing it and I think it will simplify considerably the way users can format timestamps in logs. With this change we will just be able to say "To change the formatting of timestamps in a formatter, just use the datefmt argument".

Thoughts?
History
Date User Action Args
2019-05-31 18:44:02mariocj89setrecipients: + mariocj89, vinay.sajip, p-ganssle
2019-05-31 18:44:02mariocj89setmessageid: <1559328242.82.0.712821467925.issue37117@roundup.psfhosted.org>
2019-05-31 18:44:02mariocj89linkissue37117 messages
2019-05-31 18:44:02mariocj89create