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():
|
20 | 20 | style.SQL_COLTYPE = termcolors.make_style(fg='green') |
21 | 21 | style.SQL_KEYWORD = termcolors.make_style(fg='yellow') |
22 | 22 | 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',)) |
23 | 33 | return style |
24 | 34 | |
25 | 35 | def no_style(): |
diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py
index ac5cd86..0f72f72 100644
a
|
b
|
import re
|
14 | 14 | import sys |
15 | 15 | import urllib |
16 | 16 | |
| 17 | from django.core.management.color import color_style |
17 | 18 | from django.utils.http import http_date |
18 | 19 | |
19 | 20 | __version__ = "0.1" |
… |
… |
def is_hop_by_hop(header_name):
|
223 | 224 | """Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header""" |
224 | 225 | return header_name.lower() in _hop_headers |
225 | 226 | |
| 227 | def 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 | |
226 | 241 | class ServerHandler(object): |
227 | 242 | """Manage the invocation of a WSGI application""" |
228 | 243 | |
… |
… |
class ServerHandler(object):
|
443 | 458 | |
444 | 459 | def close(self): |
445 | 460 | 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) |
447 | 463 | finally: |
448 | 464 | try: |
449 | 465 | if hasattr(self.result,'close'): |