in reply to Comma seperated output

If those nested loops are there to read only the next line, use if ( $line = <$log>) { ... } else { last; } instead of your while

if the line you are looking for might not be the next line but one of the next lines, then you have to use a while, but inside that while loop you need something to break that loop, like

if ( found_what_I_was_looking_for ) { print the_information last; }

Note that logfiles might have those additional lines intermixed with lines from other log events. If you want to catch that too, your program needs to remember unfinished events or cache new events until the unfinished event was processed