snok / django-guid

Inject an ID into every log message from a Django request. ASGI compatible, integrates with Sentry, and works with Celery

Home Page:https://django-guid.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Change from f'strings' to %strings

JonasKs opened this issue · comments

Thanks to @BBT#8008 at the Django Discord for reporting this.

Three issues of f'strings' in logging:

  1. If interpolation fails, code will break because it's not handled by the logging module
  2. Tools like Sentry aggregate logs based on the log template string, interpolating before that will make them show up as different errors/messages, while they are actually the same
  3. Interpolation is done even if the message is not to be logged

So, changing from

logger.info(f'{given_guid} is not a valid GUID. New GUID is {new_guid}')

to

logger.info("%s is not a valid GUID. New GUID is %s", given_guid, new_guid)

Will ensure the opposites of the points above:

  1. If the interpolation fails, code will not break because it's handled by the logging module
  2. Tools like Sentry will behave correctly
  3. Interpolation is only done if the message is to be logged