You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
LoggingHandler and RedirectLoggingHandler need to be able to convert LogRecord.time to a timestamp, but logbook.set_datetime_format means it is impossible to know what a naive datetime means.
Logbook should match the Python standard library and treat naive datetimes as local time.
Plan
This will be a breaking change, i.e. Logbook 2.0
Remove logbook.set_datetime_format.
Change LogRecord.time to always use aware datetimes. May wish to have Logger pass down a timezone which defaults to timezone.utc.
Add a tzinfo attribute to Handler which does record.time.astimezone(tzinfo) if it is set to None or datetime.tzinfo. This allows having handlers convert to different timezones, similar to logging.Formatter.convert. We'll need to make this work in the format string, since currently you do record.time.
Converting to a timezone is similar performance to the conversion logging.Formatter already does:
In [1]: fromtimeimportlocaltime, gmtimeIn [2]: fromdatetimeimportdatetime, timezoneIn [3]: fromzoneinfoimportZoneInfoIn [4]: now=datetime.now(timezone.utc)
In [5]: nowts=now.timestamp()
In [6]: berlin=ZoneInfo('Europe/Berlin')
In [7]: %timeitlocaltime(nowts)
331ns ± 0.701nsperloop (mean ± std. dev. of7runs, 1,000,000loopseach)
In [8]: %timeitgmtime(nowts)
218ns ± 0.35nsperloop (mean ± std. dev. of7runs, 1,000,000loopseach)
In [9]: %timeitnow.astimezone(berlin)
195ns ± 0.657nsperloop (mean ± std. dev. of7runs, 1,000,000loopseach)
In [10]: %timeitnow.astimezone(timezone.utc)
45ns ± 0.104nsperloop (mean ± std. dev. of7runs, 10,000,000loopseach)
The text was updated successfully, but these errors were encountered:
LoggingHandler
andRedirectLoggingHandler
need to be able to convertLogRecord.time
to a timestamp, butlogbook.set_datetime_format
means it is impossible to know what a naive datetime means.Logbook should match the Python standard library and treat naive datetimes as local time.
Plan
This will be a breaking change, i.e. Logbook 2.0
Remove
logbook.set_datetime_format
.Change
LogRecord.time
to always use aware datetimes. May wish to haveLogger
pass down a timezone which defaults totimezone.utc
.Add a
tzinfo
attribute toHandler
which doesrecord.time.astimezone(tzinfo)
if it is set toNone
ordatetime.tzinfo
. This allows having handlers convert to different timezones, similar tologging.Formatter.convert
. We'll need to make this work in the format string, since currently you dorecord.time
.Converting to a timezone is similar performance to the conversion
logging.Formatter
already does:The text was updated successfully, but these errors were encountered: