How to modify LOGGING settings in unittest

9 views
Skip to first unread message

panfei

unread,
Jun 10, 2019, 9:46:23 AM6/10/19
to Django users
There is a logger path for production environment, but I want to put the logs in /tmp/ in unittests.

This is the LOGGING settings for production environment:

LOGGING_DIR = '/data0/bridge_logs_test/'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '{levelname}\t{asctime}\t{pathname}\t{lineno:d}\t{process:d}\t{message}',
'style': '{'
}
},

'handlers': {
'console_log_handler': {
'level': 'ERROR',
'class': 'logging.StreamHandler',
},
'backup_log_handler': {
'level': 'INFO',
'class': 'logging.handlers.WatchedFileHandler',
'filename': os.path.join(LOGGING_DIR, 'put.log'),
'formatter': 'default',
},
        ......
},

'loggers': {
'django': {
'handlers': ['console_log_handler', ],
'level': 'DEBUG',
'propagate': True,
},
'backup': {
'handlers': ['backup_log_handler', ],
'level': 'INFO',
'propagate': False,
},
        ......
},
}

And in unittests, I write:

from django.test import TestCase
class FaceAITestCase(TestCase):
def test_function1(self):
with self.modify_settings(LOGGING_DIR="/tmp/test_logs/"):
pass

When run the unittests, it reports:
ValueError: Unable to configure handler 'access_log_handler': [Errno 2] No such file or directory: '/data0/bridge_logs_test/access.log'

And how to use /tmp/test_logs/ as the log directory in unittests ? any ideas ? Thanks.
--
不学习,不知道

vineeth sagar

unread,
Jun 10, 2019, 2:08:35 PM6/10/19
to django...@googlegroups.com
Try this with the override_settings decorator. like this.

@override_settings(LOGGING_DIR='/tmp/test_logs/'
def test_function1(self):
    pass

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BJstLD7nDPz_%2BCs203Eh2Qzk-A1wawOzbtA%3DpFE7Lb0FZBaeQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

panfei

unread,
Jun 10, 2019, 10:35:29 PM6/10/19
to Django users
Still get the same error message

vineeth sagar <vineeth...@gmail.com> 于2019年6月11日周二 上午2:08写道:

For more options, visit https://groups.google.com/d/optout.


--
不学习,不知道
Reply all
Reply to author
Forward
0 new messages