Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions requestlogger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,44 @@ def __init__(self, application, handlers, formatter=None, propagate=True, ip_hea
self.logger.addHandler(handler)
self.ip_header = ip_header
self.application = application
self._status_code = ''
self._content_length = 0

def __call__(self, environ, start_response):
start = clock()
status_codes = []
content_lengths = []

def custom_start_response(status, response_headers, exc_info=None):
status_codes.append(int(status.partition(' ')[0]))
def _start_response(status, response_headers, exc_info=None):
self._status_code = status.split()[0]
self._content_length = 0
for name, value in response_headers:
if name.lower() == 'content-length':
content_lengths.append(int(value))
break
return start_response(status, response_headers, exc_info)
retval = self.application(environ, custom_start_response)
if name.lower() == 'content-length':
self._content_length += int(value)
break
return start_response(status, response_headers, exc_info)

retval = self.application(environ, _start_response)
runtime = int((clock() - start) * 10**6)
content_length = content_lengths[0] if content_lengths else len(b''.join(retval))
msg = self.formatter(status_codes[0], environ, content_length, ip_header=self.ip_header, rt_us=runtime)
msg = self.formatter(self._status_code, environ, self._content_length, ip_header=self.ip_header, rt_us=runtime)
self.logger.info(msg)
return retval



@staticmethod
def standard_formatter(status_code, environ, content_length):
return "{0} {1}".format(dt.now().isoformat(), status_code)
def standard_formatter(status_code, environ, content_length, **kwargs):
_host = environ.get('REMOTE_ADDR', '')
_timestamp = dt.now(Local).strftime("%d/%b/%Y:%H:%M:%S %z")
_request = "{0} {1}{2}{3} {4}".format(
environ.get('REQUEST_METHOD', ''),
environ.get('PATH_INFO', ''),
'?' if environ.get('QUERY_STRING', '') else '',
environ.get('QUERY_STRING', ''),
environ.get('SERVER_PROTOCOL', '')
)
_status = status_code
_size = content_length

return f'{_host} - - [{_timestamp}] "{_request}" {_status} {_size}'


def ApacheFormatter(with_response_time=True):
Expand Down
2 changes: 1 addition & 1 deletion requestlogger/timehacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Idea: http://stackoverflow.com/a/2071364/183995
"""

from datetime import datetime as dt, tzinfo, timedelta
from datetime import tzinfo, timedelta
import time as _time

ZERO = timedelta(0)
Expand Down