Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging from output in Perl

I have a program that is supposed to run a set of other programs and collect their output for logging purposes. Everything is working fine as long as there is output to standard out.

That leads me to my two problems:

  1. How can I capture both STDIN and STDERR of another program in one file?

  2. If there is no output at all (or output to STDERR only) the program will get stuck on the line:

    while (<$input>)
    

How can I make the program wait for possible output from another program of indeterminate run time, and still continue if there is no output by the time the program finishes execution.

Here is that section of the code

my $pid = open (my $input, '-|', "$prog $args")
        or push @errors, "A failute has occurred in $prog $args";
if(not @errors){
    while (<$input>){ #POSSIBLE LOCATION FOR HANG UP IF NO PRINTING IS DONE
        if($input =~ /^END\n$/){
            last;
        }
        print $fh $_;
    }
}
else{
    print $fh "An error has occurred with executing \"$prog $args\"";
}

note: $fh is my file handler used to write to my log file, and @errors is used for internally reporting errors in my program.

EDIT: One problem I experienced with trying the Proc::Reliable route is that it seems to have after effects on STDOUT and STDERR, and I would need to fix those after each call to run. This leads to another problem in my code. I am running in parallel and any change to STDOUT and STDERR effect all of the threads (using Windows at least).

IO::CaptureOutput has the same issue for me, but it tries to correct the STDOUT and STDERR jumbling internally and causes the following error to occur:

Couldn't remove temp file 'C:\Users\SBLAKE~1\AppData\Local\Temp\m8E3pUGfOS' - Permission denied at C:/Perl/site/lib/IO/CaptureOutput.pm line 82
like image 537
Shawn Blakesley Avatar asked Aug 08 '12 17:08

Shawn Blakesley


People also ask

How do I write a log in Perl?

Use Sys::Syslog to write log messages. But since you're opening a log. txt file with the handle OUTPUT , just change your two print statements to have OUTPUT as the first argument and the string as the next (without a comma). Not only because it's in your while loop.

What is the main purpose of using loggers in Perl?

In the most simple case we use it to add logging messages to a script. We need to load the module importing the :easy key that will import 6 functions for the 6 logging levels provided by Log::Log4perl and 6 variables with corresponding names. In the code we can use any of the 6 methods to send logging messages.

How do I print a Perl script?

print() operator – print operator in Perl is used to print the values of the expressions in a List passed to it as an argument. Print operator prints whatever is passed to it as an argument whether it be a string, a number, a variable or anything. Double-quotes(“”) is used as a delimiter to this operator.


2 Answers

If you launch from a POSIX-like shell, then the 2>&1 notation sends standard error to the same place as the standard output:

some-program-to-be-logged 2>&1 | perl logger.pl -o /chosen/log.file

Both standard output and standard error are sent to the standard input of the Perl logging script.

The input loop in the logger script will hang waiting until there is input to read, or until all the processes with the pipe open for writing close the pipe (typically because they exit).

like image 157
Jonathan Leffler Avatar answered Sep 27 '22 20:09

Jonathan Leffler


Look at Proc::Reliable, it seems to be what you want.

See also:

  • How do you capture stderr, stdout, and the exit code all at once, in Perl?
  • Capture::Tiny
  • IO::CaptureOutput
like image 40
h4ck3rm1k3 Avatar answered Sep 27 '22 20:09

h4ck3rm1k3