I'm trying to configure logging for Django so that I had session_key in every line of log (if set). I think I found a way:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'request': {
'format': '%(asctime)s %(levelname)-8s [%(sessid)s] %(message)s',
},
},
'filters': {
'request': {
'()': 'yellowballs.yblogging.RequestFilter'
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'filters': ['request'],
'formatter': 'request',
}
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},
},
}
With the filter being defined like that:
class RequestFilter(logging.Filter):
def filter(self, record):
session_store = SessionStore()
record.sessid = session_store.session_key
return True
It seems to work well, but Django's logging its messages with it's default format:
2013-03-01 08:44:41,359 WARNING [None] Not Found: /
[01/Mar/2013 08:44:41] "GET / HTTP/1.1" 200 1962
How do I get the lines [01/Mar/2013 08:44:41] "GET / HTTP/1.1" 200 1962 to display in the same format as the rest of the log?
Is there a simple and pretty way to override the default format for all Django so that the log messages would be consistent across the whole project?
Override the default log format worked for me. It is required to stop the propagation.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
"verbose": {
"format": (
"%(levelname)s %(name)s %(message)s [PID:%(process)d:%(threadName)s]"
)
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'verbose',
},
},
'loggers': {
'django.server': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False, ### NOTE HERE !!!
}
}
}
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