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]: from time import localtime, gmtime
In [2]: from datetime import datetime, timezone
In [3]: from zoneinfo import ZoneInfo
In [4]: now = datetime.now(timezone.utc)
In [5]: nowts = now.timestamp()
In [6]: berlin = ZoneInfo('Europe/Berlin')
In [7]: %timeit localtime(nowts)
331 ns ± 0.701 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [8]: %timeit gmtime(nowts)
218 ns ± 0.35 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [9]: %timeit now.astimezone(berlin)
195 ns ± 0.657 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [10]: %timeit now.astimezone(timezone.utc)
45 ns ± 0.104 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
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
logbook.set_datetime_format
.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: