Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curly brace style in python logging

First three paragraphs of this section state one might use {}-formatting for logging format string.

If you are using {}-formatting (str.format()), you can use {attrname} as the placeholder in the format string.

However, it does not work.

import logging

logging.basicConfig(level=logging.INFO, format='[{asctime}] {message}')
logging.info('foo')

Prints,

[{asctime}] {message}

What is wrong?

like image 899
archeuclid Avatar asked Sep 17 '26 23:09

archeuclid


1 Answers

You need to declare your formatting style:

import logging
logging.basicConfig(level=logging.INFO, format='[{asctime}] {message}', style='{')
logging.info('test')

Works fine:

[2020-03-24 14:44:02,214] test
like image 161
truth Avatar answered Sep 21 '26 11:09

truth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!