在日志记录中格式化字符串 – 模块

huangapple go评论68阅读模式
英文:

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='{') 

huangapple
  • 本文由 发表于 2023年7月4日 20:37:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定