もた日記

くだらないことを真面目にやる

Linuxメモ : ログに色を付けるcczeコマンドの使い方

ccze


wonderwall.hatenablog.com

上記記事でcczeというログに色を付けるコマンドを知ったので使い方を調べてみる。

f:id:wonder-wall:20170730211732p:plain

ccze(1): robust log colorizer - Linux man page(cczeマニュアルページ)

インストール


CentOSの場合(今回はCentOSで試してみる)。

$ sudo yum install ccze

Ubuntuの場合。

$ sudo apt-get install ccze

Macの場合。

$ brew install ccze

ヘルプメッセージ。

$ ccze --help
Usage: ccze [OPTION...]
ccze -- cheer up 'yer logs.

  -a, --argument=PLUGIN=ARGS...   Add ARGUMENTS to PLUGIN
  -A, --raw-ansi             Generate raw ANSI output
  -c, --color=KEY=COLOR,...  Set the color of KEY to COLOR
  -C, --convert-date         Convert UNIX timestamps to readable format
  -F, --rcfile=FILE          Read configuration from FILE
  -h, --html                 Generate HTML output
  -l, --list-plugins         List available plugins
  -m, --mode=MODE            Change the output mode
                             (Available modes are curses, ansi and html.)
  -o, --options=OPTIONS...   Toggle some options
                             (such as scroll, wordcolor and lookups,
                             transparent, or cssfile)
  -p, --plugin=PLUGIN        Load PLUGIN
  -r, --remove-facility      remove syslog-ng's facility from start of the
                             lines
  -?, --help                 Give this help list
      --usage                Give a short usage message
  -V, --version              Print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Report bugs to <algernon@bonehunter.rulez.org>.


使い方


基本的には以下のようにログファイルをcczeに渡してやればよい。

# ccze -A < /var/log/httpd/error_log
# tail -f /var/log/httpd/error_log | ccze -A

FAQによると2つのファイルを入力にしたり、

# (tail -f apache/access.log & tail -f xferlog) | ccze -A

-hオプションでHTML出力にして結果をブラウザで確認することもできる。

# tail -f /var/log/syslog | ccze -A -h > ~/logfiles/syslog.html

f:id:wonder-wall:20170802215008p:plain

毎回コマンドを打つのは大変なので以下のような関数を定義するのがよいかもしれない。

tailc () {
    tail $@ | ccze -A
}


色設定


色設定は/etc/cczercファイル(環境によってパスは違うかも)を見るとなんとなくわかる。bold, underlineといった属性と文字色、背景色を設定できるようだ。

$ head -n 20 /etc/cczerc
# Configuration file for ccze
#
# Available 'pre' attributes: bold, underline, underscore, blink, reverse
# Available colors:  black, red, green, yellow, blue, magenta, cyan, white
# Available bgcolors: on_black, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white
#
# You can also use item names in color definition, like:
#
# default   blue
# date      'default'
#
# Here you defined default color to blue, and date color to default value's color, so
# your date color is blue. (You can only use predefined item names!)

# item          color                   # comment (what is color, or why it's that ;)

date            bold cyan               # Dates and times
host            bold blue               # Host names and IP numbers
process         green                   # Sender process
pid             bold white              # PIDs (Process IDs)

例えば日時の色を変更したいときに、オプションを追加して変更する場合は以下のようにする。

cat log | ccze -A --color==date="underline red"

設定ファイルで変更する場合は、下記行を記述した$HOME/.cczercを作成すると色設定が反映される。

date            underline red               # Dates and times

f:id:wonder-wall:20170802205216p:plain