1

I'm on MACOS terminal. I'm trying to print colored output running a django test. I'm setting, from the terminal

export DJANGO_COLORS="error=yellow/blue,blink;notice=magenta"

(also tried DJANGO_COLORS="light" and others.

Then, from django test code

print(os.environ['DJANGO_COLORS'])
logger.error("test")

and run

python manage.py test

The first line verifies that the environment variable is set correctly. But the next line prints normally, without coloring. What is wrong?

blue_note
  • 27,712
  • 9
  • 72
  • 90

2 Answers2

2

From the django-admin syntax coloring documentation that deals with DJANGO_COLORS (emphasis mine):

The django-admin / manage.py commands will use pretty color-coded output if your terminal supports ANSI-colored output.

That doesn't mention output from the logging package. The referenced commands use a specific kind of stdout.write invocation that should take DJANGO_COLORS into account.

If what you want is colors in your logging, you might want to check out the answers to this relevant question.

kungphu
  • 4,592
  • 3
  • 28
  • 37
0
from django.core.management import color_style
sys.stdout.write(color_style().WARNING("Colorful!"))
Dominique PERETTI
  • 1,063
  • 12
  • 12