From e77c85499adde5b280a5178271774e279dc227f7 Mon Sep 17 00:00:00 2001 From: Maciej Tronowski Date: Wed, 22 Apr 2015 17:18:57 +0200 Subject: [PATCH] logging of gridftp errors --- filex/ftp.py | 12 ++++++------ filex/utils.py | 8 ++++++-- filex/views.py | 4 ++++ plgng/settings_logging.py | 16 ++++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/filex/ftp.py b/filex/ftp.py index d83a55c..2b09d3e 100644 --- a/filex/ftp.py +++ b/filex/ftp.py @@ -4,14 +4,17 @@ from itertools import chain import os import re from threading import Event -from django.utils.http import urlunquote +from django.utils.http import urlunquote from django.utils.timezone import localtime, UTC from gridftp import FTPClient, Buffer, HandleAttr, OperationAttr class FTPError(Exception): - pass + def __init__(self, message, verbose=None, *args, **kwargs): + super(FTPError, self).__init__(message, *args, **kwargs) + + self.verbose = verbose class FTPOperation: @@ -67,14 +70,11 @@ class FTPOperation: self._end.clear() if self._error is not None: - # TODO logging - print 'GridFTP ERROR:', self._error - match = re.search(r'A system call failed: (.*)$', self._error.replace('\r\n', '\n'), re.MULTILINE) msg = match.groups()[0] if match else "Unknown error" - raise FTPError(msg) + raise FTPError(msg, self._error) def listing(self, url): self.cli.verbose_list(url, self._done, None, self.op_attr) diff --git a/filex/utils.py b/filex/utils.py index 2b5d72e..49c4d79 100644 --- a/filex/utils.py +++ b/filex/utils.py @@ -1,8 +1,8 @@ from functools import wraps +import logging from django.core.validators import RegexValidator -from django.http import JsonResponse, HttpResponse -from django.template.loader import render_to_string +from django.http import JsonResponse from django.views.decorators.csrf import csrf_protect, csrf_exempt from filex.ftp import FTPError @@ -27,6 +27,10 @@ def with_ftp_upload_handler(view_func): except FTPError as e: msg, status = parse_ftp_error(e) + logger = logging.getLogger('gridftp') + logger.error(e.verbose, + extra={'user': request.user, 'path': request.path, 'params': dict(request.GET.iterlists())}) + return JsonResponse({'error': msg}, status=status) return csrf_exempt(wrapped_view) diff --git a/filex/views.py b/filex/views.py index 0d4ab17..1904d4f 100644 --- a/filex/views.py +++ b/filex/views.py @@ -1,4 +1,5 @@ from itertools import islice +import logging import mimetypes import os @@ -42,6 +43,9 @@ class FTPView(View): except FTPError as e: msg, status = parse_ftp_error(e) + logger = logging.getLogger('gridftp') + logger.error(e.verbose, extra={'user': request.user, 'path': request.path, 'params': form.cleaned_data}) + return JsonResponse({'error': msg}, status=status) setattr(cls, cls.method, process) diff --git a/plgng/settings_logging.py b/plgng/settings_logging.py index de43332..67f5111 100644 --- a/plgng/settings_logging.py +++ b/plgng/settings_logging.py @@ -29,6 +29,10 @@ LOGGING = { 'format': '[%(asctime)s] %(levelname)-8s - %(filename)s:%(lineno)s in %(funcName)s - %(message)s', 'datefmt': DATE_FORMAT, }, + 'gridftp': { + 'format': '[%(asctime)s] %(levelname)-8s - %(path)s - <%(user)s> - %(params)s - %(message)s', + 'datefmt': DATE_FORMAT, + }, }, 'filters': { 'request_filter': { @@ -79,6 +83,13 @@ LOGGING = { 'maxBytes': 1024 * 1024 * 5, # 5 MB 'formatter': 'verbose', }, + 'gridftp': { + 'level': 'DEBUG', + 'class': 'logging.handlers.RotatingFileHandler', + 'filename': os.path.join(LOGS_DIR, 'gridftp.log'), + 'maxBytes': 1024 * 1024 * 5, # 5 MB + 'formatter': 'gridftp', + }, }, 'loggers': { 'django': { @@ -104,5 +115,10 @@ LOGGING = { 'level': 'INFO', 'propagate': False, }, + 'gridftp': { + 'handlers': ['gridftp'], + 'level': 'INFO', + }, + } } -- 1.7.9.5