2013-06-10 1 views
2

내 테스트 스위트에서 개발시 사용하는 로그 파일과 다른 로그 파일을 만들려고하는데 어떤 이유로 override_settings 데코레이터가 작동하지 않는 것 같습니다. 테스트를 실행하면 같은 'project/project/debug_logfile'이 작성됩니다. 내가 어디에서 엉망 이냐?Django : 테스트 중에 logfile 변수를 어떻게 재정의합니까?

# settings.py 
... 
LOGFILE = ROOT + '/debug_logfile' 

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:%(lineno)s] %(message)s", 
      'datefmt' : "%d/%b/%Y %H:%M:%S" 
     }, 
    }, 
    'filters': { 
     'require_debug_false': { 
      '()': 'django.utils.log.RequireDebugFalse' 
     } 
    }, 
    'handlers': { 
     'null': { 
      'level': 'DEBUG', 
      'class': 'django.utils.log.NullHandler', 
     }, 
     'console': { 
      'level': 'DEBUG', 
      'class': 'logging.StreamHandler', 
      'formatter': 'simple' 
     }, 
     'mail_admins': { 
      'level': 'ERROR', 
      'class': 'django.utils.log.AdminEmailHandler', 
      'filters': ['require_debug_false'] 
     }, 
     'logfile': { 
      'level':'DEBUG', 
      'class':'logging.handlers.RotatingFileHandler', 
      'filename': LOGFILE, 
      'maxBytes': 50000, 
      'backupCount': 2, 
      'formatter': 'standard', 
     }, 
    }, 
    'loggers': { 
     'django': { 
      'handlers': ['null'], 
      'propagate': True, 
      'level': 'INFO', 
     }, 
     'django.request': { 
      'handlers': ['mail_admins'], 
      'level': 'ERROR', 
      'propagate': False, 
     }, 
     'clients': { 
      'handlers': ['console', 'logfile'], 
      'level': 'DEBUG', 
     }, 
     # display the db queries 
     #'django.db.backends': { 
     # 'handlers': ['console'], 
     # 'level': 'DEBUG', 
     #} 
    } 
} 

# clients.management.commands.remindmanagers.py 

class Command(BaseCommand): 
    help = 'Creates task reminders and send emails to account and senior \ 
      managers when their client\'s contracts are about to expire' 

    def handle(self, *args, **kwargs): 
     three_months = datetime.date.today() + datetime.timedelta(days=90) 
     # get all contracts that will be completed in the next 3 months 
     contracts = Contract.objects.filter(finish__lte=three_months 
            ).exclude(notices_left=0) 

     if not contracts.exists(): 
      log.info('Tried running but there are no contracts about to expire.') 
      return 0 
     ... 

# tests.test_clients 

... 
from django.core.management import call_command 

@override_settings(LOGFILE=settings.ROOT + "/test_logfile") 
class ClientCommandTest(BaseTestCase): 
    def _file_exists(file_path): 
     return os.path.exists(file_path) 

    def test_remindmanagers_no_contracts(self): 
     args = [] 
     kwargs = {} 
     #self.assertFalse() 
     # since there are no contracts yet, this should create an entry in ROOT + /logfile 
     call_command('remindmanagers', *args, **kwargs)          # this should log in project/project/test_logfile not debug_logfile 

답변

0

귀하의 LOGGING DICT 리터럴은 리터럴 DICT을 처리 할 때 즉시 평가됩니다 변수 LOGFILE가 포함되어 있습니다. LOGFILE을 재정 의하여 나중에 LOGFILE 문자열이 대체되기 때문에 LOGGING dict의 항목을 변경하지 않습니다. 문자열 자체가 변형되지 않으므로 LOGFILE으로 끝나기 만하면 다른 문자열을 가리키게됩니다.

나는 당신과 같이 새로운 파일 이름으로 바로 로그 파일 핸들러를 오버라이드 (override) 할 수 있어야한다고 생각 :

@override_settings(LOGGING={ 
    'handlers': { 
    'logfile': { 
     'filename': settings.ROOT + "/test_logfile" 
    } 
    } 
}) 
class ClientCommandTest(BaseTestCase): 
    ... 
관련 문제