How to have tail -f show colored output

In the morning, I saw a friend curl a weather forecast HTTP resource on the shell terminal and found that the output text is colored, which inspired my interest in outputting colored text logs in the terminal to enhance readability.

Colorizing shell terminal
Colorizing shell terminal
Colorizing shell terminal

The key point is to use Ctrl-v ctrl-[ to input a green ^[ under insert mode in vim, direct input ^[ is not working, the ASCII/UTF-8 encoding is 1B instead of 5E5B. 0A is new line

Colorizing shell terminal

The PHP code is like this(Remember to use Ctrl-v ctrl-[ to input a green ^[ )

echo "^[[30;43m".$ip."^[[0m";
Colorizing shell terminal

Cyan interface name, yellow request data and purple response data, readability is quite high compared to all white logs

Leave a Reply

Your email address will not be published. Required fields are marked *