Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#31924 closed New feature (invalid)

Support --force-color for runserver

Reported by: Zach Waggoner Owned by: nobody
Component: Core (Management commands) Version: 3.1
Severity: Normal Keywords: runserver, force_color
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Zach Waggoner)

Currently, the --force-color option doesn't work for the runserver management command. In my own experimentation, I was able to force color output by changing self.style = color_style() to self.style = color_style(force_color=True) in django.utils.log.ServerFormatter. I can of course achieve this by using my own django.server formatter class, but it would be convenient to have it built in.

Change History (5)

comment:1 by Mariusz Felisiak, 4 years ago

Resolution: invalid
Status: newclosed

It already works properly, you just need to set the DJANGO_COLORS environment variable to reach ServerFormatter, see docs.

in reply to:  1 comment:2 by Zach Waggoner, 4 years ago

Replying to felixxm:

It already works properly, you just need to set the DJANGO_COLORS environment variable to reach ServerFormatter, see docs.

I might be missing something, but that doesn't work for me. As an example, trying to run this Procfile through honcho does not preserve the color output:

web: DJANGO_COLORS=dark python manage.py runserver

and neither does:

web: DJANGO_COLORS=dark python manage.py runserver --force-color

This is on Django 3.1.

Last edited 4 years ago by Zach Waggoner (previous) (diff)

comment:3 by Mariusz Felisiak, 4 years ago

It looks that Django doesn't recognize that your system's terminal supports color, we have an open ticket to improve detection on Windows, see #31216. You may also fake the installation of ANSICON by setting the appropriate environmental variable, ANSICON="on". Please use one of support channels.

in reply to:  3 comment:4 by Zach Waggoner, 4 years ago

Replying to felixxm:

It looks that Django doesn't recognize that your system's terminal supports color, we have an open ticket to improve detection on Windows, see #31216. You may also fake the installation of ANSICON by setting the appropriate environmental variable, ANSICON="on". Please use one of support channels.

I'm on macOS, so if what you are saying is true, then perhaps we need to open a ticket to improve detection on macOS? However, I don't think that is the problem, because running runserver directly (not in a non-TTY context like honcho) does output colors. Furthermore, running a different management command like migrate with the --force-color option also preserves the color output when run through honcho. Therefore the problem is specific to runserver, and besides, I thought the point of --force-color is to bypass the color support detection altogether.

I glanced through the source code, and it looks like django.core.management.base.BaseCommand handles the --force-color option by setting self.style = color_style(force_color). Then the specific commands which inherit from BaseCommand, like migrate, use self.style when writing to stdout. However, runserver, while inheriting from BaseCommand, doesn't ever reference self.style, nor does it do anything else with --force-color. Instead, the style object is defined in django.utils.log.ServerFormatter as self.style = color_style(), so force_color is never True regardless of any environment variables. That is why I said in my original comment that the current way to solve this is to override ServerFormatter, but it seems like Django could support --force-color for runserver, perhaps by setting an environment variable that is picked up by the default ServerFormatter? Or if there are reasons not to do this, then perhaps the documentation should be updated to note that runserver does not support --force-color.

Version 2, edited 4 years ago by Zach Waggoner (previous) (next) (diff)

comment:5 by Zach Waggoner, 4 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top