Modified Internal log level cache for better interoperability #1106
+5
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In my work I've come accross situations, where a third-party package is using my Loguru logger to emit logs. I've noticed that many time these messages are formated (colored) differently from messages emited in my code. After a brief investigation it turned out the third-party package is calling the
log()
method like this:The
logging.INFO
variable is an integer constant20
. However, Loguru'sLogger._log()
method expects a string, such as"INFO"
. Not finding the integer20
in the internalcore.levels_lookup
cache results into a different formatting of the message:(The first message is logged through the third-party package, while the second is logged natively in my code)
It is not possible to solve this using a custom log filter, because in Loguru the log level is resolved internally and the
LogRecord.levels
information is not used.I believe passing the log level using the logging package's enum is a good and wide-spread practice and should be supported by Loguru.
I propose a simple solution: adding integer keys in additon to string keys to the internal log level cache. This effectivelly resolves the issue, increasing Loguru's interoperability with other packages without any modifications the the internal processing flow and logic.