Skip to main content

A Flake8 plugin to improve logging.

Project description

https://img.shields.io/github/actions/workflow/status/adamchainz/flake8-logging/main.yml?branch=main&style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/flake8-logging.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

A Flake8 plugin to improve logging.

Requirements

Python 3.8 to 3.12 supported.

Installation

First, install with pip:

python -m pip install flake8-logging

Second, if you define Flake8’s select setting, add the L prefix to it. Otherwise, the plugin should be active by default.


Linting a Django project? Check out my book Boost Your Django DX which covers Flake8 and many other code quality tools.


Rules

L001: use logging.getLogger() to instantiate loggers

The Logger Objects documentation section starts:

Note that Loggers should NEVER be instantiated directly, but always through the module-level function logging.getLogger(name).

Directly instantiated loggers are not added into the logger tree, so their messages are discarded. This means you’ll never see any messages on such loggers. Use getLogger() to correctly instantiate loggers.

This rule detects any module-level calls to Logger().

Failing example:

import logging

logger = logging.Logger(__name__)

Corrected:

import logging

logger = logging.getLogger(__name__)

L002: use __name__ with getLogger()

The logging documentation recommends this pattern:

logging.getLogger(__name__)

__name__ is the fully qualified module name, such as camelot.spam, which is the intended format for logger names.

This rule detects probably-mistaken usage of similar module-level dunder constants:

  • __cached__ - the pathname of the module’s compiled versio˜, such as camelot/__pycache__/spam.cpython-311.pyc.

  • __file__ - the pathname of the module, such as camelot/spam.py.

Failing example:

import logging

logger = logging.getLogger(__file__)

Corrected:

import logging

logger = logging.getLogger(__name__)

L003: extra key '<key>' clashes with LogRecord attribute

The extra documentation states:

The keys in the dictionary passed in extra should not clash with the keys used by the logging system.

Such clashes crash at runtime with an error like:

KeyError: "Attempt to overwrite 'msg' in LogRecord"

Unfortunately, this error is only raised if the message is not filtered out by level. Tests may therefore not encounter the check, if they run with a limited logging configuration.

This rule detects such clashes by checking for keys matching the LogRecord attributes.

Failing example:

import logging
logger = logging.getLogger(__name__)

response = acme_api()
logger.info("ACME Response", extra={"msg": response.msg})

Corrected:

import logging
logger = logging.getLogger(__name__)

response = acme_api()
logger.info("ACME Response", extra={"response_msg": response.msg})

L004: avoid logger.exception() outside of except clauses

The exception() documentation states:

This function should only be called from an exception handler.

Calling exception() outside of an exception handler attaches None exception information, leading to confusing messages:

>>> logging.exception("example")
ERROR:root:example
NoneType: None

Use error() instead. To log a caught exception, pass it in the exc_info argument.

This rule detects exception() calls outside of exception handlers.

Failing example:

import logging

response = acme_api()
if response is None:
    logging.exception("ACME failed")

Correct example:

import logging

response = acme_api()
if response is None:
    logging.error("ACME failed")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8_logging-1.0.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_logging-1.0.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file flake8_logging-1.0.1.tar.gz.

File metadata

  • Download URL: flake8_logging-1.0.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for flake8_logging-1.0.1.tar.gz
Algorithm Hash digest
SHA256 0c5dd8210c75adcafe02ae9bd22e756bbbf3212fc5c6823d4fe3fd850c914da6
MD5 c891d090bc8b7477fcc3d4c881e8c017
BLAKE2b-256 2249b514b0f362f1aa2905c0f507684dd9cc3f4f10b5d159127d95b90556e3e3

See more details on using hashes here.

File details

Details for the file flake8_logging-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: flake8_logging-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for flake8_logging-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d33e28bea1e9cef204385469db8fe83fafa8b364ca8a48b46c72d27d4ac4096c
MD5 df0897bc3bb11e0cb396419ee03d21ce
BLAKE2b-256 a8ca098c26597fa7dd70d61aa9f6227a80d64db9cdd3f4c6421c4ca119ca56e9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page