Search code examples
phperror-logging

PHP Static Function Called Twice


I'm building a MVC PHP framework. I'm working on error logging. I'm testing a system to show and log (in a .txt file) errors thrown by a try, catch block. Since it's short, I've included the whole of index.php:

<?php
  // *** SETUP THE DOCUMENT ***
  // Declare strict_types.
  declare(strict_types = 1);

  // Include the autoload file.
  require_once "classes/autoload.php";

  // Create a new config variable to allow us to call the config file from anywhere.
  new Config();

  // Setup error reporting according to the config.
  error_reporting(Config::$config['defaults']['errReporting']);
  ini_set("display_errors", Config::$config['defaults']['ini_setErrors']);

  // Errors::showCatch("Test");

  try {
    $test = new QueryExamples();
    $test->getTexts();
  } catch (Error $e) {
    Errors::showCatch($e);
  }

  unset($test);

I've got an autoloader setup already. I'll include the code of that if anyone wants it. The next bit is a file called errors.php which is called by the try, catch and, depending on how the configuration file for the framework is setup, shows the message on the screen and/or sends it to a log file:

class Errors{

    static function showCatch($errMessage){
      // If manual errors are turned on in the config file.
      if(Config::$config['defaults']['manErrors'] == 1){
        // Show the error message.
        echo "<br />{$errMessage}<br />";
      }

      // Log the error.
      Logs::logManError($errMessage);
      die();
    }

}

How and where it is logged in the file is handled by another static function in another document:

class Logs{

    static function logManError($errMessage) {
      // If custom error logging is turned on in the config file:
      if (Config::$config['defaults']['customErrorsLogging'] == 1) {
        // Get the file path from the config file.
        $manErrors_file = Config::$config['paths']['customErrors'];

        // Format the error message.
        $logTxt = "New Custom Error - [".date("d/m/Y - H:i:s")."]\n".$errMessage."\n\n";

        file_put_contents($manErrors_file, $logTxt, FILE_APPEND);
      }
    }

}

The output in the log file:

New Custom Error - [23/03/2020 - 01:50:17]
Error: (Error message)

New Custom Error - [23/03/2020 - 01:50:17]
Error: (Error message)

I changed the error message to try and find where the errors were being called:

// Format the error message.
// $logTxt = "New Custom Error - [".date("d/m/Y - H:i:s")."]\n".$errMessage."\n\n";
$logTxt = debug_backtrace()["0"]['file']." ".debug_backtrace()['0']['line']."\n".debug_backtrace()['1']['file']." ".debug_backtrace()['1']['line']."\n\n";

The output in the log file:

F:\xampp\htdocs\FRED 0.0.0\classes\errors.php 21
F:\xampp\htdocs\FRED 0.0.0\index.php 22

F:\xampp\htdocs\FRED 0.0.0\classes\errors.php 21
F:\xampp\htdocs\FRED 0.0.0\index.php 22

errors.php:21 is where I called the the Logs:logManError() static function and index.php:22 is where I called the Errors:showCatch() function in the try, catch block.

I also tried adding a global variable to index.php to check how many times the showCatch() function was being called:

(index.php)

$GLOBALS['i'] = 0;

(errors.php)

$GLOBALS['i'] += 1;

// Log the error.
Logs::logManError($errMessage."<br />".$GLOBALS['i']);
die();

The output:

New Custom Error - [23/03/2020 - 01:50:17]
Error: (Error message)
1

New Custom Error - [23/03/2020 - 01:50:17]
Error: (Error message)
1

Solution

  • Okay. Here is the answer. After doing more research there appears to be nothing wrong with my code. It seems that it's something to do with the rewrite engine combined with a few other things. I've included a link to the page that gave me the solution.

    https://www.sitepoint.com/community/t/duplicate-entries-with-file-put-contents-when-rewriteengine-on/16188/11