Open yuanlongliao opened 1 month ago
this is expected and it's not caused by f-string
f"{'{'}"
is just "{"
and is not valid format string
the final logging message is generated from message.format(*args, **kwrags)
, so if you pass any args or kwargs to log, message need to be valid format string, which means you need to use {{
as {
, }}
as }
and {
}
must pair.
So you need to do this instead: logger.info(f"{'{{'}", e = "extra1")
This is indeed the case, but there is a more troublesome place is that in actual use, it is impossible to predict whether there is a '}' or '{' in the content that needs to be printed, and then use with f-string, and eventually there will be a similar situation: param = '11111111111111111111{2222222222222222222' logger.info(f"{param}", e = "extra1")
This is indeed the case, but there is a more troublesome place is that in actual use, it is impossible to predict whether there is a '}' or '{' in the content that needs to be printed, and then use with f-string, and eventually there will be a similar situation: param = '11111111111111111111{2222222222222222222' logger.info(f"{param}", e = "extra1")
if you can't make sure param is valid format just don't use it as message, logger.info("some message", param=param, e = "extra1")
I don't consider this as a bug per se, but I agree it's quite unfortunate.
Initially, keyword arguments were intended for formatting purposes only.
Then they evolved to capture contextual variables added to the record
as well.
I hadn't foreseen that these two uses would lead to inadvertent edge cases like this one. It was a design error.
Eventually, the automatic formatting from keyword arguments will be removed. However, it's not yet available, sorry.
When a string with '{' or '}' characters is printed in f-string format and the extra parameter is included, the following exception will be thrown
exception:
I have observed that other people have also encountered this problem before, may I ask whether this problem has been fixed? I switched to loguru from another library, so it's not practical to change it line by line