英文:
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 work 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
英文:
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
答案1
得分: 1
要使用居中格式,您需要通过指定format
参数切换到f-string(又名花括号)格式:
logging.Formatter('[{levelname:^9}] {name} | Method : {funcName} | Line : {lineno} | MSG : {message}', style='{')
英文:
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='{')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论