Skip to content

Instantly share code, notes, and snippets.

@jdrews
Forked from jdanbrown/log-color
Last active October 24, 2015 21:23
Show Gist options
  • Select an option

  • Save jdrews/af970bb97e8d34978621 to your computer and use it in GitHub Desktop.

Select an option

Save jdrews/af970bb97e8d34978621 to your computer and use it in GitHub Desktop.

Revisions

  1. jdrews revised this gist Oct 24, 2015. 1 changed file with 15 additions and 10 deletions.
    25 changes: 15 additions & 10 deletions log-color
    Original file line number Diff line number Diff line change
    @@ -6,32 +6,37 @@
    # tail foo.log | log-color
    # run-service | log-color

    export TERM=xterm-256color

    black="` tput setaf 0; tput bold`"
    red="` tput setaf 1; tput bold`"
    green="` tput setaf 2; tput bold`"
    yellow="` tput setaf 3; tput bold`"
    bluebold="` tput setaf 4; tput bold`"
    blue="` tput setaf 4; tput bold`"
    lightblue="` tput setaf 27; tput bold`"
    magenta="` tput setaf 5; tput bold`"
    cyan="` tput setaf 6; tput bold`"
    white="` tput setaf 7; tput bold`"
    lightgrey="$(tput setaf 240)"
    #off="` tput sgr0`"
    off="` echo -ne "\x0f"`" # (Works better on both osx and linux...)

    exec sed -r "
    # Timestamps
    s/^\[[0-9]{4}[^]]*\]/$black\\0$off/
    s/^[0-9]{4}-[0-9:,/ -]*/$black\\0$off/
    s/^\[[0-9]{4}[^]]*\]/$lightgrey\\0$off/
    s/^[0-9]{4}-[0-9:,/ -]*/$lightgrey\\0$off/
    # Log levels
    s/\\b(FATAL|CRITICAL)\\b/$magenta\\0$off/g
    s/\\bERROR\\b/$red\\0$off/g
    s/\\bWARN(ING)?\\b/$yellow\\0$off/g
    s/\\bINFO\\b/$green\\0$off/g
    s/\\bDEBUG\\b/$blue\\0$off/g
    s/\\bTRACE\\b/$cyan\\0$off/g
    s/\\b(FATAL|CRITICAL).*\\b/$magenta\\0$off/g
    s/\\bERROR.*\\b/$red\\0$off/g
    s/\\bWARN(ING)?.*\\b/$yellow\\0$off/g
    s/\\bINFO.*\\b/$green\\0$off/g
    s/\\bDEBUG.*\\b/$lightblue\\0$off/g
    s/\\bTRACE.*\\b/$cyan\\0$off/g
    # Grouped things
    s/ \[[^]]+\] \S+ - /$black\\0$off/
    s/ \([a-zA-Z_][a-zA-Z0-9_$.:]*\)$/$black\\0$off/
    s/ \[[^]]+\] \S+ - /$lightgrey\\0$off/
    s/ \([a-zA-Z_][a-zA-Z0-9_$.:]*\)$/$lightgrey\\0$off/
    "
  2. @jdanbrown jdanbrown created this gist Nov 21, 2011.
    37 changes: 37 additions & 0 deletions log-color
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/bin/bash -eu
    #
    # Color log4j-style logs for easier visual parsing.
    #
    # Usage:
    # tail foo.log | log-color
    # run-service | log-color

    black="` tput setaf 0; tput bold`"
    red="` tput setaf 1; tput bold`"
    green="` tput setaf 2; tput bold`"
    yellow="` tput setaf 3; tput bold`"
    blue="` tput setaf 4; tput bold`"
    magenta="` tput setaf 5; tput bold`"
    cyan="` tput setaf 6; tput bold`"
    white="` tput setaf 7; tput bold`"
    #off="` tput sgr0`"
    off="` echo -ne "\x0f"`" # (Works better on both osx and linux...)

    exec sed -r "
    # Timestamps
    s/^\[[0-9]{4}[^]]*\]/$black\\0$off/
    s/^[0-9]{4}-[0-9:,/ -]*/$black\\0$off/
    # Log levels
    s/\\b(FATAL|CRITICAL)\\b/$magenta\\0$off/g
    s/\\bERROR\\b/$red\\0$off/g
    s/\\bWARN(ING)?\\b/$yellow\\0$off/g
    s/\\bINFO\\b/$green\\0$off/g
    s/\\bDEBUG\\b/$blue\\0$off/g
    s/\\bTRACE\\b/$cyan\\0$off/g
    # Grouped things
    s/ \[[^]]+\] \S+ - /$black\\0$off/
    s/ \([a-zA-Z_][a-zA-Z0-9_$.:]*\)$/$black\\0$off/
    "