Re: Read from Apache error.log

[prev] [thread] [next] [Date index for 2005/03/27]

From: Stas Bekman
Subject: Re: Read from Apache error.log
Date: 15:08 on 27 Mar 2005
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@xxxxxx.xxx http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

Read from Apache error.log
Femi Oshagbemi 11:47 on 27 Mar 2005

Re: Read from Apache error.log
Rodger Castle 11:59 on 27 Mar 2005

Re: Read from Apache error.log
Femi Oshagbemi 12:30 on 27 Mar 2005

Re: Read from Apache error.log
Rodger Castle 12:39 on 27 Mar 2005

Re: Read from Apache error.log
Clinton Gormley 14:08 on 27 Mar 2005

Re: Read from Apache error.log
Stas Bekman 15:08 on 27 Mar 2005

Generated at 09:30 on 27 Apr 2005 by mariachi v0.52