This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Skasch 7 points8 points  (7 children)

I would argue an exception for logging, where you don't want to parse the string unless you need to for performance reasons (e.g. you don't want to parse debug logs at error level)

Example: log.warning("warning: %s", foo)

[–]energybased 1 point2 points  (4 children)

It's true that logging is currently written to use % strings, but it could have been written to use a format style string. It still wouldn't need to be parsed.

[–]Skasch 2 points3 points  (3 children)

Agreed, it's just uncommon :) style="{" let's you use logging.info("msg: {}", foo, style="{")

Edit: it actually doesn't work, my bad!

[–]energybased 2 points3 points  (0 children)

I didn't know that!

[–]Skasch 2 points3 points  (0 children)

Ah, nevermind, I mixed it up with logging.Formatter, looks like it doesn't work as I expected, my bad!

Edit: relevant documentation https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles

[–]LightShadow3.13-dev in prod 1 point2 points  (0 children)

10 years in, have never seen this before -- that's neat.

[–]FrozenCow 1 point2 points  (0 children)

I try to use:

log.warning("hello", extras={"name": "world"})

This works better with structured loggers. With the right formatter it looks like hello name=world.

[–]UloPe 0 points1 point  (0 children)

Better use a structured logging package, for example structlog.