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:
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']
--fix
: Automatically fix f-strings used in log calls to lazy log calls.
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)
- Only works with
logging
module. Other logging libraries might not work as expected, such asloguru
. - 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))