Repository: django-extensions/django-extensions

Issue:
No color output on runserver_plus
I get color output from normal runserver but not from runserver_plus.



Discussion:
that's because runserver_plus uses Werkzeug, and it uses python logging module, not trying to color anything.

i don't know if this feature is worth it. but, anyway, i created a gist: https://gist.github.com/szelga/6077959

you can use it in your project (works under django 1.5.1 for me) or create a pull request for django_extensions (or Werkzeug) based on it.


Resolving patch:
diff --git a/django_extensions/management/commands/runserver_plus.py b/django_extensions/management/commands/runserver_plus.py
--- a/django_extensions/management/commands/runserver_plus.py
+++ b/django_extensions/management/commands/runserver_plus.py
@@ -1,13 +1,17 @@
-from django.conf import settings
-from django.core.management.base import BaseCommand, CommandError
-from django_extensions.management.utils import setup_logger, RedirectHandler
-from optparse import make_option
 import os
 import re
 import socket
 import sys
 import time
 
+from optparse import make_option
+
+from django.conf import settings
+from django.core.management.base import BaseCommand, CommandError
+from django_extensions.management.utils import setup_logger, RedirectHandler
+from django_extensions.management.technical_response import null_technical_500_response
+
+
 try:
     if 'django.contrib.staticfiles' in settings.INSTALLED_APPS:
         from django.contrib.staticfiles.handlers import StaticFilesHandler
@@ -20,6 +24,7 @@
 except ImportError:
     USE_STATICFILES = False
 
+
 naiveip_re = re.compile(r"""^(?:
 (?P<addr>
     (?P<ipv4>\d{1,3}(?:\.\d{1,3}){3}) |         # IPv4 address
@@ -28,11 +33,10 @@
 ):)?(?P<port>\d+)$""", re.X)
 DEFAULT_PORT = "8000"
 
+
 import logging
 logger = logging.getLogger(__name__)
 
-from django_extensions.management.technical_response import null_technical_500_response
-
 
 class Command(BaseCommand):
     option_list = BaseCommand.option_list + (
@@ -112,8 +116,17 @@ def execute(self, sql, params=()):
             from django.core.servers.basehttp import get_internal_wsgi_application as WSGIHandler
         except ImportError:
             from django.core.handlers.wsgi import WSGIHandler  # noqa
+
         try:
             from werkzeug import run_simple, DebuggedApplication
+
+            # Set colored output
+            if settings.DEBUG:
+                try:
+                    set_werkzeug_log_color()
+                except:     # We are dealing with some internals, anything could go wrong
+                    pass
+
         except ImportError:
             raise CommandError("Werkzeug is required to use runserver_plus.  Please visit http://werkzeug.pocoo.org/ or install via pip. (pip install Werkzeug)")
 
@@ -233,3 +246,43 @@ def inner_run():
                 ssl_context=ssl_context
             )
         inner_run()
+
+
+def set_werkzeug_log_color():
+    """Try to set color to the werkzeug log.
+    """
+    from django.core.management.color import color_style
+    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)
+
+    WSGIRequestHandler.log = werk_log


Test patch: