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 to printf-style logging calls, following W1203 Pylint rule:
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74540f100198f6577d795737016b27ac7793f245341ae3a6fe87f652789f87c0
|
|
| MD5 |
972950598654b60b8a76cdb71c26e4e7
|
|
| BLAKE2b-256 |
bfb65205b4559c3651e8d0e9f1b39fd11202794350ec979b318811fae185d7e3
|
Provenance
The following attestation bundles were made for lazy_log_formatter-0.8.2.tar.gz:
Publisher:
release.yaml on dmar1n/lazy-log-formatter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lazy_log_formatter-0.8.2.tar.gz -
Subject digest:
74540f100198f6577d795737016b27ac7793f245341ae3a6fe87f652789f87c0 - Sigstore transparency entry: 664139498
- Sigstore integration time:
-
Permalink:
dmar1n/lazy-log-formatter@808548fefb26781bf4d37dfd3067079cfedfebc0 -
Branch / Tag:
refs/tags/0.8.2 - Owner: https://github.com/dmar1n
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@808548fefb26781bf4d37dfd3067079cfedfebc0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file lazy_log_formatter-0.8.2-py3-none-any.whl.
File metadata
- Download URL: lazy_log_formatter-0.8.2-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae00be94816e3e56dad3c4f575f583bde73fb696a65bf33aaf09bcf625cde626
|
|
| MD5 |
f7568500f2eac6895c966a34382eb7b3
|
|
| BLAKE2b-256 |
386377bc989ca21d4945d7952c5932ce3f9c0c4357711d84681cafc1b4851db6
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lazy_log_formatter-0.8.2-py3-none-any.whl -
Subject digest:
ae00be94816e3e56dad3c4f575f583bde73fb696a65bf33aaf09bcf625cde626 - Sigstore transparency entry: 664139519
- Sigstore integration time:
-
Permalink:
dmar1n/lazy-log-formatter@808548fefb26781bf4d37dfd3067079cfedfebc0 -
Branch / Tag:
refs/tags/0.8.2 - Owner: https://github.com/dmar1n
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@808548fefb26781bf4d37dfd3067079cfedfebc0 -
Trigger Event:
push
-
Statement type: