[logs] log function color command line tool

Time:2021-8-7

Record before optimize

When using the command line to view the log, if you want to have color, the simple script is as follows.

(Version 1)The following can only match a single line

colorize.sh

#!/bin/bash
#The following errors are matched and case sensitive. The example here is the log of springboot
# Example:
# 2021-07-14 14:34:19.222 DEBUG 5960 --- [http-nio-8089-exec-3] c.y.c.b.m.P.selectList                   : <==      Total: 8
awk '
function color(c,s) {
        printf("3[%dm%s3[0m\n",30+c,s)
}
/ERROR/ {color(1,$0);next}
/SUCCESS/ {color(2,$0);next}
/WARN/ {color(3,$0);next}
/INFO/ {color(7,$0);next}
/DEBUG/ {color(6,$0);next}
{print}
' $1
(Version 2)Match multiple rows
#!/bin/bash
#Just put the matching error at the end
awk '
function color(c,s) {
   printf("3[%dm%s3[0m\n",30+c,s)
}
/SUCCESS/ {color(2,$0);next}
/WARN/ {color(3,$0);next}
/INFO/ {color(7,$0);next}
/DEBUG/ {color(6,$0);next}
/ERROR/,/INFO|DEBUG|WARN|SUCCESS/ {color(1,$0);next}
{print}
' $1
(Version 3)Match multiple rows
#!/bin/bash
awk '
function color(c,s) {
        printf("3[%dm%s3[0m\n",30+c,s)
}
/ERROR/,/INFO|DEBUG|WARN|SUCCESS/ { if ($0 !~ /INFO|DEBUG|WARN|SUCCESS/) color(1,$0);next}
/SUCCESS/ {color(2,$0);next}
/WARN/ {color(3,$0);next}
/INFO/ {color(7,$0);next}
/DEBUG/ {color(6,$0);next}
{print}
' $1

(PowerShell version 1)Rely on awk in gunwin32
awk '
function color (f,c) 
{
    f=f+30;
    #Only 033 can be used instead of E
    message = \"3[\" f \"m\" c \"3[0m\";
    print message;
}
/ERROR/,/INFO|DEBUG|WARN|SUCCESS/ { if ($0 !~ /INFO|DEBUG|WARN|SUCCESS/) color(1,$0);next}
/SUCCESS/ { color(2,$0);next}
/WARN/ { color(3,$0);next}
/INFO/ { color(7,$0);next}
/DEBUG/ { color(6,$0);next}
{print}' $args[0];

# Example:
$ colorize .\logs\spring.log

to configure:

Import-Module posh-git
Import-Module oh-my-posh
# -Theme
Set-PoshPrompt powerlevel10k_rainbow

Set-Alias awk "I:\commandLineTools\linux\GnuWin32\bin\awk.exe"
Set-Alias sed "I:\commandLineTools\linux\GnuWin32\bin\sed.exe"
Set-Alias colorize "I:\commandLineTools\windows\colorize.ps1"

(PowerShell version 2)Rely on awk in gunwin32
Function colorize {
    [cmdletbinding()]
    Param(
        $arg0,
        [parameter(ValueFromPipeline=$True)]
        [string[]] $InputValue
    )

    Begin {
        $txt = @();
        $isPipLine = $False;
    }

    Process {
         $value = If ($InputValue) { $InputValue } Else { $arg0 }
         If (($null -ne $InputValue) -and ($arg0 -eq $null)) {
             $isPipLine = $True;
             $txt+=$InputValue
         } Else {
             awk '
                function color (f,c) 
                {
                    f=f+30;
                    #Only 033 can be used instead of E
                    message = \"3[\" f \"m\" c \"3[0m\";
                    print message;
                }
                /ERROR/,/INFO|DEBUG|WARN|SUCCESS/ { if ($0 !~ /INFO|DEBUG|WARN|SUCCESS/) color(1,$0);next}
                /SUCCESS/ { color(2,$0);next}
                /WARN/,/INFO|DEBUG|ERROR|SUCCESS/ { if ($0 !~ /INFO|DEBUG|ERROR|SUCCESS/) color(3,$0);next}
                /INFO/ { color(7,$0);next}
                /DEBUG/ { color(6,$0);next}
                {print}' $value;
         }
    }

    End {
        If ($isPipLine) {
            $txt+=$InputValue;
            $txt -Join [Environment]::NewLine | awk '
                    function color (f,c) 
                    {
                        f=f+30;
                        #Only 033 can be used instead of E
                        message = \"3[\" f \"m\" c \"3[0m\";
                        print message;
                    }
                    /ERROR/,/INFO|DEBUG|WARN|SUCCESS/ { if ($0 !~ /INFO|DEBUG|WARN|SUCCESS/) color(1,$0);next}
                    /SUCCESS/ { color(2,$0);next}
                    /WARN/,/INFO|DEBUG|ERROR|SUCCESS/ { if ($0 !~ /INFO|DEBUG|ERROR|SUCCESS/) color(3,$0);next}
                    /INFO/ { color(7,$0);next}
                    /DEBUG/ { color(6,$0);next}
                    {print}';
            #Echo "through pipe"

        } Else {
            #Echo "call file directly"
        }
    }
}

# Example:
$ sed -n '/2021-07-16 23:31:34/,$p' .\logs\spring.log | colorize
# OR
$ colorize .\logs\spring.log
(PowerShell version 3) color256
Function colorize {
    [cmdletbinding()]
    Param(
        $arg0,
        [parameter(ValueFromPipeline=$True)]
        [string[]] $InputValue
    )

    Begin {
        $txt = @();
        $isPipLine = $False;

        $script = '
                function color (f,c) 
                {
                    f=f+30;
                    #Only 033 can be used instead of E
                    message = \"3[\" f \"m\" c \"3[0m\";
                    print message;
                }
                function color256 (f,c) 
                {
                    #Only 033 can be used instead of E
                    message = \"3[38;5;\" f \"m\" c \"3[0m\";
                    print message;
                }
                /ERROR/,/INFO|DEBUG|WARN|SUCCESS/ { if ($0 !~ /INFO|DEBUG|WARN|SUCCESS/) color(1,$0);next}
                /SUCCESS/ { color(2,$0);next}
                /WARN/,/INFO|DEBUG|ERROR|SUCCESS/ { if ($0 !~ /INFO|DEBUG|ERROR|SUCCESS/) color(3,$0);next}
                /INFO/ { color256(8,$0);next}
                /DEBUG/ { color(6,$0);next}
                {print}';
    }

    Process {
         $value = If ($InputValue) { $InputValue } Else { $arg0 }
         If (($null -ne $InputValue) -and ($arg0 -eq $null)) {
             $isPipLine = $True;
             $txt+=$InputValue
         } Else {
             awk $script $value;
         }
    }

    End {
        If ($isPipLine) {
            $txt -Join [Environment]::NewLine | awk $script;
            #Echo "through pipe"

        } Else {
            #Echo "call file directly"
        }
    }
}
(PowerShell version 4) color256 and pipeline
Function colorize {
    [cmdletbinding()]
    Param(
        $arg0,
        [parameter(ValueFromPipeline=$True)]
        [string[]] $InputValue,
        [Switch] $IsPipeline
    )
    #Todo: processing pipeline character acceptance
    # $arg0
    # $InputValue

    Begin {
        $txt = @();
        $isPipLineNotByProcess = $False;

        $script = '
                function color (f,c) 
                {
                    f=f+30;
                    #Only 033 can be used instead of E
                    message = \"3[\" f \"m\" c \"3[0m\";
                    print message;
                }
                function color256 (f,c) 
                {
                    #Only 033 can be used instead of E
                    message = \"3[38;5;\" f \"m\" c \"3[0m\";
                    print message;
                }
                /ERROR/,/INFO|DEBUG|WARN|SUCCESS/ { colorValue=1; if ($0 !~ /INFO|DEBUG|WARN|SUCCESS/) color(1,$0);next}
                /SUCCESS/ { colorValue=2; color(2,$0);next}
                /WARN/,/INFO|DEBUG|ERROR|SUCCESS/ { colorValue=3; if ($0 !~ /INFO|DEBUG|ERROR|SUCCESS/) color(3,$0);next}
                /INFO/ { colorValue=8; color256(8,$0);next}
                /DEBUG/ { colorValue=6; color(6,$0);next}
                {print}';

        $colorValue="6";

        $script2 = '
                function color256 (f,c) 
                {
                    #Only 033 can be used instead of E
                    message = \"3[38;5;\" f \"m\" c \"3[0m\";
                    print message;
                }
                /ERROR/,/INFO|DEBUG|WARN|SUCCESS/ { colorValue=1; if ($0 !~ /INFO|DEBUG|WARN|SUCCESS/) color256(1,$0);next}
                /SUCCESS/ { colorValue=10; color256(10,$0);next}
                /WARN/,/INFO|DEBUG|ERROR|SUCCESS/ { colorValue=3; if ($0 !~ /INFO|DEBUG|ERROR|SUCCESS/) color256(3,$0);next}
                /INFO/ { colorValue=8; color256(8,$0);next}
                /DEBUG/ { colorValue=6; color256(6,$0);next}
                {color256(colorValue, $0)}';

        $dynamicColorValue='{ 
                switch ($0) {
                    case /ERROR/: print 1; break;
                    case /SUCCESS/: print 10; break;
                    case /WARN/: print 3; break;
                    case /INFO/:; print 8; break;
                    case /DEBUG/:; print 6; break;
                    default: if (colorValue > 0) { print colorValue } else { print 15 }; break;
                } 
            }';
    }

    #Process has been listening continuously
    Process {
         $value = If ($InputValue) { $InputValue } Else { $arg0 }
         If (($null -ne $InputValue) -and ($arg0 -eq $null) -and (!$IsPipeline)) {
             $isPipLineNotByProcess = $True;
             $txt+=$InputValue;
         }Elseif ($ispipeline) {# must have - ispipeline
             $colorValue=($value | awk -v colorValue=$colorValue $dynamicColorValue);
             $value | awk -v colorValue=$colorValue $script2;
         } Else {
             awk $script $value;
         }
    }

    End {
        If ($isPipLineNotByProcess) {
            $txt+=$InputValue;
            $txt -Join [Environment]::NewLine | awk $script;
            #Echo "through pipe"

        } Else {
            #Echo "call file directly"
        }
    }
}

Function color256() {
    foreach($fgbg in 38,48)# Foreground / Background
    {
        foreach($color in 0..255) # Colors
        {
            # Display the color
            Write-Host -NoNewline ("`e[${fgbg};5;${color}m  {0,3}  `e[0m" -f $color);
            # Display 6 colors per lines
            if( (($color + 1) % 8) -eq 0 ) {
                #Using [environment]: newline will result in a blank line in the middle
                echo `r;
            }
        } 
        echo `r;
    }
}

This PowerShell version 2, 3 and 4 requires configuration$profileCorrespondingMicrosoft.PowerShell_profile.ps1

Import-Module posh-git
Import-Module oh-my-posh
# -Theme
Set-PoshPrompt powerlevel10k_rainbow

Set-Alias awk "I:\commandLineTools\linux\GnuWin32\bin\awk.exe"
Set-Alias sed "I:\commandLineTools\linux\GnuWin32\bin\sed.exe"
Import-Module "I:\commandLineTools\windows\colorize.ps1"

$awkLogFormatScript='
function color (f,c) 
{
    f=f+30;
    #Only 033 can be used instead of E
    message = \"3[\" f \"m\" c \"3[0m\";
    print message;
}
function color256 (f,c) 
{
    #Only 033 can be used instead of E
    message = \"3[38;5;\" f \"m\" c \"3[0m\";
    print message;
}
/ERROR/,/INFO|DEBUG|WARN|SUCCESS/ { if ($0 !~ /INFO|DEBUG|WARN|SUCCESS/) color(1,$0);next}
/SUCCESS/ { color(2,$0);next}
/WARN/,/INFO|DEBUG|ERROR|SUCCESS/ { if ($0 !~ /INFO|DEBUG|ERROR|SUCCESS/) color(3,$0);next}
/INFO/ { color256(8,$0);next}
/DEBUG/ { color(6,$0);next}
{print}';

Java commands run jar redirection to process output

Example1:

$ java -jar .\target\demo.jar --spring.profiles.active=local | awk $awkLogFormatScript

Microsoft.PowerShell_profile.ps1

$PSDefaultParameterValues = @{'Out-File:encoding'='utf8'}
Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt  powerlevel10k_rainbow

Set-Alias docker-tags -Value "E:\docker-tag\docker-tags.ps1"
Set-Alias awk "I:\commandLineTools\linux\GnuWin32\bin\awk.exe"
Set-Alias sed "I:\commandLineTools\linux\GnuWin32\bin\sed"
Import-Module "I:\commandLineTools\windows\colorize.ps1"
$awkLogFormatScript='
function color (f,c) 
{
    f=f+30;
    #Only 033 can be used instead of E
    message = \"3[\" f \"m\" c \"3[0m\";
    print message;
}
function color256 (f,c) 
{
    #Only 033 can be used instead of E
    message = \"3[38;5;\" f \"m\" c \"3[0m\";
    print message;
}
/ERROR/,/INFO|DEBUG|WARN|SUCCESS/ { if ($0 !~ /INFO|DEBUG|WARN|SUCCESS/) color(1,$0);next}
/SUCCESS/ { color(2,$0);next}
/WARN/,/INFO|DEBUG|ERROR|SUCCESS/ { if ($0 !~ /INFO|DEBUG|ERROR|SUCCESS/) color(3,$0);next}
/INFO/ { color256(8,$0);next}
/DEBUG/ { color(6,$0);next}
{print}';

Example:

# test.log
$ cat > ./test.log <<EOF
AA SDF
BB DERT
CC DERA
DD TYU
EE ASDF
FF DERT
EOF
awk '
/^BB/,/FF/{
print
getline
}' ./test.log
# Output:
#   BB DERT
#   DD TYU
#   FF DERT
awk '
/BB/,/FF/{
print
}' ./test.log
# Output:
#   BB DERT
#   CC DERA
#   DD TYU
#   EE ASDF
#   FF DERT
awk '
/BB/,/FF/{
  if ($0 !~ /FF/) print $0
}' ./test.log
# Output:
#   BB DERT
#   CC DERA
#   DD TYU
#   EE ASDF
#Use example:
$ sed -n '/2021-07-14 14/,$p' ./logs/spring.log | colorize
$ sed -n '/2021-07-14 14/,$p' ./logs/spring.log | head -n 200 | colorize
$ zcat ./logs/spring.log.2021-07-10.0.gz | sed -n '/2021-07-10 14:45/,$p' | head -n 200 | colorize
$ java -jar ./demo.jar --spring.profiles.active=local | colorize

enclosure:
Gunwin32 attachment

If you want more colors, the following article will come, and then write a 256 color script when you have time.

Refs: