LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Coloring real-time logs on tty (https://www.linuxquestions.org/questions/linux-newbie-8/coloring-real-time-logs-on-tty-191839/)

d1s4st3r 06-10-2004 04:23 AM

Coloring real-time logs on tty
 
Hi everyone,
I just added this line to my "/etc/syslog.conf":
Code:

*.*        /dev/tty12
to have real-time logs printed on tty12.
The only terminals I can log in are tty[1-6] + tty7 for X, but that's ok.
Well, now I'd like to have many different colors (maybe using escape sequences) for the various types of log messages (eg: all kernel messages in green, all ACPI messages in yellow, all login failures in red, and so on) directly on the tty12, but I don't know how can I do this.
I thought I could write a Bash script which reads each line added on tty12, uses grep to discover the type of message, then colors it (using echo and redirection), but I couldn't find a way to read the data printed on tty12.
Code:

cat /dev/tty
Code:

tee /dev/tty12
these commands don't work.
I think it's very useful to have different colors instead of all-grey log messages, but what's the way to do this?
Thanks to all you people!
Bye!

Donboy 06-10-2004 07:21 AM

Here's a little gem I picked up from Linux Server Hacks. Excellent book, BTW... I highly recommend it.

This is a Perl script that will color text in the shell. Save this as "rcg" and put it somewhere in your path.

Code:

#!/usr/bin/perl

use strict;
use Term::ANSIColor qw(:constants);

my %target = ( );

while (my $arg = shift) {
  my $clr = shift;

  if(($arg =~ /^-/) | (!$clr)) {
    print "Usage: rcg [regex] [color] [regex] [color] ...\n";
    exit;
  }
  $target{$arg} = eval($clr);
}

my $rst = RESET;

while(<>) {
  foreach my $x (keys(%target)) {
    s/($x)/$target{$x}$1$rst/g;
  }
  print;
}

Usage is like this...

Code:

tail -50 /var/log/messages | rcg kernel GREEN error RED

d1s4st3r 06-10-2004 10:18 AM

Fantastic!!!
Thanx very much!
I tried it out and it works fine, but what can I do now to have the new colored logs always directed on tty12 (expecially each time they're updated, always with colors)?
I mean... should I write I kind of deamon that listens for every new log from the kernel, then colors it, then sends it to tty12 appending to the other ones before?
The only problem is that I *really* don't know how to capture new logs appended each time on tty12, because I don't know how to have that "writings" printed on the console I'm currently working on (one of tty[1-6]).
If I could read those message on my console, I could simply use "cat" to read it, send it to "rcg.perl", then send it back again to /dev/tty12, but colored this time!
BTW, I thank you very much and I'll try until I find a way!
Bye!

Donboy 06-10-2004 11:27 AM

Hmmm... hard to say. Maybe somebody here knows more about working with TTY's than me. Personally, I would just login to the server from that console and run your commands piped through the rcg program. I do this myself all the time. I login with SSH and run the command just like I'm showing above. Actually, for some commands that I use more often, I just put them into my .bashrc file as an alias so the rcg program gets called everytime.

d1s4st3r 09-03-2004 05:40 PM

Just tried out this cool program: "ccze".
I found it in Debian Sid repositories thru APT (section "utils") and it colors out any kind of log files (exim, apache, syslog, ecc...), and it has much more interesting features.
Cool!


All times are GMT -5. The time now is 12:42 AM.