1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

error_log Reporter

Discussion in 'PHP & Perl' started by seohug, Mar 21, 2017.

  1. seohug

    seohug Jr. VIP Jr. VIP

    Joined:
    Feb 14, 2011
    Messages:
    336
    Likes Received:
    61
    Gender:
    Male
    Occupation:
    Automator
    Location:
    UK
    Home Page:
    Let's say you run Wordpress, Durpal or any CMS or site that runs on PHP - which are millions out there. Your CMS or script fails to reach the database, or process a query or have some sort of an issue.

    In most hosts default config, this will log an error_log with details of the issue.
    You don't know that this has happened because you don't systematically login to check.
    All site monitoring services will not pick this up, because your site could be up (returning http status code 200), but not function or returning some error.

    How do you watch out for this?
    This is where the simple free error_log Reporter script below comes in.

    Code:
    <?php
    // Author: seohug @ BHW
    // The purpose of this script is to check for presence of error_log, if found, it collects it content and send it by email. It will also rename it with timestamp to prevent further alerts on the same error_log.
    
    // edit this
    $to_email = "[email protected]";
    $from_email = "From: [email protected]" . "\r\n";
    
    date_default_timezone_set("Europe/London");
    $timestamp = date("d-m-Y-his");
    $file = "error_log";
    
    
    if (file_exists($file)) {
       $file_content = file_get_contents($file);
       $file_content = wordwrap($file_content,70);
       echo "error_log file found with the following content\n" . $file_content;
       //mail($to_email, "error_log found", $file_content, $from_email);
       rename($file, $file . "-" . $timestamp . ".txt");
    } else {
       echo "No error file found";
    }
    
    ?>
    
    Just edit:
    $to_email : where do you want the email to do
    $from_email : let's say your site is makemoney.com, make this [email protected]
    If you want, you can change Europe/London to America/Chicago or where ever you are.

    Once done, copy create a file call it error_log_report.php and save it to a location where you want to monitor for error_log, usually root of your site. You can make multiple copies in other folders if needed.

    Now run this on a schedule using CRON and set it to execute every minute, it runs in split second, so no performance issues. If you use cPanel, this is a piece of cake:
    [​IMG]

    Just edit the path under command to be where you placed the error_log_reporter.php

    Want to test it?
    Create a file with the name exactly error_log and wait 1-2 minutes. If you the file doesn't get renamed and you don't get an email, go back and check for typos...etc.

    I hope this helps.
     
  2. tasburrfoot

    tasburrfoot Regular Member

    Joined:
    Dec 16, 2008
    Messages:
    323
    Likes Received:
    152
    PHP:
    <?php
    // Author: seohug @ BHW
    // The purpose of this script is to check for presence of error_log, if found, it collects it content and send it by email. It will also rename it with timestamp to prevent further alerts on the same error_log.

    // edit this
    $to_email "[email protected]";
    $from_email "From: [email protected]"\r\n";

    date_default_timezone_set("Europe/London");
    $timestamp date("d-m-Y-his");
    $file "error_log";


    if (
    file_exists($file)) {
       
    $file_content wordwrap(file_get_contents($file),70);
       
    mail($to_email"error_log found"$file_content$from_email);
       
    rename($file$file "-" $timestamp ".txt");
    }

    ?>
    Slight edit:

    You commented out the mail part, so it's not doing what you wanted it to do.

    Also, if it's a cron job, echoing does absolutely nothing(you're flagging it in that screenshot to ignore output) - so I removed that as well.