Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

apache errorlog

by epoptai (Curate)
on Feb 02, 2001 at 20:24 UTC ( #56015=perlcraft: print w/ replies, xml ) Need Help??

   1: #!perl
   2: # 1. errorlog.pl parses and displays apache errorlogs
   3: # 2. just change $error_log and $apache to your paths
   4: # 3. cookies remember the list size and filter status
   5: 
   6: use strict;
   7: use CGI qw(:standard);
   8: use CGI::Cookie;
   9: use HTML::Entities;
  10: 
  11: my$error_log = 'c:\path\to\error.log'; # CHANGE
  12: my$apache    = 'c:\path\to\Apache\Apache.exe: ';   # CHANGE but, leave space at end.
  13: my$show  = 10; # default number of errors to show
  14: my@EL = stat $error_log;
  15: 
  16: my%cookies = fetch CGI::Cookie;
  17: my%i = map{$_ => param($_)} param;
  18: my$url = url();
  19: my$time = localtime();
  20: $time =~s| .{4}$||o;
  21: 
  22: open (ELOG,"<".$error_log) or die "$!";
  23: my@elog=<ELOG>;
  24: close (ELOG) or die "$!";
  25: 
  26: my($cc,$c1,$c2);
  27: 
  28: if($cookies{'show'}){
  29: 	unless($i{'n'}=~/\d+/){
  30: 		$show = $cookies{'show'}->value;
  31: 		}
  32: 	}
  33: if($i{'n'}=~/\d+/){
  34: 	$show=$i{'n'};
  35: 	$c1 = new CGI::Cookie(-name=>'show',-value=>"$show",-expires=>'+1y');
  36: 	} 
  37: if(($cookies{'clean'}=~/yes/) or ($i{'do'}=~/clean/)){
  38: 	if($i{'do'}=~/clean/){
  39: 		$c2 = new CGI::Cookie(-name=>'clean',-value=>'yes',-expires=>'+1y');
  40: 		}
  41: 	$cc=' checked';
  42: 	}
  43: if(($i{'go'}=~/show/) && ($i{'do'}!~/clean/)){
  44:  	$c2 = new CGI::Cookie(-name=>'clean',-value=>'no',-expires=>'+1y');
  45: 	$cc=();
  46: 	}
  47: 
  48: print header(-cookie=>[$c1,$c2]);
  49: print<<HTML;
  50: <html><head>
  51: <title>$show Recent $ENV{'SERVER_NAME'} Server Errors</title>
  52: <SCRIPT LANGUAGE="JavaScript"><!--
  53: function me(){
  54: document.forms[0].elements[0].select();
  55: document.forms[0].elements[0].focus();}
  56: //--></SCRIPT></head>
  57: <body bgcolor="#c0c0c0" text="#000000" onload="me()">
  58: <table border="0" cellpadding="0" cellspacing="0" width="100%">
  59: <tr><form method="post"><td>
  60: <h3>$show Recent <a href="http://$ENV{'SERVER_NAME'}/">
  61: $ENV{'SERVER_NAME'}</a> Server Errors<br>
  62: <font size="-1">$time</font><br></h3></td>
  63: <td align="right"><small>
  64: clean</small><input type="checkbox" name="do" value="clean"$cc>
  65: <input type="text" name="n" value="$show" size="3" maxlength="4" onfocus="select()">
  66: <input type="submit" name="go" value="show"></td>
  67: <td rowspan="2">&nbsp;</td></form></tr></table>
  68: <hr>
  69: HTML
  70: @elog=reverse(@elog);
  71: my$c=0;
  72: foreach my $ln(@elog){
  73: 	unless($c==$show){
  74: 		&encode_entities($ln);	
  75: 		$ln=~s|\Q$apache||oi;
  76: 		if($cc=~/checked/){
  77: 			$ln=~s/\s\[(?:notice|error|warn)\]\s\[client.*\]//o;
  78: 			$ln=~s/\s2\d{3}]/\]<br>/o;
  79: 			$ln=~s/\[/<small><b>/o;
  80: 			$ln=~s|\]|</b></small>\n|o;
  81: 			}
  82: 		print $ln.'<hr>';
  83: 		$c++
  84: 		}
  85: 	}
  86: print<<HTML;
  87: <table width="100%"><tr><td colspan="2" align="right"><font size="-1">
  88: <a href="file://$error_log">$error_log</a> = $EL[7] bytes</td></tr></table></body></html>
  89: HTML
  90: exit

Comment on apache errorlog
Download Code
Re: apache errorlog
by TheoPetersen (Priest) on Feb 16, 2001 at 01:42 UTC
    You might want to read up on Apache::ParseLog, which handles the log file parsing more robustly than your example. It automatically reads httpd.conf to find out the format of the log file.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (16)
As of 2014-09-30 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (378 votes), past polls