-
Notifications
You must be signed in to change notification settings - Fork 767
Description
Hi
Bug
I'm seeing Loguru crash “Logging error in Loguru Handler”) when formatting an exception whose traceback contains a frame with tb_lineno == None:
Date,Content
"2026-02-05T17:39:09.865Z","'--- End of logging error ---"
"2026-02-05T17:39:09.865Z","TypeError: '<=' not supported between instances of 'int' and 'NoneType'"
"2026-02-05T17:39:09.865Z","^^^^^^^^^^^^^^^^^^^^^^^^^"
"2026-02-05T17:39:09.865Z","if 1 <= lineno <= len(lines):"
"2026-02-05T17:39:09.865Z","File ""/usr/lib64/python3.12/linecache.py"", line 31, in getline"
"2026-02-05T17:39:09.865Z","^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
"2026-02-05T17:39:09.865Z","source = linecache.getline(filename, lineno).strip()"
"2026-02-05T17:39:09.865Z","File ""/app/.venv/lib64/python3.12/site-packages/loguru/_better_exceptions.py"", line 211, in get_info"
"2026-02-05T17:39:09.865Z","^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
"2026-02-05T17:39:09.865Z","infos.append((get_info(tb.tb_frame, tb.tb_lineno), tb.tb_frame))"
"2026-02-05T17:39:09.865Z","File ""/app/.venv/lib64/python3.12/site-packages/loguru/_better_exceptions.py"", line 239, in _extract_frames"
"2026-02-05T17:39:09.865Z","^^^^^^^^^^^^^^^^^^^^^"
"2026-02-05T17:39:09.865Z","frames, final_source = self._extract_frames("
"2026-02-05T17:39:09.865Z","File ""/app/.venv/lib64/python3.12/site-packages/loguru/_better_exceptions.py"", line 454, in _format_exception"
"2026-02-05T17:39:09.865Z","yield from self._format_exception("
"2026-02-05T17:39:09.865Z","File ""/app/.venv/lib64/python3.12/site-packages/loguru/_better_exceptions.py"", line 422, in _format_exception"
"2026-02-05T17:39:09.865Z","yield from self._format_exception(value, tb, is_first=True, from_decorator=from_decorator)"
"2026-02-05T17:39:09.865Z","File ""/app/.venv/lib64/python3.12/site-packages/loguru/_better_exceptions.py"", line 573, in format_exception"
"2026-02-05T17:39:09.865Z","^^^^^^^^^^^^^^"
"2026-02-05T17:39:09.865Z","formatter_record[""exception""] = """".join(lines)"
"2026-02-05T17:39:09.865Z","File ""/app/.venv/lib64/python3.12/site-packages/loguru/_handler.py"", line 147, in emit"
"2026-02-05T17:39:09.865Z","Traceback (most recent call last):"
"2026-02-05T17:39:09.865Z","During handling of the above exception, another exception occurred:"
[Truncated]
"2026-02-05T17:39:08.861Z","Traceback (most recent call last):"
"2026-02-05T17:39:08.861Z","'--- Logging error in Loguru Handler #1 ---"
This ends up happening inside Loguru’s exception formatter (loguru/_better_exceptions.py) and bubbles via handler emit logic (loguru/_handler.py)
Expected
Loguru should log the exception (even if it can’t show a source line for frames with lineno=None).
Actual
Loguru prints a “Logging error in Loguru Handler …” and raises: TypeError: '<=' not supported between instances of 'int' and 'NoneType'
Environment
- Python: 3.12.x (Linux)
- loguru==0.7.3
- anyio==4.12.1
- uvloop==0.22.1
loguru_logger.add(
sink=sink_serializer,
format="{message}",
level=settings.log_level,
filter=_filter,
diagnose=False,
backtrace=False,
)
Reproduction
I have tried to make an MRE without success, it's pretty hard to reproduce unfortunately.
Notes
This appears to come from calling linecache.getline(filename, lineno) with lineno=None inside Loguru’s exception formatter.
Frames with lineno=None are known Python edge case on Python 3.12+ see python/cpython#139531 python/cpython#89726
I tried turning diagnose and backtrace on and off but it did not help.
Suggested fix
In loguru/_better_exceptions.py, we could guard lineno before calling linecache.getline(): if lineno is None (or not an int),we skip the lookup and proceed with an empty/unknown source line?
Thanks