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

Ensure that log messages use sprintf-style formatting, not format. #21

Open
bburan opened this issue Mar 21, 2018 · 10 comments
Open

Ensure that log messages use sprintf-style formatting, not format. #21

bburan opened this issue Mar 21, 2018 · 10 comments

Comments

@bburan
Copy link
Contributor

bburan commented Mar 21, 2018

Sprintf-style formatting is faster than str.format. Also, str.format always gets called even if the logging message never gets used, whereas sprintf-style formatting only gets invoked if the logging message is needed (e.g. it's getting sent to a file or the console).

@svdavid
Copy link
Contributor

svdavid commented Mar 21, 2018

can you point to an example of where you use the improved form?

@bburan
Copy link
Contributor Author

bburan commented Mar 26, 2018

log.debug('This is my error message called %s with %.02f error', name, error)

@svdavid
Copy link
Contributor

svdavid commented Mar 27, 2018

You can do that??? --without an explicit sprintf call? I agree, this makes a lot of sense.

@jacobpennington
Copy link
Contributor

Inside log statements you can, don't think it works anywhere else (unless some other package supports it obviously).

@neheller
Copy link
Contributor

I think that this grep command will find all of them

grep -ER $'(log|logging)\.(warn|info|exception|debug|log|critical|warning)\((\'|\").*(\'|\")\.format'

@bburan
Copy link
Contributor Author

bburan commented Apr 10, 2018 via email

@neheller
Copy link
Contributor

I'm not sure there's a simple way to achieve this. A pre-commit client side hook would be fairly easy to set up, but there's no way to enforce that all contributors enable it. GitHub has a web hook api which can detect pull requests, but we would need to set up a service somewhere that scrapes the added code and tests it.

@svdavid
Copy link
Contributor

svdavid commented Apr 10, 2018

Could it be built into a test function?

@neheller
Copy link
Contributor

neheller commented Apr 11, 2018

It could but unless I'm misunderstanding how you're using tests, this wouldn't prevent commits with the .format style either. If we're okay with making the check 'opt-in' then I think a pre-commit hook is the best option.

Here's a hook I threw together for this. It tests any added or modified line for the regex in question. If you want to implement it, just set it down as /.git/hooks/pre-commit (no file extension) in this repository and make sure it has executable permissions.

#!/bin/bash

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi


numbad=`git diff --cached $against | grep -E $'^\+.*(log|logging)\.(warn|info|exception|debug|log|critical|warning)\((\'|\").*(\'|\")\.format' | wc -l`

echo $numbad style errors found

# If you want to allow bad style, set this variable to true
allowbadstyle=$(git config --bool hooks.allowbadstyle)

if [ "$allowbadstyle" != "true" ] && [ $numbad -gt 0 ]
then
  git diff --cached $against | grep --color=auto -E $'^\+.*(log|logging)\.(warn|info|exception|debug|log|critical|warning)\((\'|\").*(\'|\")\.format'
  cat <<\EOF

Error: Using prohibited formatted string for logging

Example of incorrect usage:

logging.warn("This is an example {}".format(expl))

Example of correct usage:

logging.warn("This is an example %s", expl)

If you know what you are doing you can disable this check using:

  git config hooks.allowbadstyle true

EOF
  exit 1
fi

exit 0

@neheller
Copy link
Contributor

Found a bug in the above script, you should use the current (edited) version.

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

4 participants