I’m building a basic error logging feature in my application, where I just record a custom message like “product ID was missing” and record it in the DB. Is there any way I can dynamically capture from which class or function the error was triggered (ie the function from where log_error() was called), or the line number?
I’m building a basic error logging feature in my application, where I just record
Share
From PHP 5.2.0 on, there is error_get_last(). (Cheers @Gordon) It will give you an array with all the information about the last error:
A more flexible approach to handling errors in general is defining a custom error handler.
A custom error handler accepts the following parameters:
Backtraces
The most information you’ll get with a backtrace – useful in many situations. To do that, combine the error handler with debug_backtrace(). It will give you an array with every point in the call stack (i.e. which function called which called which called which… until the error.)
Stack traces cost a lot of computing time, so you should not use them in production and have a switch to turn them on and off.