In the process of developing projects, in order to facilitate the debugging code, often to stdout output some logs, the default of these logs are directly displayed in the terminal. The general application server, third-party libraries, and even some notices from the server will also be displayed in the terminal, which messes up the information we want.
We can achieve eye-catching effect by setting different colors for useful information, because I usually develop under linux, and the colors in the linux terminal are controlled by escape sequences, which start with ESC, and you can use \033 to do the same job (the ASCII code of ESC is 27 in decimal, which is equal to 33 in octal).
The writing format, and related instructions are as follows.
Format:\033 [Display Mode;Foreground Color;Background Colorm
Description:
Foreground Background Color Color
---------------------------------------
30 40 Black
31 41 Red
32 42 Green
33 43 Yellow
34 44 Blue
35 45 Fuchsia
36 46 Cyan blue
37 47 White
Display Meaning
-------------------------
0 Terminal default settings
1 Highlighting
4 Use of underlining
5 Scintillation
7 Highlighting
8 Invisible
Example:
\033[1;31;40m <! --1-Highlight 31-Foreground color red 40-Background color black -->.
\033[0m <! --The terminal default setting is used, i.e., the color setting is canceled -->
Here's the way I use it in python:
print '*' * 50
print '*HOST:\t', ('REMOTE_ADDR')
print '*URI:\t',
print '*ARGS:\t', QueryDict()
print '*TIME:\t', () - request.start_time
print '*' * 50
print '\033[0m'
The rendering is as follows:
Of course this is only a simple implementation, and only works under linux, other ways can use termcolor, or refer to ipython's console implementation (pyreadline).