Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are custom PHP logging and logging all possible errors mutually exclusive?

Tags:

php

I've been using set_error_handler to override the default php error handling with the sole purpose to have customized errors logging. but I've come to a conclusion that one simply can't have customized error logging and log all possible errors.

1) You can use set_error_handler() - but this function, quote from php manual:

following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT

So - by going this route - your customized log won't have these errors logged?

2) Second way is to use register_shutdown_function() and then run error_get_last() to get the the error... But - error_get_last() ... only gets the last error ... and while you might have multiple warnings and notices during script execution - this approach will only allow you to log the most recent error, notice, warning - right?

So - IMHO - I'm not seeing any way around this. Looks like if one wants to have the most complete error log - should just stick to default php logger - right?

like image 717
Stann Avatar asked Apr 12 '11 19:04

Stann


2 Answers

Second way is to use register_shutdown_function() and then run error_get_last() to get the the error... But - error_get_last()...only gets the last error

Right, but if it's an error type that killed the script, it would indeed be the correct error inside your shutdown function. Just in case, you can check the type key in the array that it returns for one of the catchable types. If it could have been caught by another error handler, take no action.

FWIW, I have never seen E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR or E_COMPILE_WARNING happen in the real world. Most compile errors are parse errors.

like image 66
Charles Avatar answered Oct 31 '22 09:10

Charles


1) Thats correct. Its because this errors will stop the whole php-process from working properly and so it doesnt make much sense to allow the software to recover itself. However, if you write software, that breaks that hard, you have other problems than the logging.

2) Seems correct so far. But you also have set_error_handler() for this.

like image 2
KingCrunch Avatar answered Oct 31 '22 08:10

KingCrunch