Ticket #6385: 0021-Colorize-HTTP-server-log-output.patch

File 0021-Colorize-HTTP-server-log-output.patch, 2.7 KB (added by Bastian Kleineidam <calvin@…>, 7 years ago)

split off coloring in a separate function

  • django/core/management/color.py

    From 4f97d007bfdf5b702fd6dfca105bf44d283b3cef Mon Sep 17 00:00:00 2001
    From: Bastian Kleineidam <calvin@debian.org>
    Date: Fri, 25 Jan 2008 21:39:52 +0100
    Subject: Colorize HTTP server log output
    
    
    Signed-off-by: Bastian Kleineidam <calvin@debian.org>
    
    diff --git a/django/core/management/color.py b/django/core/management/color.py
    index 40fd4e7..1b2fa3a 100644
    a b def color_style(): 
    2020    style.SQL_COLTYPE = termcolors.make_style(fg='green')
    2121    style.SQL_KEYWORD = termcolors.make_style(fg='yellow')
    2222    style.SQL_TABLE = termcolors.make_style(opts=('bold',))
     23    style.DEBUG = termcolors.make_style(fg='yellow', opts=('bold',))
     24    # HTTP STATUS CODES
     25    # 2xx
     26    style.HTTP_SUCCESS = termcolors.make_style(fg='green', opts=('bold',))
     27    # 3xx
     28    style.HTTP_REDIRECT = termcolors.make_style(fg='blue', opts=('bold',))
     29    # 4xx
     30    style.HTTP_CLIENT_ERROR = termcolors.make_style(fg='yellow', opts=('bold',))
     31    # 5xx
     32    style.HTTP_SERVER_ERROR = termcolors.make_style(fg='red', opts=('bold',))
    2333    return style
    2434
    2535def no_style():
  • django/core/servers/basehttp.py

    diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py
    index ac5cd86..0f72f72 100644
    a b import re 
    1414import sys
    1515import urllib
    1616
     17from django.core.management.color import color_style
    1718from django.utils.http import http_date
    1819
    1920__version__ = "0.1"
    def is_hop_by_hop(header_name): 
    223224    """Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header"""
    224225    return header_name.lower() in _hop_headers
    225226
     227def colorize_status (status_code):
     228    """Colorize a HTTP status code string."""
     229    style = color_style()
     230    if status_code.startswith('2'):
     231        status_code = style.HTTP_SUCCESS(status_code)
     232    elif status_code.startswith('3'):
     233        status_code = style.HTTP_REDIRECT(status_code)
     234    elif status_code.startswith('4'):
     235        status_code = style.HTTP_CLIENT_ERROR(status_code)
     236    elif status_code.startswith('5'):
     237        status_code = style.HTTP_SERVER_ERROR(status_code)
     238    return status_code
     239
     240
    226241class ServerHandler(object):
    227242    """Manage the invocation of a WSGI application"""
    228243
    class ServerHandler(object): 
    443458
    444459    def close(self):
    445460        try:
    446             self.request_handler.log_request(self.status.split(' ',1)[0], self.bytes_sent)
     461            status_code = colorize_status(self.status.split(' ',1)[0])
     462            self.request_handler.log_request(status_code, self.bytes_sent)
    447463        finally:
    448464            try:
    449465                if hasattr(self.result,'close'):
Back to Top