Skip to main content

A pre-commit script to make log lines lazzier

Project description

PyPI - Version GitHub Actions Workflow Status GitHub License PyPI - Python Version

Lazy log formatter

Pre-commit hook to automatically detect and convert f-strings in Python code to printf-style logging calls, following W1203 Pylint rule:

https://pylint.readthedocs.io/en/stable/user_guide/messages/warning/logging-fstring-interpolation.html

Usage

To use it with pre-commit, add the following lines to your .pre-commit-config.yaml:

- repo: https://github.com/dmar1n/lazy-log-formatter
  rev: 0.8.2
  hooks:
    - id: lazy-log-formatter
      args: ['--fix']

Options

  • --fix: Automatically fix f-strings used in log calls to lazy log calls.
  • PATH [PATH ...]: One or more directories or files to process. If not specified, defaults to the current directory.

Examples

Check all Python files in the current directory and subdirectories:

python -m src.cli

Check all Python files in two directories:

python -m src.cli src/ tests/

Check specific files:

python -m src.cli src/cli.py tests/test_cli.py

Fix issues in all Python files in a directory:

python -m src.cli mydir --fix

If the --fix option is used, the hook will convert f-strings in log calls to lazy log calls, as follows:

# Before
logger.info(f'Hello {name}')

# After
logger.info('Hello %s', name)
# Before
logger.info(f'Hello {name} {surname}')

# After
logger.info('Hello %s %s', name, surname)

Example in a Python class

import logging
from datetime import datetime


def log_and_return_datetime():
    now = datetime.now()
    logging.info(f"Current datetime: {now}")
    return now


class DateTimeLogger:
    def __init__(self):
        self._logger = logging.getLogger(self.__class__.__name__)

    def log_datetime(self):
        now = datetime.now()
        self._logger.info(f"Current datetime: {now}")
        return now
python src\cli.py tests\data

The output will be:

F-string in logging call at ...\tests\data\test.py:8: f'Current datetime: {now}'
F-string in logging call at ...\tests\data\test.py:18: f'Current datetime: {now}'
F-strings found and fixed in '...\tests\data\test.py'.

After running the formatter, the code will be transformed to:

import logging
from datetime import datetime


def log_and_return_datetime():
    now = datetime.now()
    logging.info("Current datetime: %s", now)
    return now


class DateTimeLogger:
    def __init__(self):
        self._logger = logging.getLogger(self.__class__.__name__)

    def log_datetime(self):
        now = datetime.now()
        self._logger.info("Current datetime: %s", now)
        return now

Important

Only works with the native Python logging module. Other libraries, such as loguru, do not support lazy calls.

For loguru, see Lazy evaluation of expensive functions:

logger.opt(lazy=True).debug("If sink level <= DEBUG: {x}", x=lambda: expensive_function(2**64))

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

lazy_log_formatter-0.8.2.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

lazy_log_formatter-0.8.2-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file lazy_log_formatter-0.8.2.tar.gz.

File metadata

  • Download URL: lazy_log_formatter-0.8.2.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lazy_log_formatter-0.8.2.tar.gz
Algorithm Hash digest
SHA256 74540f100198f6577d795737016b27ac7793f245341ae3a6fe87f652789f87c0
MD5 972950598654b60b8a76cdb71c26e4e7
BLAKE2b-256 bfb65205b4559c3651e8d0e9f1b39fd11202794350ec979b318811fae185d7e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for lazy_log_formatter-0.8.2.tar.gz:

Publisher: release.yaml on dmar1n/lazy-log-formatter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lazy_log_formatter-0.8.2-py3-none-any.whl.

File metadata

File hashes

Hashes for lazy_log_formatter-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ae00be94816e3e56dad3c4f575f583bde73fb696a65bf33aaf09bcf625cde626
MD5 f7568500f2eac6895c966a34382eb7b3
BLAKE2b-256 386377bc989ca21d4945d7952c5932ce3f9c0c4357711d84681cafc1b4851db6

See more details on using hashes here.

Provenance

The following attestation bundles were made for lazy_log_formatter-0.8.2-py3-none-any.whl:

Publisher: release.yaml on dmar1n/lazy-log-formatter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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