Skip to content

Logging anti-patterns #129

@amorgun

Description

@amorgun
  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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions