Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am in the midst of writing a small Perl utility that searches through a logfile and extracts Java exceptions and logs them into an error file. Everything is working fine except that the exceptions are being logged together in the file. I want each exception to be logged in a paragraph format. Is this possible? Keep in mind that exceptions span several lines in the log. Here is the snippet of code that I am using:
open (ERRFILE, ">error.log"); open (LOGFILE "/tmp/app.log"); while (<LOGFILE>) { $line=$_; chomp($line); if ($line =~ /xception/) { print ERRFILE "$line\n"; }

Replies are listed 'Best First'.
Re: pattern searching and grouping by paragraph
by CountZero (Bishop) on Jan 10, 2005 at 21:42 UTC
    Why do you chomp($line) only to add the linefeed again when printing?

    It looks that you could simplify your code to

    open (ERRFILE, ">error.log"); open (LOGFILE "/tmp/app.log"); while (<LOGFILE>) { print ERRFILE $line if /xception/; }
    All you have to do then is print some linefeeds when you start a new exception.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: pattern searching and grouping by paragraph
by ikegami (Patriarch) on Jan 10, 2005 at 21:19 UTC
    Could you please give us a sample of /tmp/app.log? You say exceptions span multiple lines, yet you're matching individual lines.
      Here is the sample of the log:
      2005.01.10 10:20:34.557 com.company.web.xif.eop.EOPInboundHandler$1 Ha +ndleRequest apsrv050(10.250.58.33) ERROR: Inbound Event 180 failed, 180 confirme +xception ation refers to order ref num 00020012 which doesn't exist w +her e cost_cntr_id = 64: null ERROR CODE 1103: (Fexception atAL) EOP Inbound 180 - Specified bank +Ref # not found Inbound Event 180 failed, 180 confirmexception ation refers to order r +ef num 00020012 which doesn't exist where cost_cntr_id = 64 exception at com.company.web.xif.event.EOPEvent180.validexcept +ion ateOrderReference(EOPEvent180.java:93) exception at com.company.web.xif.event.EOPEvent180.BeginInboun +dEvent(EOPEvent180.java:361) exception at com.company.web.xif.event.EOPEvent.runInbound(EOP +Event.java:165) exception at com.company.web.xif.event.EOPEventHandler.StartIn +bound(EOPEventHandler.java:154) exception at com.company.web.xif.eop.EOPInboundHandler$1.Worke +r(EOPInboundHandler.java:704) exception at com.company.web.util.TimedExecution.run(TimedExec +ution.java:172) 2005.01.10 10:20:34.558 com.company.web.xif.eop.EOPInboundHandler$1 Ha +ndleRequest apsrv050(10.250.58.33) ERROR: Failed order processing, aborting tran +saction... 2005.01.10 10:20:37.866 com.company.web.xif.event.EOPEvent180 BeginInb +oundEvent apsrv050(10.250.58.33) ERROR: Inbound Event 180 failed, 180 confirme +xception ation refers to order ref num 00020012 which doesn't exist w +her e cost_cntr_id = 64: null ERROR CODE 1103: (Fexception atAL) EOP Inbound 180 - Specified bank +Ref # not found Inbound Event 180 failed, 180 confirmexception ation refers to order r +ef num 00020012 which doesn't exist where cost_cntr_id = 64 exception at com.company.web.xif.event.EOPEvent180.validexcept +ion ateOrderReference(EOPEvent180.java:93) exception at com.company.web.xif.event.EOPEvent180.BeginInboun +dEvent(EOPEvent180.java:361) exception at com.company.web.xif.event.EOPEvent.runInbound(EOP +Event.java:165) exception at com.company.web.xif.event.EOPEventHandler.StartIn +bound(EOPEventHandler.java:154)