If our Django web application returns a 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 with Django and Python?
An alternative solution would be to ignore this line, but WARN to INFO would be preferred.
You can't do this easily, because the Django developers have decided that this particular event is important enough to be classed as a WARNING
.
However, you can convert it to INFO
when writing to a log, by adding an instance of a logging.Filter
subclass which changes the level in the LogRecord
from WARNING
to INFO
, or filters out the message altogether.
The filter should be added to any handler whose output should be changed in this way. This can be done using standard Django logging configuration mechanisms.
Firstly create logger filter
def change_404_level_to_INFO(record):
if record.status_code == 404:
record.levelname = 'INFO'
return True
After that in LOGGING
add this filter and set filter on logger
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'],
},
}
....
}
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