Skip to content

Instantly share code, notes, and snippets.

@cviebrock
Created September 29, 2016 14:57
Show Gist options
  • Save cviebrock/882df789d874ee9e7e316222cc00364f to your computer and use it in GitHub Desktop.
Save cviebrock/882df789d874ee9e7e316222cc00364f to your computer and use it in GitHub Desktop.
Log Laravel to Elastic/Logstash
<?php namespace App\Providers;
use Elastica\Client;
use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\LogstashFormatter;
use Monolog\Handler\ElasticSearchHandler;
class ElasticLoggingProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$config = $this->app->make('config');
$monolog = $this->app->make('log')->getMonolog();
$client = new Client([
'servers' => [
[
'host' => 'localhost', // eventually pull this from config/env
'port' => 9200,
],
],
]);
$handler = new ElasticSearchHandler($client);
$handler->setFormatter(new LogstashFormatter($config->get('app.env')));
$monolog->pushHandler($handler);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment