Created
July 25, 2013 08:46
-
-
Save szelga/6077959 to your computer and use it in GitHub Desktop.
monkey patch Werkzeug to color log output according to HTTP responce code. created for using with django_extensions command runserver_plus.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.core.management.color import color_style | |
try: | |
from werkzeug.serving import WSGIRequestHandler | |
from werkzeug._internal import _log | |
_style = color_style() | |
def werk_log(self, type, message, *args): | |
msg = '%s - - [%s] %s' % (self.address_string(), | |
self.log_date_time_string(), | |
message % args) | |
http_code = str(args[1]) | |
# Utilize terminal colors, if available | |
if http_code[0] == '2': | |
# Put 2XX first, since it should be the common case | |
msg = _style.HTTP_SUCCESS(msg) | |
elif http_code[0] == '1': | |
msg = _style.HTTP_INFO(msg) | |
elif http_code == '304': | |
msg = _style.HTTP_NOT_MODIFIED(msg) | |
elif http_code[0] == '3': | |
msg = _style.HTTP_REDIRECT(msg) | |
elif http_code == '404': | |
msg = _style.HTTP_NOT_FOUND(msg) | |
elif http_code[0] == '4': | |
msg = _style.HTTP_BAD_REQUEST(msg) | |
else: | |
# Any 5XX, or any other response | |
msg = _style.HTTP_SERVER_ERROR(msg) | |
_log(type, msg) | |
if settings.DEBUG: | |
WSGIRequestHandler.log = werk_log | |
except: # no werkzeug, for instance | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment