Skip to content
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

"TIP: Don’t use f-strings in logging statements" - not true (or more context needed) #225

Open
xavdid opened this issue May 6, 2022 · 1 comment

Comments

@xavdid
Copy link

xavdid commented May 6, 2022

Location within the Book

  • Chapter or Appendix: 29
  • Section: 3
  • Subsection: 5

Description

The book includes a "TIP" box saying that:

As of Python 3.9, f-strings don’t work with loggers.

I don't believe this info box is accurate, nor was I able to find a source for that. I also did a (very basic) test to check:

# tested on both 3.9.10 and 3.10.0
import logging
l = logging.getLogger()
l.addHandler(logging.StreamHandler())
a = 3

l.error(f'a is {a}')
# prints "a is 3"

Possible Solutions

I think either:

  • the box is right and my little snippet works while actual logging doesn't
  • the box is mistaken and should be removed (or updated to say "as of version X.Y.Z, f-strings work in loggers")

Your full name so we can provide accurate credit within the book

David Brownman

@strokirk
Copy link

The good example could also be improved:

logger.debug('Only %d pints of ice cream left.' % pints_remaining)

This uses interpolation of the log message, but it's better practise to allow the logging module to do that interpolation for you.

logger.debug('Only %d pints of ice cream left.', pints_remaining)

There is a great flake8 plugin to check this automatically: https://github.com/globality-corp/flake8-logging-format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants