🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨
We don't use this package anymore in our own projects and cannot justify the time needed to maintain it anymore. That's why we have chosen to abandon it. Feel free to fork our code and maintain your own copy or use one of the many alternatives.
- Run composer require for this package:
composer require swisnl/laravel-graylog2
- Add the service provider to app.php if you don't like auto discovery:
Swis\Graylog2\Graylog2ServiceProvider
- Run
php artisan vendor:publish
to publish the config file to ./config/graylog2.php. - Configure it to your liking
- Done!
The default settings enable logging of exceptions. It will add the HTTP request to the GELF message, but it will not add POST values. Check the graylog2.log-requests config to enable or disable this behavior.
You can adjust the minimum log level by changing the value log_level
in configuration.
Processors add extra functionality to the handler. You can register processors by modifying the AppServiceProvider:
public function register()
{
//...
Graylog2::registerProcessor(new \Swis\Graylog2\Processor\ExceptionProcessor());
Graylog2::registerProcessor(new \Swis\Graylog2\Processor\RequestProcessor());
Graylog2::registerProcessor(new MyCustomProcessor());
//...
}
The following processors are available by default:
ExceptionProcessor
Adds exception data to the message if there is any.
RequestProcessor
Adds the current Laravel Request to the message. It adds the url, method and ip by default.
You can define a custom processor by implementing Swis\Graylog2\Processor\ProcessorInterface
. The result should look something like this:
<?php
namespace App\Processors;
use Auth;
use Swis\Graylog2\Processor\ProcessorInterface;
class MyCustomProcessor implements ProcessorInterface
{
public function process($message, $exception, $context)
{
$message->setAdditional('domain', config('app.url'));
if (Auth::user()) {
$message->setAdditional('user_id', Auth::id());
}
return $message;
}
}
In app/Exceptions/Handler.php
you can define the $dontReport array with Exception classes that won't be reported to the logger. For example, you can blacklist the \Illuminate\Database\Eloquent\ModelNotFoundException. Check the Laravel Documentation about errors for more information.
You can instantiate the Graylog2 class to send additional GELF messages:
// Send default log message
Graylog2::log('emergency', 'Dear Sir/Madam, Fire! Fire! Help me!. 123 Cavendon Road. Looking forward to hearing from you. Yours truly, Maurice Moss.', ['facility' => 'ICT']);
// Send custom GELF Message
$message = new \Gelf\Message();
$message->setLevel('emergency');
$message->setShortMessage('Fire! Fire! Help me!');
$message->setFullMessage('Dear Sir/Madam, Fire! Fire! Help me!. 123 Cavendon Road. Looking forward to hearing from you. Yours truly, Maurice Moss.');
$message->setFacility('ICT');
$message->setAdditional('employee', 'Maurice Moss');
Graylog2::logMessage($message);
You might need to increase the size of the UDP chunks in the UDP Transport (see the config file). Otherwise, you can send packets in TCP mode.