Development and debuggingstdout logThe logs are highlighted in real time

The highlighted

It’s simply a search for a specific keyword and replace it with the ANSI Escape Code

  1. Canonical substitution,perl -pe 's/<pattern>; /<replacement>; /g'
  2. egrepKeyword substitutionegrep "fatal|error|warning|info|debug" -A10 -B10 --color=auto

Perl way can specify different keywords different colors, Egrep custom color estimation is cumbersome.

Cooperate with Linux pipeline actual test

Create a text file

Vim test.txt Enter the following:

error
xxxx
bbb
wr

fatal
info
debug
dkjk
info
error
warning
info
info

Perl-PE regularly replaces keywords

cat test.txt  | perl -pe 's/(fatal)/\e[0;41;37m$1\e[0m/g; s/(error)/\e[1;31m$1\e[0m/g; s/(warning)/\e[1;33m$1\e[0m/g; s/(info)/\e[1;32m$1\e[0m/g; s/(debug)/\e[1;34m$1\e[0m/g; s/(wechat)/\e[1;35m$1\e[0m/g'

Effect:

egrep

cat test.txt | egrep  "fatal|error|warning|info|debug" -A10 -B10 --color=auto

Effect:

stdout logTo deal with

Take the Golang program as an example

go test -v | egrep "fatal|error|warning|info|debug" -A10 -B10 --color=auto go test -v | perl -pe 's/(fatal)/\e[0;41;37m$1\e[0m/g; s/(error)/\e[1;31m$1\e[0m/g; s/(warning)/\e[1;33m$1\e[0m/g; s/(info)/\e[1;32m$1\e[0m/g;  s/(debug)/\e[1;34m$1\e[0m/g; s/(wechat)/\e[1;35m$1\e[0m/g'

go run main.go | ...Do the same thing as above.
golangThere’s one of these bags
github.com/fatih/color, you can directly in the program like this output, with a period of time, feel the log or only do log things, do not make flashy, to review the log, use these two ways to filter, or throw to
ELKGo inside.

This one is not going to be textured, With with tail -f -n 50 / usr/local/var/postgres/log/postgresql – 2018-05-24 _000000. Log | egrep “STATEMENT | log” – A10 – B10 –color=auto and cat above are the same.

Terminal console font color

Echo -e "\033[30m "echo -e "\033[31m" echo -e "\033[32m "echo -e "\033[33m" echo -e "\033[33m "echo -e "\033[33m" echo -e "\033[33m "echo -e "\033[33m" echo -e "\033[33m "echo -e "\033[33m" echo -e "\ \033[0m" echo -e "\033[34m "echo -e "\033[35m" echo -e "\033[36m "echo -e "\033[36m" echo -e "\033[37m "echo -e "\033[37m White word \ [0 m "echo - e" 033\033 [40; 37 m black white \ [0 m "echo - e" 033\033 \ [41; 37 m red bottom incorrect character, 033 [0 m "echo - e" \ [42, 033 37 m green white letters \ [0 033 m" Echo -e "\033[0m" echo -e "\033[0m" echo -e "\033[0m" echo -e "\033[45;37m "echo -e "\033[0m" echo -e "\033[45;37m" echo -e "\033[0m" echo -e "\033[0m" echo -e "\033[0m" echo -e "\033[45;37m "echo -e "\033[0m" echo -e "\033[46;37m \033[0m" echo -e "\033[47;30m \033[0m"

Detailed description:

\e[F;B;Om

Starting with the escape character, ESC’s ASCII code is 27 in decimal notation, which is equal to 033 in octal notation.

  • \e\ 033Declares the start of an escape sequence
  • [Start defining colors.
  • FIs the font color and number30 ~ 37;
  • BIs the background color and is numbered40 ~ 47.
  • OFor special meaning code
  • It doesn’t matter what order they are in.
  • mIs the tag
  • mIt is not followed by a space, is the defined color word and the background

The color table

Foreground background color ------------------------- 30 40 black 31 41 red 32 42 green 33 43 yellow 34 44 blue 35 45 magenta 36 46 cyan 37 47 white special code meaning -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 0 OFF 1 highlighted four underline 5 white flashing 7 showed eight are not visible

References:

  • Make the keyword filtering in the log cool
  • Console output color control (color display in Console mode)
  • A summary of regular expression usage in Perl