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.6.1
  hooks:
    - id: lazy-log-formatter
      args: ['--fix']

Options

  • --fix: Automatically fix f-strings used in log calls to lazy log calls.
  • DIR [DIR ...]: One or more directories to search for Python files. 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/

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)

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.6.1.tar.gz (9.4 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.6.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for lazy_log_formatter-0.6.1.tar.gz
Algorithm Hash digest
SHA256 126496434b26c27957c3e7e9e4e4c83322747d662bfe6b0adce52312b045444c
MD5 1e7c6cbebcad76349ec8332113e4c9cd
BLAKE2b-256 325a6435b668f4ff3b0dce81d7abe91ce28d1d08a72474eecea3f29e685c7358

See more details on using hashes here.

Provenance

The following attestation bundles were made for lazy_log_formatter-0.6.1.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.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for lazy_log_formatter-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 427b2f7077aeee13602060a2efce8ee0d719e09fe773d69d8e4da6d0156d4a98
MD5 44a9008ff70ea0bf275db1a78faf367a
BLAKE2b-256 8a7aa7ca1ea65ad0d6382af5affa45feda0f8a7daf85d507e9b702a37f0d027a

See more details on using hashes here.

Provenance

The following attestation bundles were made for lazy_log_formatter-0.6.1-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