Skip to content

Fix UnicodeDecodeError for Umlaute in translogger #74

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 22 additions & 2 deletions paste/translogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@ class TransLogger(object):

If ``setup_console_handler`` is true, then messages for the named
logger will be sent to the console.

Customize log-time, use a custom strftime format (within **kwargs):
time_format="%d/%Y:%H:%M "
"""

format = ('%(REMOTE_ADDR)s - %(REMOTE_USER)s [%(time)s] '
'"%(REQUEST_METHOD)s %(REQUEST_URI)s %(HTTP_VERSION)s" '
'%(status)s %(bytes)s "%(HTTP_REFERER)s" "%(HTTP_USER_AGENT)s"')

time_format = '%d/%m/%Y:%H:%M:%S'

def __init__(self, application,
logger=None,
format=None,
logging_level=logging.INFO,
logger_name='wsgi',
setup_console_handler=True,
set_logger_level=logging.DEBUG):
set_logger_level=logging.DEBUG,
**kwargs):
if format is not None:
self.format = format
self.application = application
Expand All @@ -48,6 +54,10 @@ def __init__(self, application,
self.logger.setLevel(set_logger_level)
else:
self.logger = logger
self.time_format = TransLogger.time_format
# Handle any additional keyword arguments (**kwargs)
for key, value in kwargs.items():
setattr(self, key, value)

def __call__(self, environ, start_response):
start = time.localtime()
Expand Down Expand Up @@ -84,13 +94,23 @@ def write_log(self, environ, method, req_uri, start, status, bytes):
remote_addr = environ['HTTP_X_FORWARDED_FOR']
elif environ.get('REMOTE_ADDR'):
remote_addr = environ['REMOTE_ADDR']

formatted_time = time.strftime(self.time_format, start) + offset
if "time_format" in vars(self):
try:
formatted_time = time.strftime(self.time_format, start) + offset

except ValueError as v:
self.logger.log(logging.WARN, v)
pass

d = {
'REMOTE_ADDR': remote_addr,
'REMOTE_USER': environ.get('REMOTE_USER') or '-',
'REQUEST_METHOD': method,
'REQUEST_URI': req_uri,
'HTTP_VERSION': environ.get('SERVER_PROTOCOL'),
'time': time.strftime('%d/%b/%Y:%H:%M:%S ', start) + offset,
'time': formatted_time,
'status': status.split(None, 1)[0],
'bytes': bytes,
'HTTP_REFERER': environ.get('HTTP_REFERER', '-'),
Expand Down