Skip to content

Instantly share code, notes, and snippets.

@nicoschoenmaker
Created February 19, 2016 15:53
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 nicoschoenmaker/02386b968893adcd465e to your computer and use it in GitHub Desktop.
Save nicoschoenmaker/02386b968893adcd465e to your computer and use it in GitHub Desktop.
Logging Symfony deprecation errors to the log
/**
* The goal of this handler is to write all silenced (@) E_USER_DEPRECATION
* errors to the server error log.
*
* Please note that only one error handler can be active at any given point in
* time. This means two things:
* - We have to be a good citizen and call the previous error handler (if any).
* - We're fully dependant for later error handlers to do the same thing.
*/
class DeprecationErrorHandler
{
private $previous_handler;
public function __construct()
{
$this->previous_handler = set_error_handler([$this, 'handleError']);
}
/**
* Logs silenced (error_reporting = 0) E_USER_DEPRECATED errors, and
* forwards to other error handler.
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @param array $errcontext
*/
public function handleError($errno, $errstr, $errfile = null, $errline = null, array $errcontext = [])
{
// Suppressing AsseticBundle v2.7.1 since it spams errors.
// See https://github.com/symfony/assetic-bundle/pull/395
if ($errno === E_USER_DEPRECATED
&& error_reporting() === 0
&& strpos($errstr, 'AsseticBundle') === false
) {
error_log(sprintf('PHP Deprecated: %s in %s on line %d', $errstr, $errfile, $errline), 0);
}
// Always attempt to call the previous handler
if (is_callable($this->previous_handler)) {
return call_user_func($this->previous_handler, $errno, $errstr, $errfile, $errline, $errcontext);
}
// Returning false indicates we did not handle the error, so normal handling should continue.
return false;
}
}
new DeprecationErrorHandler();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment