Reputation: 608
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?
Upvotes: 3
Views: 3517
Reputation: 4675
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 !!!
}
}
}
Upvotes: 2
Reputation: 608
It seems that those lines are logged not by my application but by manage.py runserver
server. So there is no way of formatting them, instead logging the application logs into some files. That will remove those lines from application logs completely but that's okay since they don't belong there :)
Upvotes: 0
Reputation: 1365
I think you forgot to use your formatter:
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'filters': ['set_sessid'],
'formatter': 'request', // this
},
},
Upvotes: 0