Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Currently I am using tail -f for viewing logs.

Is there any more efficient/readable/better way of viewing logs?

share|improve this question

closed as not constructive by Ƭᴇcʜιᴇ007, Diago, Dave, Diogo, 8088 Jan 11 '13 at 17:54

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

up vote 1 down vote accepted

Yes! perl stand for Practical Extraction and Research Language.

Instead of using shell syntaxes, like:

tail -f file | grep --line-buffered someting

or

tail -f file | sed -une '/someting/{s/^.*:/doing some formatting/;p}'

(Nota: --line-buffered in grep, like -u in sed are usefull to prevent buffering by fixed size blocks)

For viewing/browsing logs, perl is the language.

Depending on what you're awaiting for... you may have to build a little script for matching exactly your need.

You may found some useable samples in perlfaq, like in perldoc perlfaq5 (or man perlfaq5 if you've not installed perldoc), there is a question: How do I do a "tail -f" in perl? where I've taked this:

First try

seek(GWFILE, 0, 1);

The statement seek(GWFILE, 0, 1) doesn't change the current position, but it does clear the end-of-file condition on the handle, so that the next <GWFILE> makes Perl try again to read something.

If that doesn't work (it relies on features of your stdio implementation), then you need something more like this:

for (;;) {
    for ($curpos = tell(GWFILE); <GWFILE>; $curpos = tell(GWFILE)) {
      # search for some stuff and put it into files
    }
    # sleep for a while
    seek(GWFILE, $curpos, 0);  # seek to where we had been
  }

I've already used this many times for triggering special cases.

DEMO

For sample, there is a quick and dirty real-time log counter try it! (need read access to /var/log/syslog ;-) :

#!/usr/bin/perl -w
my %counter;

open FH, "</var/log/syslog" or die;
my $curpos = ( stat FH )[7];
seek( FH, $curpos, 0 );

for ( ; ; ) {
    for ( $curpos = tell(FH) ; <FH> ; $curpos = tell(FH) ) {
        $counter{$3}++ if /^(\S+\s+){4}(\S+\/|)([^\/\[:]*)[\[:]/;
    }
    syswrite STDOUT, sprintf "\r%s\e[K",
      join( ", ", map { sprintf "%s:%s", $_, $counter{$_} } keys %counter );
    select undef,undef,undef,1;
    seek( FH, $curpos, 0 );
}

Would produce one line, self-refreashing every seconds, containing all seen daemon's name and a counter for each of them:

smtp:6, pop3d-ssl:13, local:6, imapd:8, smtpd:30, CRON:5, pickup:1, named:1
share|improve this answer

As F. Hauri suggests, you could use perl to format the output.

For my usage, I created an alias for tail in my ~/.bashrc to read my Glassfish's logs. It is very simple (one line) and colorizes my output to underline error/warning lines:

tail-color() {
    tail $* | perl -p -e 's/(.*WARN.*)/\033[31;43;1m$1\033[0m\007/g;' -e 's/(.*ERROR.*)/\033[33;41;1m$1\033[0m\007/g;' -e 's/(.*SEVERE.*)/\033[33;41;1m$1\033[0m\007/g'
}
  1. -p : wraps the script inside a loop and outputs the processed line
  2. -e 's/(.*WARN.*)/\033[31;43;1m$1\033[0m\007/g;' colorizes every line containing "WARN"
    • \033[ begins escape sequence (enables formatting)
    • 31 means red foreground
    • 43 means yellow background
    • 1 enables bold characters
    • m closes escape sequence
    • $1 includes the input line
    • \033[0m is a second sequence to put default formatting back to standard, after the line has been outputs
    • \007 beeps
  3. -e 's/(.*ERROR.*)/\033[33;41;1m$1\033[0m\007/g;' colorizes every line containing "ERROR" ; the only differences concern colors:
    • 31 means yellow foreground
    • 43 means red background

In my case, I toggled visual bell on, so my screen blinks on error (and my speaker doesn't beep) and error/warning lines can be detected very quickly.

You can get more information about:

Again, as F. Hauri explains in his answer, you can do many things thanks to perl, so you could "develop" your own log reader depending on your needs.

share|improve this answer
    
For more portability, try to use tput instead of ansi codes: my $red=`tput setf 4` or $bold=`tput bold` or via Term::Cap(3perl). (see man terminfo) –  F. Hauri Jan 12 '13 at 20:07
    
Thanks for the tip! –  ssssteffff Jan 13 '13 at 20:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.