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

Logging anti-patterns #129

Open
amorgun opened this issue Jul 25, 2019 · 0 comments
Open

Logging anti-patterns #129

amorgun opened this issue Jul 25, 2019 · 0 comments

Comments

@amorgun
Copy link

amorgun commented Jul 25, 2019

  1. Using print instead of loging
  2. Using root logger by logging.info or logging.getLogger() instead of logging.getLogger(__name__)
  3. Configuring a logger on import
    E.g.
import logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler(sys.stdout))

if __name__ == '__main__':
    main()

This often happens after replacing print with logging
4. Using logging.getLogger(__file__) instead of logging.getLogger(__name__)
5. Sharing a logger across multiple files
E.g.

# foo.py
LOGGER = logging.getLogger(__name__)

# bar.py
from foo import LOGGER
  1. Unnecessary calculations of logging arguments
# Bad
logger.debug('Message with %s', expensive_func())

# Good
if logger.isEnabledFor(logging.DEBUG):
    logger.debug('Message with %s', expensive_func())

Docs: https://docs.python.org/3/howto/logging.html#optimization
7. Not using built-in string formatting features

# Bad
logger.info('Some data: %s', repr(data))

# Good
logger.info('Some data: %r', data)
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

1 participant