def timedLogFormatter(timestamp, request): duration = -1 if hasattr(request, 'start_time'): duration = timedelta_to_milliseconds(datetime.now() - request.start_time) return (u'%(code)s %(method)s %(uri)s %(length)dB %(duration)dms' % dict( duration=duration, method=_escape(request.method), uri=_escape(request.uri), proto=_escape(request.clientproto), code=request.code, length=request.sentLength))
def combinedLogFormatter(timestamp, request): """ @return: A combined log formatted log line for the given request. @see: L{IAccessLogFormatter} """ referrer = _escape(request.getHeader(b"referer") or b"-") agent = _escape(request.getHeader(b"user-agent") or b"-") if hasattr(request, "user") and request.user is not None: username = request.user.shortNames[0] try: username = _escape(username) except Exception: username = _escape(repr(username)) else: username = u"-" line = ( u'"%(ip)s" %(user)s - %(timestamp)s "%(method)s %(uri)s %(protocol)s" ' u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict( ip=_escape(request.getClientIP() or b"-"), timestamp=timestamp, method=_escape(request.method), uri=_escape(request.uri), protocol=_escape(request.clientproto), code=request.code, length=request.sentLength or u"-", referrer=referrer, agent=agent, user=username, )) return line
def timedLogFormatter(timestamp, request): duration = -1 if hasattr(request, 'start_time'): duration = round((datetime.now() - request.start_time).microseconds / 1000, 4) line = (u'%(code)s %(method)s %(uri)s %(length)s %(duration)dms' % dict( duration=duration, method=_escape(request.method), uri=_escape(request.uri), proto=_escape(request.clientproto), code=request.code, length=request.sentLength or u"-")) return line
def combinedLogFormatter(timestamp, request): """ @return: A combined log formatted log line for the given request. @see: L{IAccessLogFormatter} """ referrer = _escape(request.getHeader(b"referer") or b"-") agent = _escape(request.getHeader(b"user-agent") or b"-") if hasattr(request, b"user") and request.user: user = _escape(request.user) else: user = b"-" line = ( u'"%(ip)s" %(user)s - %(timestamp)s "%(method)s %(uri)s %(protocol)s" ' u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict( ip=_escape(request.getClientIP() or b"-"), timestamp=timestamp, method=_escape(request.method), uri=_escape(request.uri), protocol=_escape(request.clientproto), code=request.code, length=request.sentLength or u"-", referrer=referrer, agent=agent, user=user, ) ) return line
def debugLogFormatter(timestamp, request): """ @return: A combined log formatted log line for the given request. @see: L{IAccessLogFormatter} """ referrer = _escape(request.getHeader(b"referer") or b"-") agent = _escape(request.getHeader(b"user-agent") or b"-") line = (u'"%(ip)s" - - %(timestamp)s "%(method)s %(uri)s %(protocol)s" ' u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict( ip=_escape(request.getClientIP() or b"-"), timestamp=timestamp, method=_escape(request.method), uri=_escape(request.uri), protocol=_escape(request.clientproto), code=request.code, length=request.sentLength or u"-", referrer=referrer, agent=agent, )) additional_data = request.getHeader(b"x-portia") if additional_data: split_data = _escape(additional_data).split(':') if len(split_data) == 4: session, session_time, user, command = split_data line += (u' id=%(session)s t=%(session_time)s user=%(user)s ' u'command=%(command)s' % dict(session=session, session_time=session_time, user=user, command=command)) return line
def log(self, request): host = http._escape(request.getRequestHostname() or "-") referrer = http._escape(request.getHeader(b"referer") or "-") agent = http._escape(request.getHeader(b"user-agent") or "-") line = '"{ip:s}" - {scheme:s}://{host:s}:{port:d} {timestamp:s} "{method:s} {uri:s} {protocol:s}" {code:d} {length:s} "{referrer:s}" "{agent:s}"' line = line.format( ip=http._escape(request.getClientIP() or "-"), scheme="https" if request.isSecure() else "http", host=host, port=request.getHost().port, timestamp=self._logDateTime, method=http._escape(request.method), uri=http._escape(request.uri), protocol=http._escape(request.clientproto), code=request.code, length=str(request.sentLength) or "-", referrer=referrer, agent=agent, ) logger.info(line) if request.content is not None: try: request.content.seek(0, 0) content = request.content.read() if len(content): logger.info("Content: {}".format(content.decode("UTF-8"))) except: pass
def timedLogFormatter(timestamp, request): """ A custom request formatter. This is whats used when each request line is formatted. :param timestamp: :param request: A twisted.web.server.Request instance """ agent = _escape(request.getHeader(b"user-agent") or b"-") if hasattr(request, "started"): duration = round(time.time() - request.started, 4) else: duration = 0 line = ( u'%(duration)ss "%(ip)s" "%(method)s %(uri)s %(protocol)s %(args)s" ' u'%(code)d %(length)s"' % dict( ip=_escape(request.getClientIP() or b"-"), duration=duration, method=_escape(request.method), uri=_escape(request.uri), args=_escape(str(request.args)), protocol=_escape(request.clientproto), code=request.code, length=request.sentLength or u"-", )) return line
def logFormatter(timestamp, request): duration = -1 if hasattr(request, 'start_time'): duration = timedelta_to_milliseconds(datetime.now() - request.start_time) client_ip = '[REMOVED_IP_ADDRESS]' client_ua = '[REMOVED_USER_AGENT]' if request.log_ip_and_ua: client_ip = request.client_ip client_ua = request.client_ua return (u'%(vhost)s %(ip)s - - %(timestamp)s "%(method)s %(uri)s %(clientproto)s" %(code)s %(length)d %(duration)dms - "%(user_agent)s" %(tid)d' % dict( vhost=_escape(request.hostname), timestamp=timestamp, duration=duration, ip=_escape(client_ip), method=_escape(request.method), uri=_escape(request.uri), clientproto=_escape(request.clientproto), code=request.code, length=request.sentLength, user_agent=_escape(client_ua), tid=request.tid))
def logFormatter(timestamp, request): duration = -1 if hasattr(request, 'start_time'): duration = timedelta_to_milliseconds(datetime.now() - request.start_time) client_ip = '[REMOVED_IP_ADDRESS]' client_ua = '[REMOVED_USER_AGENT]' if request.log_ip_and_ua: client_ip = request.client_ip client_ua = request.client_ua return (u'%(ip)s %(tid)s %(code)s %(method)s %(uri)s %(length)d %(duration)dms "%(user_agent)s"' % dict( duration=duration, ip=_escape(client_ip), method=_escape(request.method), uri=_escape(request.uri), code=request.code, length=request.sentLength, user_agent=_escape(client_ua), tid=request.tid))
def timedLogFormatter(timestamp, request): duration = -1 if hasattr(request, 'start_time'): duration = timedelta_to_milliseconds(datetime.now() - request.start_time) if request.to_be_anonymized: client_ip = '[REMOVED_IP_ADDRESS]' client_ua = '[REMOVED_USER_AGENT]' else: client_ip = request.client_ip client_ua = request.client_ua return (u'%(ip)s %(tid)s %(code)s %(method)s %(uri)s %(length)d %(duration)dms "%(user_agent)s"' % dict( duration=duration, ip=_escape(client_ip), method=_escape(request.method), uri=_escape(request.uri), code=request.code, length=request.sentLength, user_agent=_escape(client_ua), tid=request.tid))
def combinedLogFormatter(timestamp: str, request: IRequest) -> str: """ @return: A combined log formatted log line for the given request. @see: L{IAccessLogFormatter} """ referrer = _escape(request.getHeader("referer") or "-") agent = _escape(request.getHeader("user-agent") or "-") clientIP = request.getClientIP() forwardedFor = ( request.requestHeaders .getRawHeaders(b"x-forwarded-for", [b""])[0] .split(b",")[0] .strip() ).decode("charmap") if forwardedFor: ip = f"{forwardedFor} > {clientIP}" else: ip = clientIP if hasattr(request, "user") and request.user is not None: username = request.user.shortNames[0] try: username = _escape(username) except Exception: username = _escape(repr(username)) else: username = "******" line = ( '"{ip}" {user} - {timestamp} "{method} {uri} {protocol}" ' '{code} {length} "{referrer}" "{agent}"'.format( ip=_escape(ip or "-"), timestamp=timestamp, method=_escape(request.method), uri=_escape(request.uri), protocol=_escape(request.clientproto), code=request.code, length=request.sentLength or "-", referrer=referrer, agent=agent, user=username, ) ) return line
def combinedLogFormatter(timestamp: str, request: IRequest) -> str: """ @return: A combined log formatted log line for the given request. @see: L{IAccessLogFormatter} """ referrer = _escape(request.getHeader("referer") or "-") agent = _escape(request.getHeader("user-agent") or "-") clientIP = request.getClientIP() forwardedFor = ( request.requestHeaders.getRawHeaders(b"x-forwarded-for", [b""])[0] .split(b",")[0] .strip() ).decode("charmap") if forwardedFor: ip = f"{forwardedFor} > {clientIP}" else: ip = clientIP if hasattr(request, "user") and request.user is not None: username = request.user.shortNames[0] try: username = _escape(username) except Exception: username = _escape(repr(username)) else: username = "******" line = ( '"{ip}" {user} - {timestamp} "{method} {uri} {protocol}" ' '{code} {length} "{referrer}" "{agent}"'.format( ip=_escape(ip or "-"), timestamp=timestamp, method=_escape(request.method), uri=_escape(request.uri), protocol=_escape(request.clientproto), code=request.code, length=request.sentLength or "-", referrer=referrer, agent=agent, user=username, ) ) return line
def _log(result, timestamp, request): host = http._escape(request.getRequestHostname() or "-") referrer = http._escape(request.getHeader(b"referer") or "-") agent = http._escape(request.getHeader(b"user-agent") or "-") line = 'HTTP [{timestamp:s}]: "{ip:s}" - {scheme:s}://{host:s}:{port:d} "{method:s} {uri:s} {protocol:s}" {code:d} {length:s} "{referrer:s}" "{agent:s}"' line = line.format( ip=http._escape(request.getClientIP() or "-"), scheme="https" if request.isSecure() else "http", host=host, port=request.getHost().port, timestamp=timestamp, method=http._escape(request.method), uri=http._escape(request.uri), protocol=http._escape(request.clientproto), code=request.code, length=str(request.sentLength) or "-", referrer=referrer, agent=agent, ) logger.info(line)
def timedLogFormatter(cls, timestamp, request): from twisted.web.http import _escape referrer = _escape(request.getHeader("referer") or "-") agent = _escape(request.getHeader("user-agent") or "-") tc = round(time.time() - request.started, 4) line = u'%(fmt)s | "%(ip)s" %(tc)ss %(code)d %(length)s "%(method)s %(uri)s %(proto)s" "%(agent)s" "%(ref)s"' % { 'fmt': Time.current_time('%m-%d %H:%M:%S.%f'), 'ip': _escape(request.getClientIP() or "-"), 'tc': tc, 'method': _escape(request.method), 'uri': _escape(request.uri), 'proto': _escape(request.clientproto), 'code': request.code, 'length': request.sentLength or "-", 'agent': agent, 'ref': referrer, } return line
def log_formatter(self, timestamp, request): if request.code < self.log_min_http_code: return None # duration = round(time.time() - request.started, 4) duration = 0.0 referrer = _escape(request.getHeader(b"referer") or b"-") agent = _escape(request.getHeader(b"user-agent") or b"-") line = ( u'"%(ip)s" - %(duration)ss - %(timestamp)s "%(method)s %(uri)s %(protocol)s" ' u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict( ip=_escape(request.getClientIP() or b"-"), timestamp=timestamp, duration=duration, method=_escape(request.method), uri=_escape(request.uri), protocol=_escape(request.clientproto), code=request.code, length=request.sentLength or u"-", referrer=referrer, agent=agent, )) return line
def debugLogFormatter(timestamp, request): """ @return: A combined log formatted log line for the given request. @see: L{IAccessLogFormatter} """ referrer = _escape(request.getHeader(b"referer") or b"-") agent = _escape(request.getHeader(b"user-agent") or b"-") line = ( u'"%(ip)s" - - %(timestamp)s "%(method)s %(uri)s %(protocol)s" ' u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict( ip=_escape(request.getClientIP() or b"-"), timestamp=timestamp, method=_escape(request.method), uri=_escape(request.uri), protocol=_escape(request.clientproto), code=request.code, length=request.sentLength or u"-", referrer=referrer, agent=agent, ) ) return line
def proxiedLogFormatter(timestamp, request): """ @return: A combined log formatted log line for the given request but use the value of the I{X-Forwarded-For} header as the value for the client IP address. @see: L{IAccessLogFormatter} """ line = ( u'"%(ip)s" - "%(method)s %(uri)s %(protocol)s" %(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict( ip=_escape(request.getClientIP() or b"-"), timestamp=timestamp, method=_escape(request.method), uri=_escape(request.uri), protocol=_escape(request.clientproto), code=request.code, length=request.sentLength or u"-", referrer=_escape(request.getHeader(b"referer") or b"-"), agent=_escape(request.getHeader(b"user-agent") or b"-"), )) return line
def _escape(self, s): if hasattr(http, '_escape'): return http._escape(s) return server.Site._escape(self, s)