Monitoring apache logs with tail –f
tends gets very frustrating for the eyes after a while. Are there any tool/options to colorize the log outputs? Maybe signal FATAL with red, etc...
- 791
- 1
- 6
- 13
-
1Related:http://serverfault.com/questions/53894/colorize-monitoring-of-logs – Cherian Sep 03 '10 at 05:22
9 Answers
Any reason why you can't use something like this:
tail -f FILE | grep --color=always KEYWORD
source: commandlinefu.com
- 1,129
- 8
- 12
-
-
This filters the output as well, so any line without `KEYWORD` will be ignored. – Michal Mau Apr 03 '15 at 17:41
-
The OP seems to be implying he's looking for a keyword or words. Unless the file is multiline in nature (which apache logs generally are not), then this answer is sufficient. – Garrett May 27 '15 at 05:50
I use a small script with grep combinations to get some colors:
#!/bin/bash
shopt -s expand_aliases
alias grey-grep="GREP_COLOR='1;30' grep -E --color=always --line-buffered"
alias red-grep="GREP_COLOR='1;31' grep -E --color=always --line-buffered"
alias green-grep="GREP_COLOR='1;32' grep -E --color=always --line-buffered"
alias yellow-grep="GREP_COLOR='1;33' grep -E --color=always --line-buffered"
alias cyan-grep="GREP_COLOR='1;36' grep -E --color=always --line-buffered"
tail -1000f /var/log/apache2/error.log | grey-grep ".*PerformanceLogger.*|$" | cyan-grep "INFO|$" | yellow-grep "WARN|$" | red-grep "[ERROR].*|[FATAL].*|$" | green-grep "***|$"
The point is that every chained grep add a different color. So the result is something like:
- 169
- 1
- 4
Found this: http://fixunix.com/unix/83044-tail-color.html
tail -f file | perl -pe 's/keyword/\e[1;31;43m$&\e[0m/g'
This only works on ANSI terminals, but all others have become virtually extinct. \e[...m ist the ANSI escape sequence SGR "select graphic rendition". The "..." can be replaced by some semicolon-separated integers, with the meaning:
0 : all attributes off 1 : bold 31 : foreground red 43 : background yellow
"keyword", of course, can be any perl regular expression:
(foo|bar) highlight the strings foo and bar \b((foo|bar)\b highlight the words foo and bar .\b((foo|bar)\b. highlight the whole line that contains the words foo or bar
Or, the easy way, just install colortail
Its probably in your favorite repo (dag for CentOS)
http://developwithstyle.com/articles/2010/04/20/tail-your-logs-with-a-touch-of-color.html
- 2,053
- 15
- 20
-
Or this SF: http://serverfault.com/questions/53894/colorize-monitoring-of-logs – Grizly Sep 03 '10 at 05:18
-
From all responses above this is what I got and it works very nicely
#!zsh
GR="grep --color=always --line-buffered -E"
alias grey="GREP_COLOR='1;30' $GR"
alias red="GREP_COLOR='1;31' $GR"
alias green="GREP_COLOR='1;32' $GR"
alias yellow="GREP_COLOR='1;33' $GR"
alias cyan="GREP_COLOR='1;36' $GR"
# show static files gray, 200 status green, 300 grey, etc
# [503] 06/24/20 19:40:34 (239) proxy:https://feedpress.me/drudgereportfeed?format=xml | cache miss: attempting entity save | 2460b
# [200] 06/24/20 19:40:34 (394) proxy:https://www.reddit.com/r/news/.rss | cache miss: attempting entity save | 25883b#
tail -300f /var/log/apache2/access.log | grey "$|[a-z0-9/]+(css|js|ico|png).*" | green "$|\[2[0-9]*\]" | yellow "$|\[3[0-9]*\]" | cyan "$|\[4[0-9]*\]" | red "$|\[5[0-9]*\].*"
I spent many years as a sysadmin for an MX-level email antivirus & antispam service & while trying to help myself out of a semi-dyslexic maillog tailing session, one hyper-spamming event, I knocked this little script up, that allowed me to selectively highlight various interesting log entries.
https://github.com/furriephillips/hl
Please be gentle - this was the creation of a madman, being spammed into kingdom come.
This should work fine, on any log you are tailing, as you can tailor the bits that get highlighted & in what colour & gradually work your way up to a fully personalised experience, perhaps with per-log command aliases, like this random & not very useful example: -
alias hlmaillog="tail -F /var/log/maillog | hl NOQUEUE lightblue | hl 'blocked using zen' pink | hl warning yellow | hl 'Name or service not known' pink | hl 'TLS connection established' yellow | hl TIMING lightblue | hl dkim red"
- 75
- 7
Shameless plug: I wrote a tool called TxtStyle that does something similar as the options mentioned earlier. You can run it as follows:
tail -f /var/log/syslog | txts --regex '\d+'
You can also define named styles in the config file (~/.txts.conf
) and use it like so:
ifconfig | txts --name ifconfig
(ifconfig
style is defined out of the box)
- 141
- 4
There's one feature that I haven't seen in those colorizers -- highlight response times (higher time -> more alarming color). 256-color support in modern terminal emulators could be useful here.
- 1,742
- 12
- 33
Another useful grep trick to show all output but colour the selected KEYWORD is :
tail -f FILE | grep --color=always -E "$|REGEXP"
- 183
- 2
- 12