Developing debug stdout log
Logs for real-time highlighting
Highlight Display
In fact, the search for specific keywords to replace the ANSI escape code
- Regular replacement,
perl -pe 's/<pattern>;/<replacement>;/g'
egrep
Keyword substitutionegrep "fatal|error|warning|info|debug" -A10 -B10 --color=auto
Perl's way to specify different keywords for different colors, egrep custom color estimation is cumbersome.
Practical testing with Linux pipelines
Create a text file
vim test.txt
Enter the following content:
errorxxxxbbbwrfatalinfodebugdkjkinfoerrorwarninginfoinfo
PERL-PE Regular Replacement 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 log
Processing
Let's take the golang
procedure as an example
go test -v | egrep "fatal|error|warning|info|debug" -A10 -B10 --color=autogo 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 | ...
deal with it as above.
golang
There is a package github.com/fatih/color, you can directly in the program in such a sub-output, with a period of time, feel the log or only do the log thing, do not make a gaudy, to review log time, use these two ways to filter, or throw to
ELK
go inside.
This does not map, with the tail -f -n 50 /usr/local/var/postgres/log/postgresql-2018-05-24_000000.log | egrep "STATEMENT|LOG" -A10 -B10 --color=auto
same as with the above cat
effect is the same.
Terminal Console Font Color
echo -e "\033[30m 黑色字 \033[0m"echo -e "\033[31m 红色字 \033[0m"echo -e "\033[32m 绿色字 \033[0m"echo -e "\033[33m 黄色字 \033[0m"echo -e "\033[34m 蓝色字 \033[0m"echo -e "\033[35m 紫色字 \033[0m"echo -e "\033[36m 天蓝字 \033[0m"echo -e "\033[37m 白色字 \033[0m" echo -e "\033[40;37m 黑底白字 \033[0m"echo -e "\033[41;37m 红底白字 \033[0m"echo -e "\033[42;37m 绿底白字 \033[0m"echo -e "\033[43;37m 黄底白字 \033[0m"echo -e "\033[44;37m 蓝底白字 \033[0m"echo -e "\033[45;37m 紫底白字 \033[0m"echo -e "\033[46;37m 天蓝底白字 \033[0m"echo -e "\033[47;30m 白底黑字 \033[0m"
Detailed Description:
\e[F;B;Om
\e
The escape character begins with ESC
a ASCII
decimal representation of the code, which is 27
equal to the octal representation 033
.
\e
Or \033
declares the beginning of the escape sequence
[
Start defining colors.
F
For font color, numbering 30~37
;
B
For background color, numbering 40~47
.
O
For special meaning code
- Their order is not related.
m
is a marker
m
There is no space behind, it is defined by the color Word and background
Color table
前景 背景颜色 ------------------------- 30 40 黑色 31 41 红色 32 42 绿色 33 43 黄色 34 44 蓝色 35 45 洋红 36 46 青色 37 47 白色 特别代码意义 ------------------------- 0 OFF 1 高亮显示 4 underline 5 闪烁 7 反白显示 8 不可见
Resources:
- Let the keywords in the log filter cool
- Console output Color control (color display in console mode)
- Summary of the usage of the Perl Express expressions