1

I know this is not proper error handling, but it is necessary to solve my problem. I am running a script that parses data in a loop over many hours, so I create a logs of what is happening in the script at various moments to ensure everything is being handled correctly. All my logs are saved in a different folder than the .php file. I was recording and logging tons of information. But, all the connecting to the log file was tremendously slowing down my program. Instead i have a global $write variable and I just append it. In order to speed up my program I just want to append my log file with $write, once at the end of each loop. The problem is that I set a max_execution_time for my program and if it dies before the loop is done, all my logs for that loop are lost. As part of my register_shutdown_function() I want to log $write.

Here's what I have:

register_shutdown_function( "fatal_handler");
function fatal_handler() {

  global $file, $write;
  $errfile = "unknown file";
  $errstr  = "shutdown";
  $errno   = E_CORE_ERROR;
  $errline = 0;

  $error = error_get_last();

  if( $error !== NULL) {
    $errno   = $error["type"];
    $errfile = $error["file"];
    $errline = $error["line"];
    $errstr  = $error["message"];

    file_put_contents($file, "\n\nFATAL ERROR\n\n", FILE_APPEND);
    file_put_contents($file, $write, FILE_APPEND);
  }
}

The problem is that I get this error message: Warning: file_put_contents(../../resultLogs/log.txt): failed to open stream: No such file or directory in C:\www\php\data_utils\program.php on line 64

Mind you $file works fine in the rest of the program... Help please!

1 Answer 1

1

See the docs :

Working directory of the script can change inside the shutdown function under some web servers, e.g. Apache.

So be sure to use an absolute path when doing this, i.e

file_put_contents('<absolute-path-to>/resultLogs/log.txt', $write, FILE_APPEND);

As you wrote "All my logs are saved in a different folder than the .php file", and I guess that your problem is, that you are having the $file constant relative to that .php file but the directory changes upon error..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.