Tarik
Tarik

Reputation: 81711

Logging all the errors,warning and notices in PHP Automatically

I am looking for a way in which all the errors will be logged automatically with one line of code. If you know Asp.NET, you probably know what I mean like using Application_Error event handler.

I checked PHP Logging framework? question in SO but they all look same, you should log each and every log message manually which means I need to call the log function everywhere I want to log.

What I am looking for not something like this (which is used by KLogger)

require_once 'KLogger.php';
...
$log = new KLogger ( "log.txt" , KLogger::DEBUG );

// Do database work that throws an exception
$log->LogError("An exception was thrown in ThisFunction()");

// Print out some information
$log->LogInfo("Internal Query Time: $time_ms milliseconds");

// Print out the value of some variables
$log->LogDebug("User Count: $User_Count");

Upvotes: 1

Views: 517

Answers (1)

raidenace
raidenace

Reputation: 12826

You can create your own custom error handler function in PHP and set it as the error handler

something like:

function handle_error($error_level,$error_message)
{
    /*
    here do what you want...typically log it into db/file/send out 
    emails based on error level etc..
    */
}

and to set this function as your default error handler for PHP add this line:

set_error_handler("handle_error"); 

All errors will now be handled by PHP based on what is written inside handle_error

Upvotes: 6

Related Questions