0

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
  • 25,410
  • 6
  • 56
  • 79

2 Answers2

1

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,279
  • 3
  • 25
  • 36
0
from django.core.management import color_style
sys.stdout.write(color_style().WARNING("Colorful!"))
Dominique PERETTI
  • 945
  • 11
  • 12