Open yuanlongliao opened 6 days 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")
from loguru import logger LOG_FORMAT = "{time:YYYY-MM-DD HH:mm:ss.SSS} | {level} | {extra} | {message}" logger.remove() logger.add(sys.stdout, format=LOG_FORMAT) logger.info(f"{'{'}", e = "extra1")
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: Traceback (most recent call last): File "/home/unitx/unitx_repos/util/util/log_test.py", line 114, in
logger.info(f"{'{'}", e = "extra1")
File "/home/unitx/miniconda3/envs/unified_dev/lib/python3.11/site-packages/loguru/_logger.py", line 2044, in info
self._log("INFO", False, __self._options, message, args, kwargs)
File "/home/unitx/miniconda3/envs/unified_dev/lib/python3.11/site-packages/loguru/_logger.py", line 2021, in _log
log_record["message"] = message.format(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Single '{' encountered in format string
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