Skip to content

Commit 0ce72d7

Browse files
committed
Add log filter
1 parent 53ad362 commit 0ce72d7

File tree

3 files changed

+142
-9
lines changed

3 files changed

+142
-9
lines changed

src/Understand/UnderstandLaravel5/UnderstandLaravel5ServiceProvider.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,22 +434,27 @@ protected function handleEvent($level, $message, $context)
434434
}
435435
}
436436

437-
/**
438-
* @param $level
439-
* @param $message
440-
* @param $context
441-
* @return bool
442-
*/
443437
protected function shouldIgnoreEvent($level, $message, $context)
444438
{
445439
$ignoredEventTypes = (array)$this->app['config']->get('understand-laravel.ignored_logs');
440+
$logFilter = $this->app['config']->get('understand-laravel.log_filter');
441+
442+
// check if the log should be ignored by its level (info, warning, etc.)
443+
if ($ignoredEventTypes)
444+
{
445+
return in_array($level, $ignoredEventTypes, true);
446+
}
446447

447-
if ( ! $ignoredEventTypes)
448+
// check if a custom filter is set and whether the log should be ignored
449+
// true - the log should be ignored
450+
// false - the log should be delivered to Understand
451+
if (is_callable($logFilter))
448452
{
449-
return false;
453+
return (bool)$logFilter($level, $message, $context);
450454
}
451455

452-
return in_array($level, $ignoredEventTypes, true);
456+
// by default logs are not ignored
457+
return false;
453458
}
454459

455460
/**

src/config/understand-laravel.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@
6666
//'emergency',
6767
],
6868

69+
/**
70+
* Log filter.
71+
*
72+
* The configuration value (filter) must be a callable type:
73+
* - https://www.php.net/manual/en/function.is-callable.php
74+
*
75+
* The suggested way would be to create an invokable class since it's hard to serialise anonymous functions (Laravel config cache):
76+
* - https://www.php.net/manual/en/language.oop5.magic.php#object.invoke
77+
*
78+
* The log (callable) filter interface is as follows: `$callable($level, $message, $context)`.
79+
*
80+
* The result of the filter must be a boolean value:
81+
* - TRUE, the log should be ignored and NOT delivered to Understand.io
82+
* - FALSE, the log should be delivered to Understand.io
83+
*
84+
* The `ignored_logs` config value has higher precedence than `log_filter`.
85+
*/
86+
'log_filter' => null,
87+
6988
/**
7089
* Field names which values should not be sent to Understand.io
7190
* It applies to POST and GET request parameters

tests/LogFilterTest.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
use Understand\UnderstandLaravel5\Logger;
4+
use Understand\UnderstandLaravel5\Handlers\CallbackHandler;
5+
use Understand\UnderstandLaravel5\UnderstandLaravel5ServiceProvider;
6+
7+
class LogFilterTest extends Orchestra\Testbench\TestCase
8+
{
9+
10+
/**
11+
* Setup service provider
12+
*
13+
* @param object $app
14+
* @return void
15+
*/
16+
protected function getPackageProviders($app)
17+
{
18+
return [UnderstandLaravel5ServiceProvider::class];
19+
}
20+
21+
/**
22+
* @return void
23+
*/
24+
public function testLogFilterAllowsDelivery()
25+
{
26+
$logsSent = 0;
27+
28+
$callback = function() use(&$logsSent)
29+
{
30+
$logsSent++;
31+
};
32+
33+
$this->app['config']->set('understand-laravel.log_filter', function() {
34+
// FALSE, logs should not be filtered
35+
return false;
36+
});
37+
38+
$handler = new CallbackHandler($callback);
39+
$this->app['understand.logger'] = new Logger($this->app['understand.fieldProvider'], $handler);
40+
41+
// trigger error
42+
$this->app['Psr\Log\LoggerInterface']->error('test');
43+
$this->app['Psr\Log\LoggerInterface']->warning('test2');
44+
45+
$this->assertEquals(2, $logsSent);
46+
}
47+
48+
/**
49+
* @return void
50+
*/
51+
public function testLogFilterFiltersOneLog()
52+
{
53+
$logsSent = 0;
54+
55+
$callback = function() use(&$logsSent)
56+
{
57+
$logsSent++;
58+
};
59+
60+
$this->app['config']->set('understand-laravel.log_filter', function($level, $message, $context) {
61+
if ($message === 'test2') {
62+
// TRUE, log should be filtered
63+
return true;
64+
}
65+
66+
// FALSE, logs should not be filtered
67+
return false;
68+
});
69+
70+
$handler = new CallbackHandler($callback);
71+
$this->app['understand.logger'] = new Logger($this->app['understand.fieldProvider'], $handler);
72+
73+
// trigger error
74+
$this->app['Psr\Log\LoggerInterface']->error('test');
75+
$this->app['Psr\Log\LoggerInterface']->warning('test2');
76+
77+
$this->assertEquals(1, $logsSent);
78+
}
79+
80+
/**
81+
* @return void
82+
*/
83+
public function testLogFilterReceivesAllData()
84+
{
85+
$logsSent = 0;
86+
87+
$callback = function() use(&$logsSent)
88+
{
89+
$logsSent++;
90+
};
91+
92+
$this->app['config']->set('understand-laravel.log_filter', function($level, $message, $context) {
93+
$this->assertEquals('error', $level);
94+
$this->assertEquals('test', $message);
95+
$this->assertEquals(['context' => 'value'], $context);
96+
97+
// FALSE, logs should not be filtered
98+
return false;
99+
});
100+
101+
$handler = new CallbackHandler($callback);
102+
$this->app['understand.logger'] = new Logger($this->app['understand.fieldProvider'], $handler);
103+
104+
// trigger error
105+
$this->app['Psr\Log\LoggerInterface']->error('test', ['context' => 'value']);
106+
107+
$this->assertEquals(1, $logsSent);
108+
}
109+
}

0 commit comments

Comments
 (0)