-
-
Save sixlive/933abf42214fc2c51671facd3939df87 to your computer and use it in GitHub Desktop.
This file contains 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 | |
namespace App\Logging; | |
use Gelf\Publisher; | |
use Monolog\Logger; | |
use Monolog\Handler\GelfHandler; | |
use Gelf\Transport\HttpTransport; | |
use Monolog\Formatter\GelfMessageFormatter; | |
class GraylogLogger | |
{ | |
public function __invoke(array $config) | |
{ | |
return tap(new Logger('graylog'), function ($logger) { | |
$logger->pushHandler($this->gelfHandler()); | |
}); | |
} | |
private function gelfHandler() | |
{ | |
return tap(new GelfHandler($this->gelfPublisher()), function ($handler) { | |
$handler->setFormatter(new GelfMessageFormatter(config('graylog.source_name'))); | |
}); | |
} | |
private function gelfPublisher() | |
{ | |
return tap(new Publisher, function ($publisher) { | |
$publisher->addTransport($this->httpTransport()); | |
}); | |
} | |
private function httpTransport() | |
{ | |
return new HttpTransport( | |
config('graylog.http_host'), | |
config('graylog.http_port') | |
); | |
} | |
} |
This file contains 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 | |
use App\Logging\GraylogLogger; | |
return [ | |
/* | |
|-------------------------------------------------------------------------- | |
| Default Log Channel | |
|-------------------------------------------------------------------------- | |
| | |
| This option defines the default log channel that gets used when writing | |
| messages to the logs. The name specified in this option should match | |
| one of the channels defined in the "channels" configuration array. | |
| | |
*/ | |
'default' => env('LOG_CHANNEL', 'stack'), | |
/* | |
|-------------------------------------------------------------------------- | |
| Log Channels | |
|-------------------------------------------------------------------------- | |
| | |
| Here you may configure the log channels for your application. Out of | |
| the box, Laravel uses the Monolog PHP logging library. This gives | |
| you a variety of powerful log handlers / formatters to utilize. | |
| | |
| Available Drivers: "single", "daily", "slack", "syslog", | |
| "errorlog", "custom", "stack" | |
| | |
*/ | |
'channels' => [ | |
'stack' => [ | |
'driver' => 'stack', | |
'channels' => ['single'], | |
], | |
'graylog' => [ | |
'driver' => 'custom', | |
'via' => GraylogLogger::class, | |
], | |
'single' => [ | |
'driver' => 'single', | |
'path' => storage_path('logs/laravel.log'), | |
'level' => 'debug', | |
], | |
'daily' => [ | |
'driver' => 'daily', | |
'path' => storage_path('logs/laravel.log'), | |
'level' => 'debug', | |
'days' => 7, | |
], | |
'slack' => [ | |
'driver' => 'slack', | |
'url' => env('LOG_SLACK_WEBHOOK_URL'), | |
'username' => 'Laravel Log', | |
'emoji' => ':boom:', | |
'level' => 'critical', | |
], | |
'syslog' => [ | |
'driver' => 'syslog', | |
'level' => 'debug', | |
], | |
'errorlog' => [ | |
'driver' => 'errorlog', | |
'level' => 'debug', | |
], | |
], | |
]; |
This file contains 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 | |
namespace App\Listeners; | |
use App\Events\RequestCompleted; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
class LogRequestDataToLogger implements ShouldQueue | |
{ | |
/** | |
* Handle the event. | |
* | |
* @param RequestCompleted $event | |
* | |
* @return void | |
*/ | |
public function handle(RequestCompleted $event) | |
{ | |
logger()->info('request_log', $event->requestLogData); | |
} | |
} |
This file contains 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 | |
namespace App\Http\Middleware; | |
use Closure; | |
use App\Facades\RequestLog; | |
use Illuminate\Support\Carbon; | |
use App\Events\RequestCompleted; | |
class RequestLogMiddleware | |
{ | |
public function handle($request, Closure $next) | |
{ | |
RequestLog::set([ | |
'url' => $request->path(), | |
'method' => $request->method(), | |
'agent' => $request->header('user-agent'), | |
'api_action' => $request->route()->apiAction, | |
'terminal_id' => optional($request->terminal)->uuid, | |
'terminal_serial_number' => optional($request->terminal)->serial_number, | |
'username' => auth()->user()->username, | |
'user_id' => auth()->user()->uuid, | |
]); | |
return $next($request); | |
} | |
public function terminate($request, $response) | |
{ | |
RequestLog::set([ | |
'request' => $request->getContent(), | |
'response' => $response->getContent(), | |
'status_code' => $response->getStatusCode(), | |
'total_request_time' => (int) ($this->requestTime() * 1000), | |
'created_at' => Carbon::now()->toDateTimeString(), | |
]); | |
if (config('passportconnect.log_requests')) { | |
event(new RequestCompleted(RequestLog::all())); | |
} | |
} | |
private function requestTime() | |
{ | |
if (! defined('LARAVEL_START')) { | |
return null; | |
} | |
return (microtime(true) - LARAVEL_START); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment