着色尾部输出

tpe*_*son 23 linux bash shell

我一直在努力让服务器初创公司的尾部更具可读性.我当前的命令过滤掉了启动时的大部分INFO和DEBUG消息:

tail -F ../server/durango/log/server.log | grep -e "ERROR" -e "WARN" -e "Shutdown" -e "MicroKernel" | grep --color=auto -E 'MicroKernel|$'
Run Code Online (Sandbox Code Playgroud)

我想做的是制作一些突出显示黄色的WARN和红色的ERROR以及绿色的MicroKernel.我试过多次管道grep --color = auto,但幸存的唯一颜色是管道中的最后一个命令.

这样做有一个班轮吗?甚至是很多班轮?

Ken*_*ent 27

是的,有办法做到这一点.也就是说,只要您的终端支持ANSI转义序列.这是大多数存在的终端.

我想我不需要解释如何grep,sed等点是颜色吧?

见下文,这将使

WARN yellow
ERROR red
foo   green
Run Code Online (Sandbox Code Playgroud)

这是一个例子:

kent$ echo "WARN
ERROR
foo"|sed 's#WARN#\x1b[33m&#; s#ERROR#\x1b[31m&#; s#foo#\x1b[32m&#'
Run Code Online (Sandbox Code Playgroud)

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

看到结果:

在此输入图像描述

  • 如果您只想着色匹配的关键字而不是整行,请在`sed`命令中的&符号后面添加"^ [[0m". (5认同)
  • 我将oneliner编辑成更易于复制/粘贴的内容. (3认同)

Dav*_*ell 6

我多年前写过一个剧本.您可以通过连续相互调用来轻松覆盖多种颜色的情况highlight.

来自README:

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.
Run Code Online (Sandbox Code Playgroud)


kim*_*san 5

多年来,我一直在使用一个名为 grc 的工具。奇迹般有效。它为许多标准日志输出和格式提供了一些非常好的模板,并且很容易定义您自己的模板。我经常使用的一个命令是

grc tail -f /var/log/syslog
Run Code Online (Sandbox Code Playgroud)

它为系统日志输出着色,因此很容易发现错误(通常标记为红色。

在此处找到该工具:

https://github.com/garabik/grc

(它也可以作为最常见的 linux 风格的包提供)。


arm*_*ino 5

我编写了TxtStyle,一个用于对日志进行着色的小实用程序。您定义正则表达式以在~/.txts.conf文件中突出显示:

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

然后应用样式:

txts -n example example.log
Run Code Online (Sandbox Code Playgroud)

或者你也可以通过管道输出

tail -f example.log | txts -n example
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述