Claudia Benete Claudia Benete - 11 months ago 42
HTML Question

Perl: color error log with specific character

how to color the output with specific string/character, like if the log contains word "ERROR", it will be RED, "Warning" will be yellow and "Info" will be green in PERL

Answer Source

If you are going to output this to a web browser you could use: (Keep in mind that if the log file is to big then ths connection will time-out in the web browser)

$logfile = "/var/log/log.txt" ;
open INFILE, "$logfile" ; 
@loglines = <INFILE> ; 
close INFILE;

foreach $line (@loglines) {
   if ($line =~ /ERROR/) {
      $line = "<span style=\"color: red;\">" . $line . "</span>";
   elsif ($line =~ /Warning/) {
     $line = "<span style=\"color: yellow;\">" . $line . "</span>";
   elsif ($line =~ /Info/) {
     $line = "<span style=\"color: green;\">" . $line . "</span>";
    print $line;

It would be better to use CSS classes instead of the span tags as outlined above.