Skip to content
This repository was archived by the owner on Jan 11, 2022. It is now read-only.

Commit 40c704c

Browse files
author
Wesley Elfring
committed
Merge remote-tracking branch 'origin/develop'
2 parents 22e44b5 + d33346e commit 40c704c

13 files changed

+318
-196
lines changed

README.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,26 @@
1212
## Logging exceptions
1313
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.
1414

15+
## Message Processors
16+
Processors add extra functionality to the handler. You can register processors adding this to your AppServiceProvider:
17+
```php
18+
Graylog2::registerProcessor(new \Swis\Graylog2\Processor\ExceptionProcessor());
19+
```
20+
In the default package, the following processors are available:
21+
### ExceptionProcessor
22+
Adds exception data to the message if there is any.
23+
### RequestProcessor
24+
Adds the current Laravel Request to the message. It adds the url, method and ip by default.
25+
1526
### Don't report exceptions
1627
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](https://laravel.com/docs/5.4/errors#the-exception-handler) about errors for more information.
1728

18-
### Send default additional data
19-
Using the config, you can specify additional key => value data to be sent with the GELF message. For example, you can use this to add a client ID to a message.
20-
2129
## Logging arbitrary data
2230
You can instantiate the Graylog2 class to send additional GELF messages:
2331

24-
```
25-
$graylog2 = app('graylog2');
26-
27-
32+
```php
2833
// Send default log message
29-
$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']);
34+
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']);
3035

3136

3237
// Send custom GELF Message
@@ -36,7 +41,7 @@ $message->setShortMessage('Fire! Fire! Help me!');
3641
$message->setFullMessage('Dear Sir/Madam, Fire! Fire! Help me!. 123 Cavendon Road. Looking forward to hearing from you. Yours truly, Maurice Moss.');
3742
$message->setFacility('ICT');
3843
$message->setAdditional('employee', 'Maurice Moss');
39-
$graylog2->logMessage($message);
44+
Graylog2::logMessage($message);
4045
```
4146

4247
## Troubleshooting

config/graylog2.php

+11-16
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@
55
'enabled' => true,
66

77
// Log HTTP requests with exceptions
8-
'log-requests' => true,
8+
'log_requests' => true,
9+
10+
// Log HTTP Request GET data
11+
'log_request_get_data' => false,
12+
13+
// Log HTTP Request POST data
14+
'log_request_post_data' => false,
15+
16+
// Filter out some sensitive post parameters
17+
'disallowed_post_parameters' => ['password', 'username'],
918

1019
/*
1120
* Also add exception data in the full message.
1221
* This increases the size of the message by a lot. The
1322
* exception information is also included in the 'exception'
1423
* field.
1524
*/
16-
'exception-in-full-message' => false,
25+
'stack_trace_in_full_message' => false,
1726

1827
'connection' => [
1928
'host' => '127.0.0.1',
@@ -22,19 +31,5 @@
2231

2332
// Set to UdpTransport::CHUNK_SIZE_LAN as a default
2433
'chunk_size' => '8154',
25-
],
26-
27-
/*
28-
* Add additional context to the GELF message
29-
*/
30-
'context' => [
31-
'host' => 'localhost',
32-
'version' => 'v1',
33-
'facility' => 'default-facility',
34-
],
35-
36-
// Allows you to set additional fields in the GELF message, use key => value
37-
'additional-fields' => [
38-
3934
]
4035
];

src/Facades/Graylog2.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Swis\Graylog2\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Graylog2 extends Facade
8+
{
9+
protected static function getFacadeAccessor()
10+
{
11+
return 'graylog2';
12+
}
13+
}

src/Graylog2.php

+91-47
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Gelf\Transport\TcpTransport;
88
use Gelf\Transport\TransportInterface;
99
use Gelf\Transport\UdpTransport;
10-
use Illuminate\Http\Request;
1110
use Psr\Log\AbstractLogger;
11+
use Swis\Graylog2\Processor\ProcessorInterface;
1212

1313
class Graylog2 extends AbstractLogger
1414
{
@@ -18,31 +18,53 @@ class Graylog2 extends AbstractLogger
1818
/** @var Publisher */
1919
protected $publisher;
2020

21-
protected $context = [];
21+
/** @var ProcessorInterface[] */
22+
protected $processors = [];
2223

2324
public function __construct()
2425
{
25-
// Setup context for later use
26-
$this->context = config('graylog2.context');
26+
$this->setupGraylogTransport();
27+
}
2728

28-
// Setup transport
29-
if (config('graylog2.connection.type') === 'UDP') {
30-
$transport = new UdpTransport(
31-
config('graylog2.connection.host'),
32-
config('graylog2.connection.port'),
33-
config('graylog2.connection.chunk_size')
34-
);
35-
} else {
36-
$transport = new TcpTransport(
37-
config('graylog2.connection.host'),
38-
config('graylog2.connection.port')
39-
);
29+
/**
30+
* Logs with an arbitrary level.
31+
*
32+
* @param mixed $level
33+
* @param string $message
34+
* @param array $context
35+
*/
36+
public function log($level, $message, array $context = [])
37+
{
38+
$gelfMessage = $this->logger->prepareLog($level, $message, $context);
39+
$exception = null;
40+
if (array_key_exists('exception', $context)) {
41+
$exception = $context['exception'];
4042
}
4143

42-
// Setup publisher and logger
43-
$this->publisher = new Publisher();
44-
$this->publisher->addTransport($transport);
45-
$this->logger = new Logger($this->publisher, $this->context['facility']);
44+
$gelfMessage = $this->invokeProcessors($gelfMessage, $exception, $context);
45+
$this->logger->publishMessage($gelfMessage);
46+
}
47+
48+
/**
49+
* @param \Exception $exception
50+
*/
51+
public function logException(\Exception $exception)
52+
{
53+
// Set short-message as it is a requirement
54+
$message = new Message();
55+
$message->setShortMessage(substr($exception->getMessage(), 0, 100));
56+
57+
$message = $this->invokeProcessors($message, $exception);
58+
$this->logger->publishMessage($message);
59+
}
60+
61+
/**
62+
* @param Message $message
63+
*/
64+
public function logGelfMessage(Message $message)
65+
{
66+
$message = $this->invokeProcessors($message);
67+
$this->logger->publishMessage($message);
4668
}
4769

4870
/**
@@ -56,44 +78,66 @@ public function addTransportToPublisher(TransportInterface $transport)
5678
}
5779

5880
/**
59-
* Send a message to Graylog.
81+
* Set's the default facility on the logger/transport.
6082
*
61-
* @param string $level
62-
* @param string $message
63-
* @param array $context
83+
* @param $facility
6484
*/
65-
public function log($level, $message, array $context = [])
85+
public function setFacility($facility)
6686
{
67-
$message = $this->logger->prepareLog($level, $message, $context);
68-
69-
if (!empty($this->context['host'])) {
70-
$message->setHost($this->context['host']);
71-
}
87+
$this->logger->setFacility($facility);
88+
}
7289

73-
if (!empty($context['request']) && $context['request'] instanceof Request) {
74-
$message
75-
->setAdditional('request_url', $context['request']->url())
76-
->setAdditional('request_method', $context['request']->method())
77-
->setAdditional('request_ip', $context['request']->ip());
78-
}
90+
/**
91+
* Allows you to refine the message before sending it to Graylog.
92+
* You could add a callback to add the current user or other
93+
* runtime info to the message.
94+
*
95+
* @param callable $callback
96+
* @param ProcessorInterface $processor
97+
*/
98+
public function registerProcessor(ProcessorInterface $processor)
99+
{
100+
$this->processors[] = $processor;
101+
}
79102

80-
// Add additionals from config
81-
if (!empty(config('graylog2.additional-fields'))) {
82-
foreach (config('graylog2.additional-fields') as $key => $value) {
83-
$message->setAdditional($key, $value);
84-
}
103+
/**
104+
* @param Message $message
105+
* @param null $exception
106+
* @param mixed $context
107+
*
108+
* @return Message
109+
*/
110+
private function invokeProcessors(Message $message, $exception = null, $context = [])
111+
{
112+
foreach ($this->processors as $processor) {
113+
$message = $processor->process($message, $exception, $context);
85114
}
86115

87-
$this->logger->publishMessage($message);
116+
return $message;
88117
}
89118

90119
/**
91-
* Log an already constructed GELF message.
92-
*
93-
* @param Message $message
120+
* Setup Graylog transport.
94121
*/
95-
public function logMessage(Message $message)
122+
private function setupGraylogTransport()
96123
{
97-
$this->logger->publishMessage($message);
124+
// Setup the transport
125+
if (config('graylog2.connection.type') === 'UDP') {
126+
$transport = new UdpTransport(
127+
config('graylog2.connection.host'),
128+
config('graylog2.connection.port'),
129+
config('graylog2.connection.chunk_size')
130+
);
131+
} else {
132+
$transport = new TcpTransport(
133+
config('graylog2.connection.host'),
134+
config('graylog2.connection.port')
135+
);
136+
}
137+
138+
// Setup publisher and logger
139+
$this->publisher = new Publisher();
140+
$this->publisher->addTransport($transport);
141+
$this->logger = new Logger($this->publisher);
98142
}
99143
}

src/Graylog2Handler.php

+10-17
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,22 @@ public function handle(array $record)
1717
return false;
1818
}
1919

20-
try {
21-
/** @var Graylog2 $graylog2 */
22-
$graylog2 = app('graylog2');
23-
24-
$context = array_merge(
25-
$record['context'],
26-
[
27-
'extra' => $record['extra'],
28-
'channel' => $record['channel'],
29-
]
30-
);
31-
32-
if (config('graylog2.log-requests')) {
33-
$context['request'] = app('Illuminate\Http\Request');
34-
}
20+
// Handle a log from Laravel
21+
/** @var Graylog2 $graylog2 */
22+
$graylog2 = app('graylog2');
3523

24+
try {
3625
$graylog2->log(
3726
strtolower($record['level_name']),
3827
$record['message'],
39-
$context
28+
$record['context']
4029
);
30+
31+
return true;
4132
} catch (\Exception $e) {
42-
Log::error('Cannot log the error to Graylog2.');
33+
Log::info('Could not log to Graylog.');
34+
35+
return false;
4336
}
4437
}
4538
}

src/Graylog2ServiceProvider.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ public function boot()
2323
*/
2424
public function register()
2525
{
26-
$this->app->bind('graylog2', function ($app) {
27-
return new Graylog2();
28-
});
26+
$this->app->singleton('graylog2', Graylog2::class);
2927

3028
// Register handler
3129
$monoLog = Log::getMonolog();
3230
$monoLog->pushHandler(new Graylog2Handler());
3331
}
32+
33+
/**
34+
* Get the services provided by the provider.
35+
*
36+
* @return array
37+
*/
38+
public function provides()
39+
{
40+
return ['graylog2'];
41+
}
3442
}

0 commit comments

Comments
 (0)