Solved

Error logs at folder level

Posted on 2014-09-23
3
248 Views
Last Modified: 2014-09-24
Hi All,

On some of the shared hosting websites, I noticed that (due to some configurations set by them), the error.log is generated in the same folder where the error occured. This is a nice thing and I am wondering if we can implement the same on our own webserver setup.

I know, we can enable logging in vitualhost config or .htaccess, but it's practially not possible to keep creating htaccess files for each folder that gets added to the code etc..

Any thoughts?


Thanks,
Ron
0
Comment
Question by:Starquest321
  • 2
3 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
In my PHP initialization settings I have error_log set to (drum roll) "error_log" and whenever errors occur, the error_log file gets created in the same directory where the errors occurred.  These three settings seem to be in play:

log_errors = On
log_errors_max_len = 1024
error_log = error_log

I put this script in my web root directory and run it to find all of the error logs, and optionally to delete them.

<?php // find_error_log.php
error_reporting(E_ALL);
ob_start();

// PUT THESE SETTINGS IN PHP.INI
// log_errors = On
// log_errors_max_len = 1024
// error_log = error_log

// PUT THIS SCRIPT IN THE WEB ROOT DIRECTORY
$path = realpath(getcwd());
$plen = strlen($path);

// THE ERROR LOG FILE NAME
$signal = strrev('error_log');


// IF THERE IS A POST-METHOD REQUEST TO DELETE THE ERROR LOG
if (!empty($_POST['log']))
{
    // MAKE SURE WE ONLY UNLINK THE ERROR LOG FILE
    $test = strrev($_POST['log']);
    if (strpos($test, $signal) === 0)
    {
        unlink($path . $_POST['log']);
        echo '<h3>' . $_POST['log'] . ' Discarded</h3>';
    }
}


// SEE http://php.net/manual/en/class.recursivedirectoryiterator.php#85805
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objs as $name => $obj)
{
    // PROCESS THE ERROR LOG ONLY
    $test = strrev($name);
    if (strpos($test, $signal) === 0)
    {
        $name = substr($name, $plen);
        $form = <<<EOD
<form method="post" style="margin:0; padding:0; display:inline;!important">
<b>$name</b>
<input type="submit" value="Discard?" />
<input type="hidden" name="log" value="$name" />
</form>
EOD;
        echo $form;

        // SHOW THE CONTENTS OF THE ERROR LOG
        echo '<pre>';
        print_r(file_get_contents($path . $name));
        echo "</pre>";
    }
}

$out = ob_get_contents();
if (empty($out)) echo '<h3>Good News! No error_log found.</h3>';

Open in new window

0
 

Author Closing Comment

by:Starquest321
Comment Utility
Thanks, that worked
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Gr8!  Thanks for the points, ~Ray
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now