Skip to content

Commit b919041

Browse files
authored
Merge pull request #25 from aivis/feature/ignored_logs
add ignored_logs config property
2 parents 349d35e + 0e5d406 commit b919041

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

src/Understand/UnderstandLaravel5/UnderstandLaravel5ServiceProvider.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ protected function listenQueryEvents()
292292
*/
293293
protected function handleEvent($level, $message, $context)
294294
{
295+
if ($this->shouldIgnoreEvent($level, $message, $context))
296+
{
297+
return;
298+
}
299+
295300
// `\Log::info`, `\Log::debug` and NOT `\Exception` or `\Throwable`
296301
if (in_array($level, ['info', 'debug']) && ! ($message instanceof Exception || $message instanceof Throwable))
297302
{
@@ -311,6 +316,24 @@ protected function handleEvent($level, $message, $context)
311316
}
312317
}
313318

319+
/**
320+
* @param $level
321+
* @param $message
322+
* @param $context
323+
* @return bool
324+
*/
325+
protected function shouldIgnoreEvent($level, $message, $context)
326+
{
327+
$ignoredEventTypes = (array)$this->app['config']->get('understand-laravel.ignored_logs');
328+
329+
if ( ! $ignoredEventTypes)
330+
{
331+
return false;
332+
}
333+
334+
return in_array($level, $ignoredEventTypes, true);
335+
}
336+
314337
/**
315338
* Get the services provided by the provider.
316339
*

src/config/understand-laravel.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,21 @@
3333
/**
3434
* SSL CA Bundle location
3535
*/
36-
'ssl_ca_bundle' => base_path('vendor/understand/understand-laravel5/src/ca_bundle.crt')
36+
'ssl_ca_bundle' => base_path('vendor/understand/understand-laravel5/src/ca_bundle.crt'),
37+
38+
/**
39+
* The log types that should not be sent to Understand.io.
40+
*
41+
* By default, send everything.
42+
*/
43+
'ignored_logs' => [
44+
//'debug',
45+
//'info',
46+
//'notice',
47+
//'warning',
48+
//'error',
49+
//'critical',
50+
//'alert',
51+
//'emergency',
52+
],
3753
];

tests/EventListenerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@ public function testEventListener()
4848
$this->assertSame($called, 1);
4949
$this->assertTrue($messageSame);
5050
}
51+
52+
/**
53+
* Test event listener
54+
*
55+
* @return void
56+
*/
57+
public function testIgnoredLogsConfig()
58+
{
59+
$called = 0;
60+
61+
$callback = function($data) use(&$called)
62+
{
63+
$called++;
64+
};
65+
66+
$this->app['config']->set('understand-laravel.ignored_logs', ['debug', 'notice']);
67+
68+
$handler = new CallbackHandler($callback);
69+
$this->app['understand.logger'] = new Logger($this->app['understand.fieldProvider'], $handler, false);
70+
71+
// debug and notice should be ignored
72+
$this->app['Psr\Log\LoggerInterface']->debug('test');
73+
$this->app['Psr\Log\LoggerInterface']->notice('test');
74+
75+
// error and alert should reach the logger
76+
$this->app['Psr\Log\LoggerInterface']->error('test');
77+
$this->app['Psr\Log\LoggerInterface']->alert('test');
78+
79+
$this->assertSame($called, 2);
80+
}
5181

5282
/**
5383
* Test error handler logging

0 commit comments

Comments
 (0)