Python Logging: change "WARN" to "INFO"

If our Django web application returns 404, we see this in the logs:

2017-11-21 12:48:26 django.request.get_response: WARNING Not Found: /foooooo 

I would like to change this particular line created by get_response () from WARN to INFO.

How to configure this using Django and Python?

An alternative solution would be to ignore this line, but WARN would be preferable to INF.

+7
python django logging
source share
2 answers

You cannot do this easily because the Django developers decided that this particular event was important enough to be classified as WARNING .

However, you can convert it to INFO when logging by adding an instance of the logging.Filter subclass that changes the level in LogRecord from WARNING to INFO or filters out the message at all.

A filter must be added to any handler whose output must be changed in this way. This can be done using standard Django log tuning mechanisms.

+10
source share

First create a registration filter

 def change_404_level_to_INFO(record): if record.status_code == 404: record.levelname = 'INFO' return True 

After that, add this filter to LOGGING and set the filter to the log

 LOGGING = { .... 'filters': { 'change_404_to_INFO': { '()': 'django.utils.log.CallbackFilter', 'callback': change_404_to_INFO`, }, }, .... 'loggers': { 'django.request': { 'handlers': ['console',], 'level': 'DEBUG', 'propagate: False, 'filters': ['change_404_to_INFO'], }, } .... } 
+3
source share

All Articles