Colorize tail output

后端 未结 6 553
慢半拍i
慢半拍i 2021-01-30 07:27

I\'ve been trying to make tail a little more readable for server startups. My current command filters out most of the INFO and DEBUG messages from the startup:

         


        
相关标签:
6条回答
  • 2021-01-30 07:47

    I wrote TxtStyle, a small utility for colorising logs. You define regular expressions to highlight in ~/.txts.conf file:

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

    And then apply the styles:

    txts -n example example.log
    

    or you can also pipe the output

    tail -f example.log | txts -n example
    

    0 讨论(0)
  • 2021-01-30 07:57

    I wrote a script for this years ago. You can easily cover the case of multiple colors by piping successive invocations of highlight to each other.

    From the 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.
    
    0 讨论(0)
  • 2021-01-30 07:59

    I have been using a tool called grc for this for years. works like a charm. It comes with some quite good templates for many standard log outputs and formats and it is easy to define your own. A command I use often is

    grc tail -f /var/log/syslog
    

    It colorizes the syslog output so it is easy to spot errors (typically marked red.

    Find the tool here:

    https://github.com/garabik/grc

    (it is also available as package for most common linux flavours).

    0 讨论(0)
  • 2021-01-30 08:02

    yes, there is way to do this. That is, as long as your terminal supports ANSI escape sequences. This is most terminals that exist.

    I think I don't need explain how to grep, sed etc. point is the color right?

    see below, this will make

    WARN yellow
    ERROR red
    foo   green
    

    here is example:

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

    Note: \x1b is hexadecimal for the ESC character (^VEsc).

    to see the result:

    enter image description here

    0 讨论(0)
  • 2021-01-30 08:09

    I use a version of this that I hacked: python log watcher

    0 讨论(0)
  • 2021-01-30 08:10

    You can create a colored log instead of using a complex command.

    enter image description here

    For php is like this:

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

    The key point is to use Ctrl-v ctrl-[ to input a green ^[ under insert mode in vim, direct input ^[ does not work.

    enter image description here

    More info here

    0 讨论(0)
提交回复
热议问题