Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tzinfo utcoffset error #1260

Merged
merged 1 commit into from
Dec 31, 2024

Conversation

bijlpieter
Copy link
Contributor

Hey all, thank you for the wonderful library!

Since 0.7.3 I started running into tons of errors:

Traceback (most recent call last):
  File "xxx\loguru\_handler.py", line 184, in emit
    formatted = precomputed_format.format_map(formatter_record)
  File "xxx\loguru\_datetime.py", line 140, in __format__
    return _compile_format(fmt)(self)
           ~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "xxx\loguru\_datetime.py", line 23, in _loguru_datetime_formatter
    args = tuple(f(t, dt) for f in formatters)
  File "xxx\loguru\_datetime.py", line 23, in <genexpr>
    args = tuple(f(t, dt) for f in formatters)
                 ~^^^^^^^
  File "xxx\loguru\_datetime.py", line 106, in <lambda>
    "Z": ("%s", lambda t, dt: _format_timezone(dt.tzinfo or timezone.utc, sep=":")),
                              ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "xxx\loguru\_datetime.py", line 40, in _format_timezone
    offset = tzinfo.utcoffset(None).total_seconds()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'total_seconds'

Like #856 is requesting, I'm "manually" adding a configurable timezone to my logging:

def tzconverter(record):
    record["time"] = record["time"].astimezone(tz=ZoneInfo("..."))

logger.configure(patcher=tzconverter)

The issue seems to be that the new _format_timezone calls tzinfo.utcoffset with None. And apparently zoneinfo.ZoneInfo objects return None for that input. I think the proper way to handle this would be to instead call tzinfo.utcoffset with the current time.

This PR implements that!

@bijlpieter
Copy link
Contributor Author

Code coverage CI seems unrelated to my change... let me know if I should have another look

@Delgan Delgan merged commit dfe9607 into Delgan:master Dec 31, 2024
20 checks passed
@Delgan
Copy link
Owner

Delgan commented Dec 31, 2024

Oh, thanks a lot for fixing that!

Not sure why the implementation was changed from .utcoffset(dt) to .utcoffset(None) recently, it must have been an oversight. I'll add a few unit tests to avoid any regression in the future.

As a workaround for now, I think the tzconverter() patching function can be updated as follow:

def tzconverter(record):
    zone = ZoneInfo("America/Los_Angeles")
    date = record["time"]
    seconds = zone.utcoffset(date).total_seconds()
    tzinfo = timezone(timedelta(seconds=seconds), zone.tzname(date))
    record["time"] = record["time"].astimezone(tz=tzinfo)

@bijlpieter
Copy link
Contributor Author

Fantastic Delgan, much appreciated, and happy coding :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants