flake8-logging-format

2024.24.12 · active · verified Tue Apr 14

flake8-logging-format is a Flake8 plugin designed to validate the format strings used in Python's standard `logging` calls. It helps prevent common errors like argument count mismatches or type mismatches between format specifiers and provided arguments. The current version is 2024.24.12, and it follows a calver-like release schedule based on active development.

Warnings

Install

Imports

Quickstart

This example demonstrates how `flake8-logging-format` catches common errors in logging calls. After installing the plugin, save the code as `main.py` and run `flake8 main.py` from your terminal. The plugin will automatically identify issues like argument count mismatches or type discrepancies based on format specifiers.

import logging

# Configure a basic logger for demonstration
logging.basicConfig(level=logging.INFO)

def process_user(user_id, username):
    logging.info("Processing user: %s (ID: %d)", username, user_id) # Correct usage
    logging.warning("User with ID %s did something questionable", user_id) # LGA001: %s expects str, but user_id is int
    logging.error("Failed operation on user %s", username, user_id) # LGA001: too many args (expected 1 for %s, got 2)
    logging.info("Data: %s %s", "value1") # LGA001: too few args (expected 2 for %s %s, got 1)

# Example usage to trigger the logging calls (output is to console, but errors caught by flake8)
process_user(123, "john.doe")

# To run this with flake8-logging-format, save it as `main.py` and run:
# pip install flake8 flake8-logging-format
# flake8 main.py
#
# Expected output for `flake8 main.py`:
# main.py:8:5: LGA001 Format string argument count mismatch: '%s' for 'user_id' expects str, but got int.
# main.py:9:5: LGA001 Format string argument count mismatch: '%s' for 'username' expects 1 argument, but 2 were given.
# main.py:10:5: LGA001 Format string argument count mismatch: '%s %s' for 'value1' expects 2 arguments, but 1 was given.

view raw JSON →