颜色化tail输出

32

我一直在尝试使服务器启动时的日志输出更易读。我的当前命令会过滤掉大部分启动时的 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,但只有最后一条命令的颜色能够保留下来。 是否有一种方法可以用一行命令或几行命令实现此功能?

如果你只想获取红色,可以使用grep命令,它更容易一些,因为你不需要知道任何 ANSI 转义序列。 - Trevor Boyd Smith
如果您在一个不方便安装非标准工具的服务器上,可以使用sed或awk。 - Nick Dong
6个回答

33

是的,有方法可以做到这一点。只要您的终端支持ANSI转义序列,这适用于大多数现有终端。

我认为我不需要解释如何使用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)。

查看结果:

输入图像描述


5
如果你只想给匹配的关键词上色而不是整行文字,那么在 sed 命令中的“&”符号后面立即添加“^[[0m”。请注意,这不会改变原意。 - chepner
@sehe...是的,有点懒得打那些...抱歉...顺便说一下,我会给你的\x1b编辑点赞;可惜gif不能被编辑。 :) - Kent
1
@Kent 我意识到了,否则我一开始就不会提到 ^[ 了 :) - sehe
1
我对你的风格印象深刻 :) 终端很棒 - Satish
1
不确定是否是终端问题,但 ^[[0m 对我来说除了在匹配结尾处回显这些字符外没有任何作用,而且颜色即使在退出命令后仍然存在。因此,我将匹配项例如 #WARN# 转换为 #^.WARN.$#,并在和号后使用 \x1b[0m。虽然感谢您的帮助,但现在我已经解决了它,如果服务器启动出现问题,它会更容易看到。 - tpederson
显示剩余2条评论

10

我几年前就写了一个脚本。 通过将连续的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.

8

多年来,我一直在使用一个叫做grc的工具。它非常好用,可以为许多标准日志输出和格式提供一些相当不错的模板,并且很容易定义自己的模板。 我经常使用的一个命令是

grc tail -f /var/log/syslog

它可以将syslog输出进行着色,以便更容易发现错误(通常标记为红色)。
在这里找到该工具: https://github.com/garabik/grc (它也可作为大多数常见Linux版本的软件包提供)。

我喜欢这个工具的通用性,来自apt:“适用于所有内容的通用着色器”,因此它可以处理syslog文件以及ls输出等。最重要的是,它已经包含在我的Debian / Ubuntu版本中。 - Gerben Versluis
我非常喜欢它,以至于我在/etc/profile.d/grc.sh中放置了一个文件,如果安装了grc,则默认使用grc进行cat/head/tail:if [ -x /usr/bin/grc ]; then alias cat='grc --colour auto cat'; alias tail='grc --colour auto tail'; alias head='grc --colour auto head'; fi - Gerben Versluis

5
我写了一个名为 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

enter image description here


1
这非常好,非常感谢,解决了我一个紧急的问题! - JrRockeTer
@JrRockeTer 很高兴你觉得它有用 :) - armandino

0

您可以创建一个带颜色的日志,而不是使用复杂的命令。

enter image description here

对于 PHP 来说,就是这样的:

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

关键是在vim的插入模式下使用Ctrl-v ctrl-[输入绿色的 ^[,直接输入 ^[ 是无效的。

enter image description here

更多信息在这里


2
当链接到您自己的网站或内容(或与您有关联的内容)时,您必须在答案中披露您的关联,以便不被视为垃圾邮件。根据Stack Exchange政策,在您的用户名中具有相同的文本作为URL或在您的个人资料中提及它不被视为充分的披露。 - Sabito stands with Ukraine

-1

我使用 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"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接