StdOut log log real-time highlighting

Source: Internet
Author: User
Tags egrep

Developing debug stdout log Logs for real-time highlighting

Highlight Display

In fact, the search for specific keywords to replace the ANSI escape code

    1. Regular replacement,perl -pe 's/<pattern>;/<replacement>;/g'
    2. egrepKeyword 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 logProcessing

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. golangThere 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 ELKgo 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

\eThe escape character begins with ESC a ASCII decimal representation of the code, which is 27 equal to the octal representation 033 .

    • \eOr \033 declares the beginning of the escape sequence
    • [Start defining colors.
    • FFor font color, numbering 30~37 ;
    • BFor background color, numbering 40~47 .
    • OFor special meaning code
    • Their order is not related.
    • mis a marker
    • mThere 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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.