Skip to content

make logging calls more efficient #89

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

Closed
wholmgren opened this issue Oct 25, 2015 · 0 comments · Fixed by #92
Closed

make logging calls more efficient #89

wholmgren opened this issue Oct 25, 2015 · 0 comments · Fixed by #92
Assignees
Labels
Milestone

Comments

@wholmgren
Copy link
Member

I recently learned that I've been using the logging module incorrectly. The problem is that I've been using the pattern

logger.debug('array of numbers that you rarely want to see: {}'.format(numbers))

Regardless of the logging level, numbers will be formatted into a string. This is because the string formatting is part of the input to the logging function, so it happens before the logging module has a chance to decide if it actually needs to do the conversion and print the output.

The correct way to do it is

logger.debug('array of numbers that you rarely want to see: %s', numbers)

Now numbers is only converted into a string when the logging level is set to debug.

This only makes a measurable impact in function time in a few places, but I want to change it throughout the library so that people don't learn bad habits from my examples.

I had some utility functions in some other code that spent 75% of their time doing string formatting for rare logging calls instead of doing real work. Embarrassing.

@wholmgren wholmgren added the bug label Oct 25, 2015
@wholmgren wholmgren added this to the 0.3 milestone Oct 25, 2015
@wholmgren wholmgren self-assigned this Oct 25, 2015
This was referenced Oct 26, 2015
@wholmgren wholmgren modified the milestones: 0.2.2, 0.3 Mar 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant