How can I log fatal errors in PHP?

14,016

Solution 1

It is not possible to handle fatal errors using a custom error handler.

The best solution is simply enabling error logging (e.g. to syslog) in your php.ini and then using a tool like logcheck/logsentry to receive regular emails about unusual syslog entries.
Instead of syslog PHP can also log errors to a file - simply have a look at the error logging options of php.ini.

log_errors = On
error_log = syslog
error_log = /path/to/some/folder/phperrors.log

Obviously you only want to use one of the error_log lines.

Solution 2

There is a way to cope with your task and actually you can set a custom error handler on fatal errors.

You can do it this way:

ini_set('error_reporting', E_ERROR);
register_shutdown_function("fatal_handler");
function fatal_handler() {
    $error = error_get_last();
    // Do whatever you want with this error. For example:
    YourDBApplicationLayer::writeFatal($error);
}

Solution 3

You could have all your base classes belong to a super-class utilizing method overloading:

class Base 
{
    public function __call($name)
    {
        MyLog::logError(...);
        trigger_error("Function ".get_class($this)."::$name doesn't exist", 
            E_USER_ERROR);
    }
}

Attempts to invoke non-existing methods of classes derived from Base would be ultimately handled by Base::__call(). For static methods, accordingly, there's __callStatic() (as of PHP 5.3).

Share:
14,016
Hamidreza
Author by

Hamidreza

Updated on June 19, 2022

Comments

  • Hamidreza
    Hamidreza almost 2 years

    How can I log the following error to a text file or database?

    Fatal error: Call to undefined method PROJECTS::ssss()

  • Hamidreza
    Hamidreza about 12 years
    I have an error handler to handling my error but some error like above, I could not handle them or log.
  • ThiefMaster
    ThiefMaster about 12 years
    Ugh, i'm pretty sure he wants a generic solution.
  • Hamidreza
    Hamidreza about 12 years
    I do not know where this error happen. i need to log all type error
  • Hamidreza
    Hamidreza about 12 years
    this answer dose not help me because i dont know where or when this error happen
  • Sebastián Grignoli
    Sebastián Grignoli over 11 years
    You should be able to handle a fatal error with register_shutdown_function()
  • ThiefMaster
    ThiefMaster over 11 years
    @SebastiánGrignoli: There is no clean way to detect if there was an error in that function.
  • Sebastián Grignoli
    Sebastián Grignoli over 11 years
    <?php function cleanup(){ print_r(error_get_last()); } register_shutdown_function("cleanup");asdfasdf();
  • Sebastián Grignoli
    Sebastián Grignoli over 11 years
    I agree it's not 100% clean, but it's enough for logging the error, I think.
  • Peter Mortensen
    Peter Mortensen over 2 years
    What does the strikeout for "base" mean? Is it an old revision? - can it be deleted? Or something else?
  • Peter Mortensen
    Peter Mortensen over 2 years
    Re "It is not possible to handle fatal errors using a custom error handler.": Did it change with PHP 7 or not?
  • Linus Kleen
    Linus Kleen over 2 years
    "All your base." Get it?