def __init__(self, urls, host=None, port=None, middleware_classes=None, name=None, log=None, log_format=None, log_level=None, settings=None): self.urls = urls self.host = host or self.host self.port = port or self.port self.middleware_classes = middleware_classes self.name = name # Log params log_params = {} self.log = log or self.log if self.log: # and isinstance(self.log, basestring): #self.log = file(self.log, 'a') log_params['filename'] = self.log log_params['filemode'] = 'a' self.log_level = log_level or self.log_level self.log_format = log_format or self.log_format self.logger = get_logger('london', **log_params) self.logger.setLevel(self.log_level) # Setting the customized settings self.settings = settings or self.settings if self.settings: self.update_settings() self.load_middleware()
def process_exception(self, request, response, exception): logger = get_logger('london') full_url = 'http%s://%s%s'%('s' if request.is_secure() else '', request.META['HTTP_HOST'], request.get_full_path()) exception.name = exception.__class__.__name__ # Traceback tb = traceback.format_exc() # Heap heap = get_memory_heap() # ps info process_info = get_process_info() if app_settings.PRINT_TRACEBACK: msg = [] msg.append('-'*40) msg.append(tb) msg.append('-'*40) if heap: msg.append(heap) msg.append('-'*40) if process_info: msg.append(['%s: %s'%(k,v) for k,v in process_info]) msg.append('-'*40) logger.error('\n'.join(msg)) # URL not found if isinstance(exception, Http404): if app_settings.PAGE_NOT_FOUND_NOTIFICATION: body = [ 'URL: %s'%full_url, 'Headers:', ] for k,v in request.META.items(): body.append('- %s: %s'%(k,v)) if request.POST: body.append('POST Data:') for k,v in request.POST.items(): body.append('- %s: %s'%(k,v)) send_message_to_admins(subject='Page not found in the site', body='\n'.join(body)) else: # Error notification if not app_settings.DEBUG and app_settings.SERVER_ERROR_NOTIFICATION: body = [ '%s: %s'%(exception.__class__.__name__, exception), 'URL: %s'%full_url, '\nHeaders:', ] for k,v in request.META.items(): body.append('- %s: %s'%(k,v)) if request.POST: body.append('\nPOST Data:') for k,v in request.POST.items(): body.append(u'- %s: %s' % (k,unicode(v))) body.append('\nTraceback:') body.append(tb) if heap: body.append('\nMemory Heap:') body.append(heap) if process_info: body.append('\nProcess Info:') for k,v in process_info: body.append('\n- %s: %s' % (k,v)) send_message_to_admins(subject='Error in the server', body='\n'.join(body)) # Response if isinstance(exception, Http404): view = import_anything(app_settings.ERROR_HANDLERS['404']) else: view = import_anything(app_settings.ERROR_HANDLERS['500']) resp = view(request, exception=exception, traceback_string=tb, heap_string=heap, process_info=process_info) if isinstance(exception, Http404): return HttpResponseNotFound(resp.content) else: return HttpResponseServerError(resp.content)