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
First of all I want to thank you for this awesome library—it's truly outstanding! You're the best ❤️
I've discovered a bug ☹️
When using handlers in Loguru, the StandardSink allows log records to be written as LogRecord objects using logging.makeRecord.
Loguru introduces a unique way to define levels with logger.level(...) in the Core object. However, this method of saving levels doesn’t align with how Python's logging library handles levels internally. Specifically, the discrepancy causes an issue when Loguru-created log records are processed by logging.
Steps to Reproduce
Define custom log levels in Loguru using logger.level(...).
Use a handler to write the log record, leveraging logging.makeRecord.
Observe that levelname is incorrectly labeled in the resulting LogRecord object.
Root Cause
When creating a LogRecord, Python's logging library uses two fields: levelno and levelname. While Loguru's record['level'].no can populate levelno, the levelname field relies on logging's getLevelName function. Loguru stores level definitions in its Core object, which is incompatible with logging's method that relies on two dictionaries to store level names.
Consequently, when Loguru log levels aren’t added to logging's internal structures, getLevelName returns a default string, e.g., "Level {level_number}", since it can't find the corresponding name.
Suggested Solution
To improve compatibility, consider calling logging.addLevelName to register Loguru levels when adding a new level. This way, getLevelName will find the custom levels, and LogRecord will accurately reflect both levelno and levelname.
This solution isn’t perfect, but it will do the job until a way to pass the core parameter into StandardSink is available.
The text was updated successfully, but these errors were encountered:
First of all I want to thank you for this awesome library—it's truly outstanding! You're the best ❤️
I've discovered a bug☹️
When using handlers in Loguru, the
StandardSink
allows log records to be written asLogRecord
objects usinglogging.makeRecord
.Loguru introduces a unique way to define levels with
logger.level(...)
in the Core object. However, this method of saving levels doesn’t align with how Python'slogging
library handles levels internally. Specifically, the discrepancy causes an issue when Loguru-created log records are processed bylogging
.Steps to Reproduce
logger.level(...)
.logging.makeRecord
.levelname
is incorrectly labeled in the resultingLogRecord
object.Root Cause
When creating a
LogRecord
, Python's logging library uses two fields:levelno
andlevelname
. While Loguru'srecord['level'].no
can populatelevelno
, thelevelname
field relies on logging'sgetLevelName
function. Loguru stores level definitions in its Core object, which is incompatible with logging's method that relies on two dictionaries to store level names.Consequently, when Loguru log levels aren’t added to
logging
's internal structures,getLevelName
returns a default string, e.g., "Level {level_number}", since it can't find the corresponding name.Suggested Solution
To improve compatibility, consider calling
logging.addLevelName
to register Loguru levels when adding a new level. This way,getLevelName
will find the custom levels, andLogRecord
will accurately reflect bothlevelno
andlevelname
.This solution isn’t perfect, but it will do the job until a way to pass the core parameter into StandardSink is available.
The text was updated successfully, but these errors were encountered: