How to setup python.logging format using f-string style How to setup python.logging format using f-string style python-3.x python-3.x

How to setup python.logging format using f-string style


You can initialize logging.Formatter with argument style set to "{".

Example

formatter = logging.Formatter("{processName:<12} {message} ({filename}:{lineno})", style="{")

Available options for style are documented.

After this attach the formatter to a handler and add the handler to your desired logger(s).

If you are using logging.basicConfig you can set it with the following way:

logging.basicConfig(format="{processName:<12} {message} ({filename}:{lineno})", style="{")