So, I am trying to create a logging system for my Django Project where I need to save all the different log level messages to different files.
TLDR,
I managed to get the Logs for particular level to appear in their respective files, but
Debug.log contains all the log level messages
Info.log contains all log level messages leaving debug
warning.log contains WARN, ERROR & CRITICAL log level messages
error.log contains ERROR & CRITICAL log level messages
critical.log contains just CRITICAL log level messages
So, I followed the official https://docs.djangoproject.com/en/2.2/topics/logging/
Got a clearer picture from this Django rest framework logging different levels on different files
Then wrote the following code.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'debug_logs': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': 'logs/debug.log',
'formatter': 'verbose',
},
'error_logs': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': 'logs/error.log',
'formatter': 'verbose',
},
'warn_logs': {
'level': 'WARN',
'class': 'logging.FileHandler',
'filename': 'logs/warn.log',
'formatter': 'verbose',
},
'info_logs': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': 'logs/info.log',
'formatter': 'verbose',
},
'critical_logs': {
'level': 'CRITICAL',
'class': 'logging.FileHandler',
'filename': 'logs/critical.log',
'formatter': 'verbose',
},
},
'loggers': {
'': {
'handlers': ['error_logs', 'warn_logs', 'info_logs', 'critical_logs', 'debug_logs'],
'level': 'DEBUG',
'propagate': True,
},
},
}
So, now the result that i am getting is as follows,
debug.log
INFO 2019-05-02 05:36:22,888 autoreload 1683 4558792128 Watching for file changes with StatReloader ERROR 2019-05-02 05:36:26,604 getQuestions 1683 123145336807424 error! DEBUG 2019-05-02 05:36:26,604 getQuestions 1683 123145336807424 debug INFO 2019-05-02 05:36:26,604 getQuestions 1683 123145336807424 info WARNING 2019-05-02 05:36:26,605 getQuestions 1683 123145336807424 warning CRITICAL 2019-05-02 05:36:26,605 getQuestions 1683 123145336807424 critical
info.log
INFO 2019-05-02 05:36:22,888 autoreload 1683 4558792128 Watching for file changes with StatReloader ERROR 2019-05-02 05:36:26,604 getQuestions 1683 123145336807424 error! INFO 2019-05-02 05:36:26,604 getQuestions 1683 123145336807424 info WARNING 2019-05-02 05:36:26,605 getQuestions 1683 123145336807424 warning CRITICAL 2019-05-02 05:36:26,605 getQuestions 1683 123145336807424 critical
warn.log
ERROR 2019-05-02 05:36:26,604 getQuestions 1683 123145336807424 error! WARNING 2019-05-02 05:36:26,605 getQuestions 1683 123145336807424 warning CRITICAL 2019-05-02 05:36:26,605 getQuestions 1683 123145336807424 critical
error.log
ERROR 2019-05-02 05:36:26,604 getQuestions 1683 123145336807424 error! CRITICAL 2019-05-02 05:36:26,605 getQuestions 1683 123145336807424 critical
critical.log
CRITICAL 2019-05-02 05:36:26,605 getQuestions 1683 123145336807424 critical