Search code examples
perllog4perl

Parsing help in Perl


My (existing) perl files creates a Log file using Log4Perl in the following format

[2011-11-21 08:50:22,406] States_Sync INFO Logger.pm:33 script starts
[2011-11-21 08:50:22,610] States_Sync ERROR Logger.pm:36 Error occurred ....
[2011-11-21 08:50:22,406] States_Sync INFO Logger.pm:33 ...
[2011-11-21 08:50:22,610] States_Sync ERROR Logger.pm:36 Error occurred ....
[2011-11-21 08:50:22,406] States_Sync INFO Logger.pm:33 ...
[2011-11-21 08:50:22,610] States_Sync ERROR Logger.pm:36 Error occurred ....

The above is only an example of my log file. I use the following formatter

$layout = Log::Log4perl::Layout::PatternLayout->new("[%d{ISO8601}] %c %p %F{1}:%L %m%n");

Currently I have to send an email in case of error.

Instead of modifying the existing script, I thought of parsing the generated log files only for Error and send all the messages related to "Error" from the log file as email

Is there any easy way of parsing the log file ?

Regards,

Karthik


Solution

  • use grep(1):

    grep ERROR log.file
    

    or use perl:

    perl -ne 'print if /ERROR/' log.file