



# Lazy log formatter
Pre-commit hook to automatically detect and convert f-strings in Python code to
[printf-style](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting) 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`:
```yaml
- repo: https://github.com/dmar1n/lazy-log-formatter
rev: 0.7.0
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:
```sh
python -m src.cli
```
Check all Python files in two directories:
```sh
python -m src.cli src/ tests/
```
Fix issues in all Python files in a directory:
```sh
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:
```python
# Before
logger.info(f'Hello {name}')
# After
logger.info('Hello %s', name)
```
```python
# Before
logger.info(f'Hello {name} {surname}')
# After
logger.info('Hello %s %s', name, surname)
```
### Example in a Python class
```python
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
```
```bash
python src\cli.py tests\data
```
The output will be:
```text
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:
```python
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](https://loguru.readthedocs.io/en/stable/overview.html#lazy-evaluation-of-expensive-functions):
```python
logger.opt(lazy=True).debug("If sink level <= DEBUG: {x}", x=lambda: expensive_function(2**64))
```
Raw data
{
"_id": null,
"home_page": null,
"name": "lazy-log-formatter",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "code-quality, f-strings, lazy logging, linting, logging, pre-commit, pylint, W1203",
"author": "Daniel Mar\u00edn",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/fe/b6/a0f399c2282390d3045edc7969e3ca547d543bcf19ea4265aacef0278938/lazy_log_formatter-0.7.0.tar.gz",
"platform": null,
"description": " \n\n\n\n\n# Lazy log formatter\n\nPre-commit hook to automatically detect and convert f-strings in Python code to \n[printf-style](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting) logging calls,\nfollowing W1203 Pylint rule:\n\nhttps://pylint.readthedocs.io/en/stable/user_guide/messages/warning/logging-fstring-interpolation.html\n\n## Usage\n\nTo use it with pre-commit, add the following lines to your `.pre-commit-config.yaml`:\n\n```yaml\n- repo: https://github.com/dmar1n/lazy-log-formatter\n rev: 0.7.0\n hooks:\n - id: lazy-log-formatter\n args: ['--fix']\n```\n\n## Options\n\n- `--fix`: Automatically fix f-strings used in log calls to lazy log calls.\n- `DIR [DIR ...]`: One or more directories to search for Python files. If not specified, defaults to the current directory.\n\n## Examples\n\nCheck all Python files in the current directory and subdirectories:\n\n```sh\npython -m src.cli\n```\n\nCheck all Python files in two directories:\n\n```sh\npython -m src.cli src/ tests/\n```\n\nFix issues in all Python files in a directory:\n\n```sh\npython -m src.cli mydir --fix\n```\n\nIf the `--fix` option is used, the hook will convert f-strings in log calls to lazy log calls, as follows:\n\n```python\n# Before\nlogger.info(f'Hello {name}')\n\n# After\nlogger.info('Hello %s', name)\n```\n\n```python\n# Before\nlogger.info(f'Hello {name} {surname}')\n\n# After\nlogger.info('Hello %s %s', name, surname)\n```\n\n### Example in a Python class\n\n```python\nimport logging\nfrom datetime import datetime\n\n\ndef log_and_return_datetime():\n now = datetime.now()\n logging.info(f\"Current datetime: {now}\")\n return now\n\n\nclass DateTimeLogger:\n def __init__(self):\n self._logger = logging.getLogger(self.__class__.__name__)\n\n def log_datetime(self):\n now = datetime.now()\n self._logger.info(f\"Current datetime: {now}\")\n return now\n```\n\n```bash\npython src\\cli.py tests\\data\n```\n\nThe output will be:\n\n```text\nF-string in logging call at ...\\tests\\data\\test.py:8: f'Current datetime: {now}'\nF-string in logging call at ...\\tests\\data\\test.py:18: f'Current datetime: {now}'\nF-strings found and fixed in '...\\tests\\data\\test.py'.\n```\n\nAfter running the formatter, the code will be transformed to:\n\n```python\nimport logging\nfrom datetime import datetime\n\n\ndef log_and_return_datetime():\n now = datetime.now()\n logging.info(\"Current datetime: %s\", now)\n return now\n\n\nclass DateTimeLogger:\n def __init__(self):\n self._logger = logging.getLogger(self.__class__.__name__)\n\n def log_datetime(self):\n now = datetime.now()\n self._logger.info(\"Current datetime: %s\", now)\n return now\n```\n\n### Important\n\nOnly works with the native Python `logging` module. Other libraries, such as `loguru`, do not support lazy calls.\n\nFor `loguru`, see [Lazy evaluation of expensive functions](https://loguru.readthedocs.io/en/stable/overview.html#lazy-evaluation-of-expensive-functions):\n\n```python\nlogger.opt(lazy=True).debug(\"If sink level <= DEBUG: {x}\", x=lambda: expensive_function(2**64))\n```\n\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "A pre-commit script to make log lines lazzier",
"version": "0.7.0",
"project_urls": {
"Homepage": "https://github.com/dmar1n/lazy-log-formatter",
"Repository": "https://github.com/dmar1n/lazy-log-formatter"
},
"split_keywords": [
"code-quality",
" f-strings",
" lazy logging",
" linting",
" logging",
" pre-commit",
" pylint",
" w1203"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "15f6a74ed0d9f3bfb4a5cbfb8ff16bee2e31b164aeb5f6e9a31979a58666d634",
"md5": "cc2bb28a22f60911bee050d224d4119e",
"sha256": "b080329895b7499c83c56360813f74fa8805475ca0c9f26990aa72ef9c4a505f"
},
"downloads": -1,
"filename": "lazy_log_formatter-0.7.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cc2bb28a22f60911bee050d224d4119e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 11590,
"upload_time": "2025-07-27T09:25:30",
"upload_time_iso_8601": "2025-07-27T09:25:30.079993Z",
"url": "https://files.pythonhosted.org/packages/15/f6/a74ed0d9f3bfb4a5cbfb8ff16bee2e31b164aeb5f6e9a31979a58666d634/lazy_log_formatter-0.7.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "feb6a0f399c2282390d3045edc7969e3ca547d543bcf19ea4265aacef0278938",
"md5": "27dcdf6b18187686ccf12af405339522",
"sha256": "f5656247d7527f076bd52cf75c6f5e524e276f695d7081bc0d5d54160476d757"
},
"downloads": -1,
"filename": "lazy_log_formatter-0.7.0.tar.gz",
"has_sig": false,
"md5_digest": "27dcdf6b18187686ccf12af405339522",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 9987,
"upload_time": "2025-07-27T09:25:31",
"upload_time_iso_8601": "2025-07-27T09:25:31.420540Z",
"url": "https://files.pythonhosted.org/packages/fe/b6/a0f399c2282390d3045edc7969e3ca547d543bcf19ea4265aacef0278938/lazy_log_formatter-0.7.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-27 09:25:31",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dmar1n",
"github_project": "lazy-log-formatter",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "lazy-log-formatter"
}