Skip to content

snok/printf-log-formatter

Repository files navigation

logo

printf-log-formatter

Automatically convert f-strings and str.format() syntax to printf-style strings.

In other words, this syntax

logger.error(f"{1}")
logger.error("{}".format(1))
logger.error("{foo}".format(foo=1))

is changed to

logger.error("%s", 1)
logger.error("%s", 1)
logger.error("%s", 1)

Motivation

Why would we want to do this? This article explains it pretty well.

Mainly it's useful for Python projects using Sentry's log integration.

Installation

You have two options for running this pre-commit hook:

Python hook

If you would like to install this using Python, run:

pip install printf-log-formatter

then set the pre-commit hook up using:

- repo: local
  hooks:
  - id: printf-log-formatter
    name: printf-log-formatter
    entry: printf-log-formatter
    language: system
    types: [ python ]
    args:
      - --log-level=error

Rust hook

If you're happy to compile the Rust version, you can use:

- repo: https://github.com/snok/printf-log-formatter
  rev: ''
  hooks:
    - id: printf-log-formatter
      args:
        - --log-level=error

I just want to downgrade loggers once

The Rust binary or Python package can also be run directly, like this:

printf-log-formatter $(find . -name "*.py") --log-level error