rubo77
rubo77

Reputation: 20827

Counter how many errors were triggered in PHP

If I have to call to a third-party legacy function that doesn't allow proper error checking, is there a global counter on how many error-notices were already triggered by a php script?

I would like to detect in my script if there were errors/warnings/notices during a function call triggered and react accordingly in my script.

As a workaround, I would go for counting the number of rows in the apache errorlog before and after the function call, but that doesn't work:

<?php
$errorlogsize_before=filesize(DEBUGMODE_LOGFILE);
third_party_function();
$errorlogsize_after=filesize(DEBUGMODE_LOGFILE);
echo "Logsize before:".$errorlogsize_before."; Logsize after:".$errorlogsize_after."

They display the same filesize.

Upvotes: 0

Views: 65

Answers (3)

Mantas
Mantas

Reputation: 56

You can use function set_error_handler and implement your own counters

For example: I use custom error handler to send UDP data to statsd daemon when PHP error occurs. Then you can graph errors by type and by page/file

Upvotes: 2

Nick Prozee
Nick Prozee

Reputation: 2913

You can create your own counter:

<?php
$counter = 0;
$site = "://testsite.hjkh/"; //<== IncorrectSite
 fopen($site,"r")
 or die(errorCount + 1); //<==ErrorCount +1
 ?>

At the end of your script, Echo the ErrorCount.
Take a look here: http://www.w3schools.com/php/func_misc_die.asp

Upvotes: 0

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146410

I'll assume your use case is a call to a third-party legacy function that doesn't allow proper error checking. In that case, you can change the error handler temporarily with set_error_handler() and restore it afterwards with restore_error_handler(). Other alternatives like error_get_last() discard all errors but the last one.

Upvotes: 3

Related Questions