Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Simple Apache Error Log Viewer

by coolmichael (Deacon)
on Jun 12, 2002 at 01:00 UTC ( #173705=CUFP: print w/ replies, xml ) Need Help??

I wanted to learn a little more about CGIs and Cookies. This is the result. Any comments are welcome.

Error lines are wrapped with Text::Wrap. Each time the script is loaded, it tries to set a cookie containing the last line read in the error log. Each load of the script accesses the cookie and only displays unread lines.

# usage: # http://host/path/velog.cgi will show the lines in the error log # you haven't seen yet. # http://host/path/velog.cgi?all=234 will show all the lines in the
Update: I've taken some suggestions and made some changes. I'm much happier with the script now. Thanks all. I've posted the updated version as a reply for anyone who might be interested.
#!perl -wT #config variables my $logfile = q(D:\httpd\Apache\logs\error.log); my $initial = ""; #first line of each error my $subseq = ""; #additional lines for each error my $domain = ""; my $path = ""; my $expires = ""; my $cookiename = "veloglastline"; use strict; use CGI; use Text::Wrap; #returns a CGI::cookie that looks something like veloglastline=#### #where #### is the last line of the error log viewed. sub makecookie { my $q = shift; my $line = shift; my $cookie = $q->cookie( { -name => $cookiename, -value => $line, -domain => $domain, -expires => $expires, -path => $path } ); return $cookie; } #checkes for a CGI::cookie that looks something like veloglastline=### +# #where #### is the last line of the error log viewed. #defaults to 0 (ie, show the entire error log) sub getlastlineviewed { my $q = shift; my $cookie = $q->cookie($cookiename) || 0; my $line; if($cookie =~ m/^(\d*)$/) { $line = $1; }else{ $line = 0; } return $line; } #gets the last $lastline lines from the errorlog, and returns a REFERE +NCE #to an array of the last lines. sub getlogtext { my $q = shift; my $lastline = shift; my @lines; open (ELOG, "<$logfile") or die "unexpected error opening error log for reading: +$!\n"; #intentionally toss away these lines. <ELOG> while((!eof(ELOG)) && ($. < $lastline)); if(eof) { @lines = ("No unread lines found"); }else { @lines = <ELOG>; #slurp in the rest of the li +nes. } close ELOG; return \@lines; #return a REFERENCE to the a +rray! } #takes a reference to a CGI object, the total number of lines in the e +rrorlog #and a reference to an array of text. Sets a cookie with the number of + lines #read along, and prints the text inside <pre></pre> tags. #this may change to plain text in the near future. sub printerrorlog { my $q = shift; my $lastline = shift; my $text = shift; my $cookie = makecookie($q, $lastline); print $q->header(-type => "text/plain", -cookie => $cookie), wrap($initial, $subseq, @$text); } my $q = CGI->new(); my $startline; my $lastline; my $logtext; $startline = getlastlineviewed($q); $startline=0 if(defined($q->param("all"))); $logtext = getlogtext($q, $startline); $lastline = $startline + $#$logtext; printerrorlog($q, $lastline, $logtext);

Comment on Simple Apache Error Log Viewer
Select or Download Code
Re: Simple Apache Error Log Viewer
by coolmichael (Deacon) on Jun 12, 2002 at 05:37 UTC
    So, with a few comments from some of the monks here, I've made some simplifications. Thanks especially to crazyinsomniac for suggesting Tie::File. I had read about it before, but using it didn't even occur to me.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://173705]
Approved by beretboy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2014-07-10 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (214 votes), past polls