linux 为尾部输出着色

xnifntxz  于 2022-12-11  发布在  Linux
关注(0)|答案(6)|浏览(136)

我一直在尝试让tail对于服务器启动程序更容易阅读。我当前的命令过滤掉了启动程序中的大多数INFO和DEBUG消息:

tail -F ../server/durango/log/server.log | grep -e "ERROR" -e "WARN" -e "Shutdown" -e "MicroKernel" | grep --color=auto -E 'MicroKernel|$'

我想做的是创建一些东西,用黄色突出显示WARN,用红色突出显示ERROR,用绿色突出显示MicroKernel。我多次尝试只使用管道grep --color=auto,但唯一保留下来的颜色是管道中的最后一个命令。
有没有一个行程序可以做到这一点?或者甚至有一个多行程序?

yqkkidmi

yqkkidmi1#

是,有办法做到这一点,也就是说,只要您终端支持**ANSI escape sequences**,这是大多数现有终端
我想我不需要解释如何grep,sed等点的颜色是正确的?
见下文,这将使

WARN yellow
ERROR red
foo   green

以下是示例:

kent$ echo "WARN
ERROR
foo"|sed 's#WARN#\x1b[33m&#; s#ERROR#\x1b[31m&#; s#foo#\x1b[32m&#'

注意\x1b是十六进制的 ESC 字符(^VEsc)。

查看结果:

nkkqxpd9

nkkqxpd92#

我在几年前就为此编写了a script。通过将highlight的连续调用相互连接,可以很容易地覆盖多种颜色的情况。
读我档案:

Usage: ./highlight [-i] [--color=COLOR_STRING] [--] <PATTERN0> [PATTERN1...]

This is highlight version 1.0.

This program takes text via standard input and outputs it with the given
perlre(1) pattern(s) highlighted with the given color.  If no color option
is specified, it defaults to 'bold red'.  Colors may be anything
that Perl's Term::ANSIColor understands.  This program is similar to
"grep --color PATTERN" except both matching and non-matching lines are
printed.

The default color can be selected via the $HIGHLIGHT_COLOR environment
variable.  The command-line option takes precedence.

Passing -i or --ignore-case will enable case-insensitive matching.

If your pattern begins with a dash ('-'), you can pass a '--' argument
after any options and before your pattern to distinguish it from an
option.
du7egjpx

du7egjpx3#

多年来我一直在使用一个名为grc的工具来实现这一点。工作起来很有魅力。它为许多标准日志输出和格式提供了一些相当不错的模板,并且很容易定义自己的模板。我经常使用的一个命令是

grc tail -f /var/log/syslog

它将系统日志输出着色,以便容易发现错误(通常标记为红色)。
在此处查找工具:
https://github.com/garabik/grc
(it也可作为用于大多数常见Linux风格的包来获得)。

yqlxgs2m

yqlxgs2m4#

我写了TxtStyle,一个给日志着色的小工具。你可以定义正则表达式在~/.txts.conf文件中高亮显示:

[Style="example"]
!red: regex("error")
green: regex("\d{4}-\d\d-\d\d")
# ...

然后套用样式:

txts -n example example.log

也可以通过管道输出

tail -f example.log | txts -n example

ugmeyewa

ugmeyewa5#

您可以创建彩色日志,而不必使用复杂的命令。

对于php是这样的:

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

**关键是使用Ctrl-v ctrl-[输入一个绿色^[**在vim的插入模式下,直接输入^[不起作用。

More info here

kqlmhetl

kqlmhetl6#

My sample using awk。匹配日志格式,如:xxxx [debug] xxxxx xxxx xxxx

black=30m
red=31m
green=32m
yellow=33m
blue=34m
magenta=35m
cyan=36m
white=37m

blacklog="\"\033[$black\" \$0 \"\033[39m\""
redlog="\"\033[$red\" \$0 \"\033[39m\""
greenlog="\"\033[$green\" \$0 \"\033[39m\""
yellowlog="\"\033[$yellow\" \$0 \"\033[39m\""
bluelog="\"\033[$blue\" \$0 \"\033[39m\""
magentalog="\"\033[$magenta\" \$0 \"\033[39m\""
cyanlog="\"\033[$cyan\" \$0 \"\033[39m\""
whitelog="\"\033[$white\" \$0 \"\033[39m\""

trace="/\[trace\]/ {print $redlog}"
debug="/\[debug\]/ {print $magentalog}"
info="/\[info\]/ {print $greenlog}"
warning="/\[warning\]/ {print $bluelog}"
error="/\[error\]/ {print $yellowlog}"

yourcommand | awk "$trace $debug $info $warning $error"

相关问题