def logHttpError(self, ResponseCode=None, **kwargs): requestLogDict = dict() responseLogDict = dict() requestLogDict['timestamp'] = self._time() for key in ['Client', 'Headers', 'RequestURI', 'Method', 'HTTPVersion']: if key in kwargs: requestLogDict[key] = kwargs[key] if ResponseCode: responseLogDict['httpStatus'] = str(ResponseCode) self.do.logHttpError(ResponseCode=ResponseCode, **kwargs) collectLogForScope(httpRequest=requestLogDict, httpResponse=responseLogDict)
def handleRequest(self, **kwargs): requestLogDict = dict() responseLogDict = dict() timestamp = self._time() requestLogDict['timestamp'] = timestamp for key in [ 'Client', 'Headers', 'RequestURI', 'Method', 'HTTPVersion', 'path', 'query', 'arguments' ]: if key in kwargs: requestLogDict[key] = kwargs[key] body = kwargs.get('Body') if body: requestLogDict['bodySize'] = len(body) sizeInBytes = 0 httpStatus = "" try: for response in compose(self.all.handleRequest(**kwargs)): if response is Yield or callable(response): yield response continue bResponse = ensureBytes(response) sizeInBytes += len(bResponse) if not httpStatus and bResponse.startswith(b'HTTP/1'): httpStatus = bResponse[len(b'HTTP/1.0 '):][:3] yield bResponse except (SystemExit, KeyboardInterrupt, AssertionError): raise except: _, errorValue, _ = exc_info() responseLogDict['size'] = sizeInBytes or '-' responseLogDict['httpStatus'] = str( httpStatus, encoding='utf-8' ) if httpStatus else '500' # assuming this is what HttpServer will make of it responseLogDict['duration'] = self._time() - timestamp responseLogDict['exception'] = errorValue collectLogForScope(httpRequest=requestLogDict, httpResponse=responseLogDict) raise responseLogDict['size'] = sizeInBytes if httpStatus: responseLogDict['httpStatus'] = str(httpStatus, encoding='utf-8') responseLogDict['duration'] = self._time() - timestamp collectLogForScope(httpRequest=requestLogDict, httpResponse=responseLogDict)
def testCollectLogForScope(self): __callstack_var_logCollector__ = dict() collectLogForScope(scope={'key': 'value'}) collectLogForScope(scope={'key2': 'value'}) self.assertEquals({'scope': {'key': ['value'], 'key2': ['value']}}, __callstack_var_logCollector__) collectLogForScope(scope={'key': 'value2'}) self.assertEquals({'scope': {'key': ['value', 'value2'], 'key2': ['value']}}, __callstack_var_logCollector__)
def handleRequest(self, **kwargs): requestLogDict = dict() responseLogDict = dict() timestamp = self._time() requestLogDict['timestamp'] = timestamp for key in ['Client', 'Headers', 'RequestURI', 'Method', 'HTTPVersion', 'path', 'query', 'arguments']: if key in kwargs: requestLogDict[key] = kwargs[key] body = kwargs.get('Body') if body: requestLogDict['bodySize'] = len(body) sizeInBytes = 0 httpStatus = "" try: for response in compose(self.all.handleRequest(**kwargs)): if response is Yield or callable(response): yield response continue if hasattr(response, '__len__'): sizeInBytes += len(response) if not httpStatus and response.startswith('HTTP/1'): httpStatus = response[len('HTTP/1.0 '):][:3] yield response except (SystemExit, KeyboardInterrupt, AssertionError): raise except: _, errorValue, _ = exc_info() responseLogDict['size'] = sizeInBytes or '-' responseLogDict['httpStatus'] = httpStatus or '500' # assuming this is what HttpServer will make of it responseLogDict['duration'] = self._time() - timestamp responseLogDict['exception'] = errorValue collectLogForScope(httpRequest=requestLogDict, httpResponse=responseLogDict) raise responseLogDict['size'] = sizeInBytes if httpStatus: responseLogDict['httpStatus'] = httpStatus responseLogDict['duration'] = self._time() - timestamp collectLogForScope(httpRequest=requestLogDict, httpResponse=responseLogDict)
def callMe(self, times=1): for i in range(times): collectLogForScope(httpResponse={'duration': Decimal(0.4 * i)}) collectLogForScope(otherResponse={'duration': Decimal(0.3)})
def callMe(self, times=1): for i in range(times): collectLogForScope(collectResponse={'some': 'value'})
displayValue=displayValue(term)) for term in terms ] jsonResponse['facets'][fieldname] = fieldList jsonResult['response'] = jsonResponse except ValueError, e: jsonResult['error'] = { 'type': type(e).__name__, 'message': str(e), } yield "HTTP/1.0 200 OK" + CRLF yield "Content-type: application/json" + CRLF yield CRLF yield dumps(jsonResult, item_sort_key=_item_sort_key, indent=2) finally: collectLogForScope(search=logDict) def jsonResponse(self, **kwargs): t0 = self._timeNow() result = yield self.any.executeQuery(**kwargs) queryTime = self._timeNow() - t0 total, hits = result.total, result.hits jsonResponse = JsonDict({'total': total}) if hits: if hasattr(result, 'items'): jsonResponse['items'] = result.items else: jsonResponse['items'] = [] for hit in hits: