J
J
JackShcherbakov2018-04-01 12:32:10
PHP
JackShcherbakov, 2018-04-01 12:32:10

Why don't warnings and custom errors end up in the PHP error log?

Hello colleagues! I recently encountered the following problem:
I want to notify users if something went wrong. So, if something went wrong, then instead of the usual page (which he expected to see), the user will receive a message stating that something went wrong and he should try again later.
I sketched this code:

<style>
  .error_message{
    background-color:blue;
    color:white;
    border: 2px black solid;
  }
</style>
<?
set_error_handler(function($errno, $error_message, $err_file, $errline){
  echo "<div class='error_message'>Произошла ошибка уровня $errno. Попробуйте заглянуть позже.</div>";

  if (($errno == E_USER_ERROR) || ($errno == E_ERROR)) {
    echo "<p>Fatal error. Program ending.</p>";
    exit;
  }

  echo "<hr/>";
});
trigger_error('Trigger function called.', E_USER_NOTICE);
fopen('nofile', 'r');
trigger_error('This computer is beige.', E_USER_WARNING);
include ('nofile');
trigger_error('This computer will self destruct in 15 seconds.', E_USER_ERROR);

?>

Here is the configuration:
error_reporting = E_ALL 
display_errors = Off
display_startup_errors = On
log_errors = On

I'm trying to make the user know that some kind of error has occurred, and I'm trying to send the information about the error to the error log. But for some reason, errors do not reach the log. If you remove set_exception_handler, it doesn't work either*. What's the matter? I noticed that only fatal errors (and other serious errors, such as parsing errors) reach the log. What am I doing wrong and how to fix it? What? I do not understand?
Here is the output of this script in the browser (without CSS):
Произошла ошибка уровня 1024. Попробуйте заглянуть позже.
Произошла ошибка уровня 2. Попробуйте заглянуть позже.
Произошла ошибка уровня 512. Попробуйте заглянуть позже.
Произошла ошибка уровня 2. Попробуйте заглянуть позже.
Произошла ошибка уровня 2. Попробуйте заглянуть позже.
Произошла ошибка уровня 256. Попробуйте заглянуть позже.
Fatal error. Program ending.

I would like to thank everyone in advance who will help.
UPD:
*It works without set_error_handler.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JackShcherbakov, 2018-04-01
@JackShcherbakov

I solved the problem with logging by returning false so that error handling is transferred to the PHP handler, which will do the logging.

set_error_handler(function($errno, $error_message, $err_file, $errline){
  echo "<div class='error_message'>Произошла ошибка уровня $errno. Попробуйте заглянуть позже.</div>";

  if (($errno == E_USER_ERROR) || ($errno == E_ERROR)) {
    echo "<p>Fatal error. Program ending.</p>";
    exit;
  }

  echo "<hr/>";
  return false;
});

You can also keep a log yourself, but I want to entrust this matter to PHP.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question