diff --git a/.gitignore b/.gitignore index 96bc99ece0..3405a7a43e 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ htmlcov/ mayan/media/ mayan/media/document_cache/ mayan/settings/local.py +mayan/error.log settings_local.py static_collected/ /celerybeat-schedule diff --git a/HISTORY.rst b/HISTORY.rst index e87bbdd84a..fac2320fb6 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -4,6 +4,10 @@ - Add support for emailing documents to a recipient list. GitLab #396. - Backport metadata widget changes from @Macrobb. GitLab #377. - Make users and group searchable. +- Add support for logging errors during in production mode. + Add COMMON_PRODUCTION_ERROR_LOG_PATH to control path of log file. + Defaults to mayan/error.log. +- Add support logging request exceptions. 2.5.2 (2017-07-08) ================== diff --git a/mayan/apps/common/apps.py b/mayan/apps/common/apps.py index 298c6707fe..dd612b8625 100644 --- a/mayan/apps/common/apps.py +++ b/mayan/apps/common/apps.py @@ -2,6 +2,7 @@ from __future__ import absolute_import, unicode_literals from datetime import timedelta import logging +import os from kombu import Exchange, Queue @@ -30,7 +31,7 @@ from .literals import DELETE_STALE_UPLOADS_INTERVAL from .menus import menu_about, menu_main, menu_tools, menu_user from .licenses import * # NOQA from .queues import * # NOQA - Force queues registration -from .settings import setting_auto_logging +from .settings import setting_auto_logging, setting_production_error_log_path from .tasks import task_delete_stale_uploads # NOQA - Force task registration logger = logging.getLogger(__name__) @@ -154,13 +155,15 @@ class CommonApp(MayanAppConfig): if setting_auto_logging.value: if settings.DEBUG: level = 'DEBUG' + handlers = ['console'] else: level = 'ERROR' + handlers = ['console', 'logfile'] loggers = {} for project_app in apps.apps.get_app_configs(): loggers[project_app.name] = { - 'handlers': ['console'], + 'handlers': handlers, 'propagate': True, 'level': level, } @@ -179,7 +182,11 @@ class CommonApp(MayanAppConfig): 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'intermediate' - } + }, + 'logfile': { + 'class': 'logging.handlers.WatchedFileHandler', + 'filename': setting_production_error_log_path.value, + }, }, 'loggers': loggers } diff --git a/mayan/apps/common/middleware/error_logging.py b/mayan/apps/common/middleware/error_logging.py new file mode 100644 index 0000000000..39058af612 --- /dev/null +++ b/mayan/apps/common/middleware/error_logging.py @@ -0,0 +1,13 @@ +from __future__ import unicode_literals + +import logging + +logger = logging.getLogger(__name__) + + +class ErrorLoggingMiddleware(object): + def process_exception(self, request, exception): + logger.exception( + 'Exception caught by request middleware; %s, %s', request, + exception + ) diff --git a/mayan/apps/common/settings.py b/mayan/apps/common/settings.py index 8edf6c8512..893f3219fe 100644 --- a/mayan/apps/common/settings.py +++ b/mayan/apps/common/settings.py @@ -1,7 +1,9 @@ from __future__ import unicode_literals +import os import tempfile +from django.conf import settings from django.utils.translation import ugettext_lazy as _ from smart_settings import Namespace @@ -40,3 +42,10 @@ setting_temporary_directory = namespace.add_setting( ), is_path=True ) +setting_production_error_log_path = namespace.add_setting( + global_name='COMMON_PRODUCTION_ERROR_LOG_PATH', + default=os.path.join(settings.BASE_DIR, 'error.log'), help_text=_( + 'Path to the logfile that will track errors during production.' + ), + is_path=True +) diff --git a/mayan/apps/common/views.py b/mayan/apps/common/views.py index a05d2a2dba..b59a1a611f 100644 --- a/mayan/apps/common/views.py +++ b/mayan/apps/common/views.py @@ -37,6 +37,7 @@ class CheckVersionView(SimpleView): template_name = 'appearance/generic_template.html' def get_extra_context(self): + raise Exception('asd') try: check_version() except NotLatestVersion as exception: diff --git a/mayan/settings/base.py b/mayan/settings/base.py index d13585e8ae..9ffd843215 100644 --- a/mayan/settings/base.py +++ b/mayan/settings/base.py @@ -108,6 +108,7 @@ INSTALLED_APPS = ( ) MIDDLEWARE_CLASSES = ( + 'common.middleware.error_logging.ErrorLoggingMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'corsheaders.middleware.CorsMiddleware',