Skip to main content

A pre-commit script to make log lines lazzier

Project description

Lazy log formatter

Pre-commit hook to automatically detect and convert f-strings in Python code used in log calls to lazy log calls, following W1203 Pylint rule:

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

Usage

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

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

Options

  • --fix: Automatically fix f-strings used in log calls to lazy log calls.

Examples

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)

Current limitations

  • Only works with logging module. Other logging libraries might not work as expected, such as loguru.
  • Multi-line f-strings in the editor are not supported yet (they are simply ignored).

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.4.2.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

lazy_log_formatter-0.4.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lazy_log_formatter-0.4.2.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for lazy_log_formatter-0.4.2.tar.gz
Algorithm Hash digest
SHA256 cde1791b3543ea90aa47d2fe3e997b239880472eae8fd06fdb561af76a4488c8
MD5 0385f0342802c95c448a662d2e5780e1
BLAKE2b-256 e06d3f1f420cf5f0d587b601f17fcfed714b823463c97909be87fc01c4fcd784

See more details on using hashes here.

Provenance

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

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

Attestations:

File details

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

File metadata

File hashes

Hashes for lazy_log_formatter-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7b3ed23784ef8ad2a405231a66e735d6c051b34a46341a85e4eb1193c653f037
MD5 cf0a6d93e10a1d4e683bdcafc9cb4d87
BLAKE2b-256 1de38477781b0c4506502bb990e9d7383d7b975823e0c2fc7f894907a2d9c402

See more details on using hashes here.

Provenance

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

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

Attestations:

Supported by

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