pythonpython-logginglevels

Formatting String in logging - module


i have one question concerning the logging module. I actually try to create my own handler and when i'm formatting the strings using "Logging.Formatter()", i have the following problem :

The output should look like this :

[ WARNING ] Someething Not Expected Happened Or Indicative To Prospective Problems !
[  DEBUG  ] Diagnosing Supposes !
[  ERROR  ] Serious Problems !

The levelname should be centered quoted by brackets.

But i didn't worked out, how to do this without breaking the formatation.

My formatting string is :

logging.Formatter( f'[ %(levelname)s ] %(name)s | Method : %(funcName)s | Line : %(lineno)s | MSG : %(message)s' )

If i try to use the usual formatter like [{'WARNING':^9}], it doesn't work :/

Like :

[ "%(levelname)s":^9 ]

I hope you can help me solving this problem.

Best regards


Solution

  • In order to use center formatting, you need to switch to f-string (aka brace) formatting by specifying the format paramter:

    logging.Formatter('[{levelname:^9}] {name} | Method : {funcName} | Line : {lineno} | MSG : {message}', style='{')