Closed adhadse closed 3 months ago
When using a logging method with positional or keyword arguments, Loguru will try to format your message. Example:
logger.info("Value: {num}", num=42)
# Output: "Value: 42"
I suppose the KeyError
you're facing is due to these lines:
logger.error(
f"validation error {validation_error.errors()}",
extra_message=validation_error.errors(),
)
The message is a f-string already formatted, but Loguru will tries to format it again due to the extra_message
keyword argument. This likely can cause errors depending on the content of validation_error.errors()
.
Maybe try the following instead:
logger.error("validation error {extra_message}", extra_message=validation_error.errors())
Cool, I'll add this. Shouldn't loguru internally handle if the key doesn't exist in the string?
That's not really possible (would imply re-implementing parsing and formatting of str.format()
), but I plan to disable the automatic formatting of keyword arguments (since it leads to confusing errors like the one you faced, and because f-string are now prevalent).
Cool, got it! Thanks Delgan. I'll be closing this issue.
Receiving KeyError when using logger with no patching. Occuring in
loguru/_logger.py#L2021
Maybe similar to #1008.
The code is something like this:
the
another_logger
is just to view what's happening inside thelogger
before it errors out. You can create simple Pydantic model and try validating it in order to recreate this issue.A simple test in notebook gave me, "'type'" is expected in string to be formatted, but it doesn't exist. How is it possible?
Error: