Django --LOGGING

10425 ワード

djangoプロジェクトでログを書く機能を実現するには、一般的にPythonloggingを連想しますが、彼には小さな欠陥があり、手動Try ... Exceptationで傍受する必要があります.アクセサリーに合わせて柔軟ですが、シーンによってはあまり似合わないものもあります.DjangosettingsファイルにはLOGGINGコンポーネントを直接追加できますが、Djangoプロジェクトの異常を検出し、ログに書き込む機能があります.
コード:
# settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,  #             
    'formatters': {  #          
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(thread)d %(message)s'
        },
        'simple': { #        
            'format': '%(levelname)s %(module)s %(lineno)d %(message)s'
        },
    },
    'filters': {  #        
        'require_debug_true': {  # django debug        
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {  #       
        'console': {  #         
            'level': 'INFO',  #     
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'},
        'file': {  #         
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(BASE_DIR, 'logs/permission.log'),  #        
            'maxBytes': 300 * 1024 * 1024,  #      300M
            'backupCount': 10,  #       
            'formatter': 'verbose''encoding': 'utf-8',
        },
        #          
        'error': {
            'level': 'ERROR',
            'class': 'logging.handlers.RotatingFileHandler',  #      ,   
            'filename': os.path.join(BASE_DIR, "logs/error.log"),  #     
            'maxBytes': 1024 * 1024 * 100,  #      100M
            'backupCount': 5,
            'formatter': 'verbose',
            'encoding': 'utf-8',
        },
    },
    'loggers': {  #    
        'django': {  #        django    
            'handlers': ['console', 'file','error'],  #                
            #        'console'  
            'propagate': True,  #           
            'level': 'INFO',  #             
        },
    }
}

そうですね.これを加えるとerror書き込みログを自動的に検出できます.コードで使用されるモードはdebug=Trueで、直接手動でfalseに変更することもできます.
公式ドキュメント:https://docs.djangoproject.com/en/2.1/topics/logging/科学的なインターネットが必要です
ドキュメントには詳しく紹介されています