Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logger not printing out 'extra' dictionary info [closed]

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:
like image 431
Eric Thomas Avatar asked Jun 17 '16 17:06

Eric Thomas


1 Answers

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'

  • You may find some other useful info here.
like image 114
dot.Py Avatar answered Oct 04 '22 23:10

dot.Py