Skip to content

Instantly share code, notes, and snippets.

@lillesvin
Created May 26, 2015 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lillesvin/c2a48f19a2a6df4dc9a0 to your computer and use it in GitHub Desktop.
Save lillesvin/c2a48f19a2a6df4dc9a0 to your computer and use it in GitHub Desktop.
Handler for logging last error even when fatal
class ShutdownHandler
{
private $catch;
private $error;
public function __construct($catch = null)
{
$this->catch = $catch ?: E_ERROR | E_PARSE;
}
private function getError()
{
$this->error = error_get_last();
}
private function isPanicWorthy()
{
if (!is_null($this->error)) {
return $this->error['type'] & $this->catch;
}
return false;
}
public function panic()
{
$this->getError();
if ($this->isPanicWorthy()) {
/**
* Send error metrics to Librato, New Relic,
* via email or whatever.
*/
}
}
}
register_shutdown_function([new ShutdownHandler(), 'panic']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment