【问题标题】:Colorize tail output着色尾部输出
【发布时间】:2013-01-19 10:39:36
【问题描述】:

我一直在努力让 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,但唯一幸存的颜色是管道中的最后一个命令。

是否有一个班轮可以做到这一点?甚至是多班轮?

【问题讨论】:

标签: linux bash shell


【解决方案1】:

我写了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

【讨论】:

    【解决方案2】:

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

    grc tail -f /var/log/syslog
    

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

    在此处查找工具:

    https://github.com/garabik/grc

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

    【讨论】:

      【解决方案3】:

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

      对于php是这样的:

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

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

      More info here

      【讨论】:

      • 当链接到您自己的网站或内容(或您附属的内容)时,您must disclose your affiliation in the answer 以免被视为垃圾邮件。根据 Stack Exchange 政策,在您的用户名中包含与 URL 相同的文本或在您的个人资料中提及它不被视为充分披露。
      【解决方案4】:

      我在几年前写了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.
      

      【讨论】:

        【解决方案5】:

        是的,有办法做到这一点。也就是说,只要你的终端支持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)。

        查看结果:

        【讨论】:

        • 如果您只想为匹配的关键字着色,而不是整行,请在 sed 命令中的“&”后立即添加“^[[0m”。
        • @sehe ...是的,打字有点懒惰... sry..顺便说一句,我会支持您的\x1b编辑;可惜gif不能编辑。 :)
        • @Kent 我意识到了这一点,否则我一开始就不会提到^[ :)
        • 我对你的风格印象深刻 :) 不错的终端
        • 不确定它是否是终端问题,但 ^[[0m 除了在匹配结束时回显这些字符外,没有为我做任何事情,即使我退出命令,颜色仍然存在。所以,我也匹配例如#WARN#并将其变成#^.*WARN.*$#,并且我在符号后面使用了\x1b [0m。不过,感谢您的帮助,我现在已经可以正常工作了,而且可以更轻松地查看服务器是否在启动时遇到问题。
        【解决方案6】:

        我使用了我破解的这个版本: python log watcher

        【讨论】:

          猜你喜欢
          • 2011-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-10
          • 1970-01-01
          • 1970-01-01
          • 2021-10-25
          • 1970-01-01
          相关资源
          最近更新 更多