markjugg has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm writing a CGI script to handle "500" server errors with Apache. As part of this, I'd like to show the last few lines from the Apache error log.

I noticed the error log location is not in the CGI environment (at least not in the %ENV hash). Is there a way I can dynamically figure out where the error log is? (better yet, do you have a pointer to an example of this?)

For those concerned about security, this will be used by developers only, not by the public. :) Thanks!

-mark

  • Comment on Reading the last fews lines of the Apache error log

Replies are listed 'Best First'.
Re: Reading the last fews lines of the Apache error log
by Fastolfe (Vicar) on Feb 06, 2001 at 02:28 UTC
    Be VERY CAREFUL doing this in a production setting. Items in a web server's error log could easily contain very privileged information, including database usernames/passwords, or snippets of code that could contain other sensitive information. Additionally, this opens you up to some cross-site scripting vulnerabilities. I would be very wary of doing something like this on a production server. Remember that the "last few lines" of your error log could easily be output from another script!

    If you're just trying to debug your CGI scripts, consider using CGI::Carp to do this. I'd still turn this off on a production site, though.

Re: Reading the last fews lines of the Apache error log
by arturo (Vicar) on Feb 06, 2001 at 02:28 UTC

    The easy way is: manually check the apache configuration file httpd.conf (which often lives in /etc/ or /etc/httpd, but can be anywhere) and find out where the error log goes, then set that as the value of a scalar.

    The second is to have your script parse the httpd.conf file and figure out the location of the error file itself. I really don't recommend the second though; first, it's not a value that's likely to change and second, you really don't want your script to go digging through a file with potentially sensitive information (for that matter, this goes for the error logfile).

    I note, also, that if your script fails to compile (thus, sending out no headers), you'll still get bupkus information through this method. Make your scripts bomb with useful info while you're developing, say, by using

    use CGI::Carp qw(fatalsToBrowser);
    or some such.

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

(jeffa) Re: Reading the last fews lines of the Apache error log
by jeffa (Bishop) on Feb 06, 2001 at 02:33 UTC
    I concur with fastolfe and arturo. You will be better off monitoring the output of 'tail -f error.log' (assuming you are using a *NIX platform) while you test your CGI scripts. Developers could all be part of the same group that has read priviledges set to the error log.

    This is yet another example of 'convience' that just opens up a big ole can of worms.

    Jeff

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
(crazyinsomniac) Re: Reading the last fews lines of the Apache error log
by crazyinsomniac (Prior) on Feb 06, 2001 at 02:44 UTC
    I agree with the monks above. But if you don't want to reveal line numbers and maybe some variable names, why don't you just keep your own error log. Check out this for details.

    update:
    DOUGH!!! I totally misread the question. kickstart is right just edit your httpd.conf file, i just did this the other day. It's pretty easy, but you will need to reload the server if you messup.

    I suggest IndigoPerl if you got windows. It loads pretty fast and works well. It is very good for testing. Here's a review.

    Even abbot's make mistakes, what can you do.

    "cRaZy is co01, but sometimes cRaZy is cRaZy".
                                                          - crazyinsomniac

      Not sure what you were pointing to.

      You could also just call something like this in httpd.conf: ErrorDocument 500 /cgi-bin/error500.cgi

      ...in a previous job I wrote a script like this for 404 errors that tracked the error, logged it, kept some statistics, then emailed the appropriate webmaster based on the URL (in this case the domain name) with the error (containing the $ENV{'HTTP_REFERER'} in case it was helpful to track down the error).

      Kickstart

Re: Reading the last fews lines of the Apache error log
by Kickstart (Pilgrim) on Feb 06, 2001 at 04:41 UTC
    The error log can be defined in each virtual host, as well as globally, so assuming your script has read access of httpd.conf, you could parse that to find out the location of the log (then run 'tail -3 $log_location' or whatever').

    Kickstart

Re: Reading the last fews lines of the Apache error log
by dws (Chancellor) on Feb 06, 2001 at 02:57 UTC
    It might help if you could tell is why you want to read the end of the log.
Re: Reading the last fews lines of the Apache error log
by ok (Beadle) on Feb 06, 2001 at 02:31 UTC
    Grep it from httpd.conf? Use find? Not sure how this relates to Perl though...