PHP code example of morrislaptop / error-tracker-adapter

1. Go to this page and download the library: Download morrislaptop/error-tracker-adapter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

morrislaptop / error-tracker-adapter example snippets


$monolog = new Monolog\Logger();
$monolog->pushHandler(new FlowdockHandler('apiToken', Monolog\Logger::ERROR));
$logger = new Morrislaptop\ErrorTracker\Provider\Log($monolog);

$tracker = new Morrislaptop\ErrorTracker\Adapter\Sentry(new Raven_Client('https://blah.com'));
// or
$tracker = new Morrislaptop\ErrorTracker\Adapter\BugSnag(new Bugsnag_Client('2344324342'));

$handler = new Morrislaptop\ErrorTracker\ExceptionHandler($tracker);
$handler->bootstrap();

 namespace App;

use Exception;
use Morrislaptop\ErrorTracker\Tracker;

class ExceptionHandler
{
	/**
	 * @var Tracker
	 */
	protected $tracker;

	/**
	 * @param Tracker $tracker
	 */
	function __construct(Tracker $tracker)
	{
		$this->tracker = $tracker;
	}

	/**
	 * Bootstrap this class into the runtime
	 */
	public function bootstrap() {
		error_reporting(-1);
		set_error_handler([$this, 'handleError']);
		set_exception_handler([$this, 'handleException']);
		register_shutdown_function([$this, 'handleShutdown']);
	}

	/**
	 * Convert a PHP error to an ErrorException.
	 *
	 * @param  int  $level
	 * @param  string  $message
	 * @param  string  $file
	 * @param  int  $line
	 * @param  array  $context
	 * @return void
	 *
	 * @throws \ErrorException
	 */
	public function handleError($level, $message, $file = '', $line = 0, $context = array())
	{
		if (error_reporting() & $level)
		{
			throw new ErrorException($message, 0, $level, $file, $line);
		}
	}

	/**
	 * Handle an uncaught exception from the application.
	 *
	 * Note: Most exceptions can be handled via the try / catch block in
	 * the HTTP and Console kernels. But, fatal error exceptions must
	 * be handled differently since they are not normal exceptions.
	 *
	 * @param  \Exception  $e
	 * @return void
	 */
	public function handleException($e)
	{
		$this->tracker->report($e);

		throw $e; // throw back to core PHP to render
	}

	/**
	 * Handle the PHP shutdown event.
	 *
	 * @return void
	 */
	public function handleShutdown()
	{
		if ( ! is_null($error = error_get_last()) && in_array($error['type'], [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]))
		{
			$this->handleException(new ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line']));
		}
	}

}


$monolog = new Monolog\Logger();
$monolog->pushHandler(new StreamHandler('path/to/your.log', Monolog\Logger::ERROR));
$logger = new Morrislaptop\ErrorTracker\Provider\Log($monolog);

$sentry = new Morrislaptop\ErrorTracker\Adapter\Sentry(new Raven_Client('https://blah.com'));
$bugsnag = new Morrislaptop\ErrorTracker\Adapter\BugSnag(new Bugsnag_Client('2344324342'));

// If you wanted to fallback to a log if reporting to Sentry fails..
$chain = new Morrislaptop\ErrorTracker\Group\Chain();
$chain->add($sentry);
$chain->add($logger);
$chain->report(new \Exception('This will be logged to file if Sentry is down'));

// If you wanted to report to Bugsnag AND Sentry to compare those platforms
$chain = new Morrislaptop\ErrorTracker\Group\Chain();
$chain->add($sentry);
$chain->add($bugsnag);
$chain->report(new \Exception('Which is the better platform? This exception will go to both'));


$this->tracker->report($e, ['user_id' => Session::get('user_id')]);