0

I have this code here to create error log:

  error_log(date("[Y-m-d H:i:s]:")." You messed up!", 3, "../my-errors.log");

Here, we can see the custom error 'You messed up!' that I have set to print in the error log. I don't want to use the custom error here. Instead of this I want to set the Errors/Warnings/Notices that are generated by PHP itself.

Is this possible and how can we do that?

Thankyou

  • 4
    If you want to log PHP's regular errors / warnings, then just enable php logging. They are already probably being logged to a directory like /var/log/apache/error.log. Or just change the directory if you want to log it to a custom file: https://stackoverflow.com/questions/3531703/how-to-log-errors-and-warnings-into-a-file – Ole Haugset Nov 16 '17 at 09:52

1 Answers1

1

if I understood it well, you're looking for error_get_last():

array error_get_last ( void )

error_get_last — Get the last occurred error. / Gets information about the last error that occurred.

Take a look:

$last_error = error_get_last();
$formated_last_error = sprintf('%s in %s on line %d'.PHP_EOL, 
   $last_error['message'], $last_error['file'], $last_error['line']);

error_log(date(DATE_ATOM) . $formated_last_error().PHP_EOL, 3, '/tmp/logs.log');

However, you should take a look at set_error_handler() function which is a general approach.

Community
  • 1
  • 1
felipsmartins
  • 13,269
  • 4
  • 48
  • 56