Skip to content

Instantly share code, notes, and snippets.

@intech
Last active April 27, 2016 15:04
Show Gist options
  • Save intech/ed1a9f26b6a8df4275218ddcf9ee735a to your computer and use it in GitHub Desktop.
Save intech/ed1a9f26b6a8df4275218ddcf9ee735a to your computer and use it in GitHub Desktop.
Laravel to elasticsearch error handler by udp json
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
NotFoundHttpException::class,
ValidationException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
$trace = [];
$stack = array_slice($e->getTrace(), 0, 5);
foreach($stack as $id => $step) {
unset($step['args'],$step['type']);
$trace[$id] = $step;
}
$request = request();
$log = [
'host' => $request->header('host'),
'client' => $request->ip(),
'useragent' => $request->header('user-agent'),
'url' => $request->url(),
'method' => $request->method(),
'cookies' => $request->cookie(),
'data' => $request->all(),
'files' => $request->file(),
'code' => $e->getCode(),
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => array_values($trace),
'session' => $request->session()->all()
];
if(($fp = stream_socket_client('udp://10.0.1.39:9999', $errno, $errstr, 60)) !== false)
{
$log = json_encode($log);
stream_set_blocking($fp, 0);
fwrite($fp, $log);
fclose($fp);
}
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
return parent::render($request, $e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment