[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    Re: Read from Apache error.log
From:       Stas Bekman <stas () stason ! org>
Date:       2005-03-27 15:08:36
Message-ID: 4246CC74.4080109 () stason ! org
[Download RAW message or body]

Femi Oshagbemi wrote:
> Hello, im very new to the perl, modperl and apache combination.
> 
> I was wondering if it is somehow possible to read the apache error.log 
> input just after each execution of a procedure?
> 
> I am invoking another program from within my perl module using a 
> 'System(...)' call. Although the call is successfull, there are times 
> when the invoked program may terminate with a 'parse error:' for 
> example, and theses are logged in the error.log. Is there a way the 
> catch or read this as it happens and display to the browser?

Instead of ``, qx or system, you can use Apache::SubProcess, IPC::Run or 
IPC::Run3 which all give you an access to STDERR of the sub-process. (Note 
that IPC::Open* don't work under mod_perl, at least not with 1.0, didn't 
test with 2.0).

If for some reason you still want to read from error_log, here is how:

we have a similar need in the mod_perl2 test suite, so here is how we
do that:

use TestCommon::LogDiff;

my $path = '/path/to/error_log'; # adjust this
my $logdiff = TestCommon::LogDiff->new($path);
# do something
my $diff = $logdiff->diff;

and here is the module that does the work:

package TestCommon::LogDiff;

use POSIX ();

sub new {
     my $class = shift;
     my $path  = shift;

     open my $fh, "<$path" or die "Can't open $path: $!";
     seek $fh, 0, POSIX::SEEK_END();
     $pos = tell $fh;

     my %self = (
         path => $path,
         fh   => $fh,
         pos  => $pos,
     );

     return bless \%self, $class;
}

sub DESTROY {
     my $self = shift;
     close $self->{fh};
}

sub diff {
     my $self = shift;

     # XXX: is it possible that some system will be slow to flush the
     # buffers and we may need to wait a bit and retry if we see no new
     # logged data?
     my $fh = $self->{fh};
     seek $fh, $self->{pos}, POSIX::SEEK_SET(); # not really needed

     local $/; # slurp mode
     my $diff = <$fh>;
     seek $fh, 0, POSIX::SEEK_END();
     $self->{pos} = tell $fh;

     return defined $diff ? $diff : '';
}

1;

__END__

=head1 NAME

TestCommon::LogDiff - get log file diffs

=head1 Synopsis

   use TestCommon::LogDiff;
   use Apache::Test;

   plan tests => 2;

   my $path = "/tmp/mylog";
   open my $fh, ">>$path" or die "Can't open $path: $!";

   my $logdiff = TestCommon::LogDiff->new($path);

   print $fh "foo 123\n";
   my $expected = qr/^foo/;
   ok t_cmp $logdiff->diff, $expected;

   print $fh "bar\n";
   my $expected = 'bar';
   ok t_cmp $logdiff->diff, $expected;


=head1 Description

Useful for testing the warning, error and other messages going into
the log file.

=head1 API

=head2 new

open the log file and point the filehandle pointer to its end.

=head2 diff

extract any newly logged information since the last check and move the
filehandle to the end of the file.

=cut


-- 
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic