In my python code I pass a dictionary into the logger for example.
extra_info = {"test":"data"}
logger.info("",extra=extra_info)
My hope is to get the extra info to print and I assumed it was in the message but when I use this format
format=%(asctime)s %(levelname)s %(name)s: %(message)s
The extra_info content are not being printed. I just get the following
2016/06/17 13:10:47 INFO test:
According to the logging docs
, the extra
keyword argument is used to add additional context to log records.
You can use info passed via extra
parameter in the formatter by adding the dict key:
format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s - %(EXTRA_DICT_KEY)s'
Note that if you set up formatter like this and forget to pass the dict key, you'll get a string formatting exception.
As you can see in your example, you haven't specified any key in the formatter.
So, instead of:
format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s'
Try:
format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s - %(test)s'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With