2016-09-22 78 views
1

我在Django中實現了自定義命令,並且它們的異常未記錄在我的日誌文件中。在Django命令中記錄異常

我創建了一個應用程序my_app_with_commands,其中包含一個目錄management/commands,其中我實現了一些命令。

的樣本命令,可能是這樣的,它崩潰的原因的異常:

import logging 
from django.core.management.base import BaseCommand 


class Command(BaseCommand): 
    help = 'Do something usful' 
    log = logging.getLogger(__name__) 

    def handle(self, *args, **options): 
     self.log.info('Starting...') 
     raise RuntimeError('Something bad happened') 
     self.log.info('Done.') 

我的日誌配置是這樣的:

LOGGING = { 
    'version': 1, 
    'disable_existing_loggers': False, 
    'formatters': { 
     'normal': { 
      'format': '%(asctime)s %(module)s %(levelname)s %(message)s', 
     } 
    }, 
    'handlers': { 
     'file': { 
      'level': 'INFO', 
      'class': 'logging.FileHandler', 
      'filename': os.path.join(BASE_DIR, '..', 'logs', 'my_log.log'), 
      'formatter': 'normal', 
     }, 
     'mail_admins': { 
      'level': 'ERROR', 
      'class': 'django.utils.log.AdminEmailHandler', 
      'include_html': True, 
     } 
    }, 
    'loggers': { 
     'django': { 
      'handlers': ['file'], 
      'level': 'INFO', 
      'propagate': True, 
     }, 
     'my_app_with_commands': { 
      'handlers': ['file', 'mail_admins'], 
      'level': 'INFO', 
      'propagate': True, 
     }, 
    }, 
} 

當我運行命令,調用到記錄器已成功保存到my_log.log文件:

2016-09-22 11:37:01,514 test INFO Starting... 

但是exc主器件接收與每個追溯,顯示在stderr當命令被稱爲:

[[email protected] src]$ ./manage.py test 
Traceback (most recent call last): 
    File "./manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/home/mgarcia/anaconda3/envs/my_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line 
    utility.execute() 
    File "/home/mgarcia/anaconda3/envs/my_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/home/mgarcia/anaconda3/envs/my_env/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/home/mgarcia/anaconda3/envs/my_env/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute 
    output = self.handle(*args, **options) 
    File "/home/mgarcia/my_project/src/my_app_with_commands/management/commands/test_command.py", line 11, in handle 
    raise RuntimeError('Something bad happened') 
RuntimeError: Something bad happened 

我可以在我的每個命令的try/except塊實施,並手動登錄異常。但是,我可以捕獲異常並將其保存到我的日誌文件中,而不是使用Django設置的stderr

+0

而是修改每個命令的,你可以只寫一個裝飾,並使用:' @catch_and_log_exceptions; def handle(self,...)'其中裝飾器包含'try:return方法(self,* args,** kwargs);除了例外,如e:self.log(...)'。 – Bakuriu

回答

0

的方式之一 - 你可以編輯manage.py文件,以在它添加一個try/except塊:

log = logging.getLogger('my_app_with_commands') 
# ... 
try: 
    execute_from_command_line(sys.argv) 
except Exception as e: 
    log.error('your exception log') 
    raise e