Created
October 3, 2012 02:26
-
-
Save JCook21/3824584 to your computer and use it in GitHub Desktop.
Error handling file to log all PHP errors to Graylog2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This file sets up exception and error handlers to log errors to our Graylog2 | |
* server. | |
* | |
* @author Jeremy Cook | |
*/ | |
//Add the autoloader generated by composer for dependencies. | |
require_once 'path/to/composer/autoload.php'; | |
/** | |
* Create a closure to handle uncaught exceptions | |
*/ | |
set_exception_handler($handler = function(Exception $e) use (&$handler) { | |
$message = sprintf( | |
'Uncaught exception of type %s thrown in file %s at line %s%s.', | |
get_class($e), | |
$e->getFile(), | |
$e->getLine(), | |
$e->getMessage() ? sprintf(' with message "%s"', $e->getMessage()) : '' | |
); | |
LoggingHelper::getLogger()->addError($message, array( | |
'Exception file' => $e->getFile(), | |
'Exception line' => $e->getLine(), | |
'Exception trace' => $e->getTraceAsString() | |
)); | |
/** | |
* If there was a previous nested exception call this function recursively | |
* to log that too. | |
*/ | |
if ($prev = $e->getPrevious()) { | |
$handler($prev); | |
} | |
}); | |
/** | |
* Set a custom error handler to make sure that errors are logged to Graylog. | |
* Allows any non-fatal errors to be logged to the Graylog2 server. | |
*/ | |
set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext){ | |
$message = 'Error of level '; | |
switch ($errno) { | |
case E_USER_ERROR: | |
$message .= 'E_USER_ERROR'; | |
break; | |
case E_USER_WARNING: | |
$message .= 'E_USER_WARNING'; | |
break; | |
case E_USER_NOTICE: | |
$message .= 'E_USER_NOTICE'; | |
break; | |
case E_STRICT: | |
$message .= 'E_STRICT'; | |
break; | |
case E_RECOVERABLE_ERROR: | |
$message .= 'E_RECOVERABLE_ERROR'; | |
break; | |
case E_DEPRECATED: | |
$message .= 'E_DEPRECATED'; | |
break; | |
case E_USER_DEPRECATED: | |
$message .= 'E_USER_DEPRECATED'; | |
break; | |
case E_NOTICE: | |
$message .= 'E_NOTICE'; | |
break; | |
case E_WARNING: | |
$message .= 'E_WARNING'; | |
break; | |
default: | |
$message .= sprintf('Unknown error level, code of %d passed', $errno); | |
} | |
$message .= sprintf( | |
'. Error message was "%s" in file %s at line %d.', | |
$errstr, | |
$errfile, | |
$errline | |
); | |
LoggingHelper::getLogger()->addError($message, $errcontext); | |
return false;//Returning false will mean that PHP's error handling mechanism will not be bypassed. | |
}); | |
/** | |
* This function will be called before the script exits. | |
* This allows us to catch and log any fatal errors in the Graylog2 server. | |
* This is needed as the set_error_handler function cannot be used to handle | |
* any of the errors in the array below. | |
*/ | |
register_shutdown_function(function(){ | |
$codes = array( | |
1 => 'E_ERROR', | |
4 => 'E_PARSE', | |
16 => 'E_CORE_ERROR', | |
32 => 'E_CORE_WARNING', | |
64 => 'E_COMPILE_ERROR', | |
128 => 'E_COMPILE_WARNING' | |
); | |
$error = error_get_last(); | |
if (is_array($error) && array_key_exists($error['type'], $codes)) { | |
$message = sprintf( | |
'Error of type %s raised in file %s at line %d with message "%s"', | |
$codes[$error['type']], | |
$error['file'], | |
$error['line'], | |
$error['message'] | |
); | |
$logger = LoggingHelper::getLogger(); | |
if (in_array($error['type'], array(32, 128))) { | |
//These errors are warnings and should be logged at a lower level. | |
$logger->addError($message); | |
} else { | |
$logger->addAlert($message); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment