-
Notifications
You must be signed in to change notification settings - Fork 711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KeyError in string format when curlies { } present #1008
Comments
Hi @crizCraig. Did the I'm not really sure how I can help you there. When arguments are passed to the logging call, Loguru uses them to format the message. If the arguments were not meant for formatting but the message contains curly braces, Python will complain. If you need the keyword argument to only populate the I'm planning to disable keyword arguments formatting in the future, though, which should help with the issue you're facing. |
Thanks @Delgan! Yes, the guardrails above worked. kwargs are populated by this @retry(
stop=stop_after_attempt(10),
wait=wait_random_exponential(multiplier=1, max=10),
reraise=True,
before_sleep=before_sleep_log(log, "ERROR"), # type: ignore
after=after_log(log, "ERROR"), # type: ignore
) which does this logger.log(
log_level,
f"Retrying {fn_name} " f"in {retry_state.next_action.sleep} seconds as it {verb} {value}.",
exc_info=local_exc_info,
) |
Perhaps the best thing here is for tenacity to not allow setting up retries with a loguru logger. Here's a little prototype of wrapping a loguru logger with std logging that might be better from loguru import logger as loguru_logging
import logging
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG)
class InterceptHandler(logging.Handler):
def emit(self, record):
# Get corresponding Loguru level if it exists
try:
level = loguru_logging.level(record.levelname).name
except ValueError:
level = record.levelno
# Find caller from where originated the logged message
frame, depth = logging.currentframe(), 2
while frame.f_code.co_filename == logging.__file__:
frame = frame.f_back
depth += 1
loguru_logger = loguru_logging.bind(_depth=depth, _frame=frame)
loguru_logger.log(level, record.msg)
# get root std logging object
std_root_logger = logging.getLogger()
# Add the interceptor handler to the standard logger
std_root_logger.addHandler(InterceptHandler())
std_root_logger.handlers.pop(0) # Remove the default handler added by logging.basicConfig
loguru_logging.info("This is from Loguru!")
logging.info("This is from standard logging!") |
Hey @crizCraig. Do you know what happened to the https://github.com/jd/tenacity project? It returns 404. 😬 |
Weird. Hopefully it's a temporary absence. I see the docs and pypi are
still up. https://tenacity.readthedocs.io/en/latest/
…On Mon, Oct 23, 2023 at 1:03 PM Delgan ***@***.***> wrote:
Hey @crizCraig <https://github.com/crizCraig>. Do you know what happened
to the https://github.com/jd/tenacity project? It returns 404. 😬
—
Reply to this email directly, view it on GitHub
<#1008 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABMH2KKSPZ4JFWH2M2HCPTYA3ERBAVCNFSM6AAAAAA6F7DDXCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZVHEZTGNRSHE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Thanks for the additional details, @crizCraig. It seems This issue is similar to another one in which I share a possible workaround, please take a look: #969 (comment) The |
Agreed, thanks @Delgan! |
I'm running into the same problem as #767 and used a similar fix, but one that still tries to do the formatting.
So my message is
And I basically get
KeyError: ...foo_json...
where foo_json is some JSON I'm logging with the problematic curlies.loguru version:
0.7.0
Thanks for the awesome library @Delgan !
The text was updated successfully, but these errors were encountered: