I have a setting for logging in my django project and I want to make a test using assertLogs.
I used the example provided in the documentation: https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertLogs
with self.assertLogs('foo', level='INFO') as cm:
logging.getLogger('foo').info('first message')
logging.getLogger('foo.bar').error('second message')
self.assertEqual(cm.output, ['INFO:foo:first message',
'ERROR:foo.bar:second message'])
My django settings are like below:
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
'standard': {
'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
},
},
'filters': {
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
},
},
'handlers': {
'console': {
'level': 'INFO',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
},
'default': {
'level':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename': BASE_DIR + '/logs/default.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter':'standard',
},
'debug_file': {
'level':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename': BASE_DIR + '/logs/debug.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter':'standard',
},
'warning_file': {
'level':'WARNING',
'class':'logging.handlers.RotatingFileHandler',
'filename': BASE_DIR + '/logs/warning.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter':'standard',
},
'info_file': {
'level':'INFO',
'class':'logging.handlers.RotatingFileHandler',
'filename': BASE_DIR + '/logs/info.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter':'standard',
},
'error_file': {
'level':'ERROR',
'class':'logging.handlers.RotatingFileHandler',
'filename': BASE_DIR + '/logs/error.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter':'standard',
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
},
'emails': {
'level':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename': BASE_DIR + '/logs/emails.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter':'standard',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'INFO',
},
'debug_logger': {
'handlers': ['debug_file'],
'level': 'DEBUG',
'propagate': True
},
'info_logger': {
'handlers': ['info_file'],
'level': 'INFO',
'propagate': True
},
'error_logger': {
'handlers': ['error_file'],
'level': 'ERROR',
'propagate': True
},
'django.request': {
'handlers': ['default', 'mail_admins'],
'level': 'ERROR',
'propagate': False,
},
'emails': {
'handlers': ['emails'],
'level': 'DEBUG',
'propagate': True
},
}
}
And my test:
from django.test import TestCase
import unittest
import logging
class LoggerTestCase(unittest.TestCase):
def test_message_logger(self):
with self.assertLogs('loggers', level='INFO') as logs:
logging.getLogger('debug_logger').debug('debug message')
logging.getLogger('info_logger').info('info message')
logging.getLogger('warning_logger').warning('warning message')
logging.getLogger('error_logger').error('error message')
logging.getLogger('critical_logger').critical('critical message')
self.assertEqual(logs.output, ['INFO:loggers.debug:debug message',
'ERROR:loggers.info:info message',
'WARNING:loggers.warning:warning message',
'ERROR:loggers.error:error message',
'CRITICAL:loggers.critical:critical message'
])
But I'm getting this error:
Traceback (most recent call last):
line 16, in test_message_logger
logging.getLogger('critical_logger').critical('critical message')
AssertionError: no logs of level INFO or higher triggered on loggers
I expect to use correctly the assertLogs. I already used my prints messages in my views and it worked fine, so how can i fix this error? I can't detect where is my mistake
Any help will be apreciate
Update
Replacing loggers
by critical_logger
I get this error:
Traceback (most recent call last):
File
line 19, in test_message_logger
'CRITICAL:loggers.critical:critical message'
AssertionError: Lists differ: ['CRITICAL:critical_logger:critical message'] != ['INFO:loggers.debug:debug message', 'ERROR[149 chars]age']
loggers
bycritical_logger
I get a different error, but I can understand what you're meaning, make a general assertLogs with only one logger it would be good, How could do that? – Chlo