Skip to content

Commit c424192

Browse files
committed
add the service container resolver
1 parent 0ce72d7 commit c424192

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/Understand/UnderstandLaravel5/UnderstandLaravel5ServiceProvider.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,11 @@ protected function shouldIgnoreEvent($level, $message, $context)
448448
// check if a custom filter is set and whether the log should be ignored
449449
// true - the log should be ignored
450450
// false - the log should be delivered to Understand
451-
if (is_callable($logFilter))
451+
if ($logFilter)
452452
{
453-
return (bool)$logFilter($level, $message, $context);
453+
$factory = is_callable($logFilter) ? $logFilter : $this->app->make($logFilter);
454+
455+
return (bool)$factory($level, $message, $context);
454456
}
455457

456458
// by default logs are not ignored

src/config/understand-laravel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
*
7272
* The configuration value (filter) must be a callable type:
7373
* - https://www.php.net/manual/en/function.is-callable.php
74+
* or a callable dependency from the service container:
75+
* - https://laravel.com/docs/9.x/container#the-make-method
7476
*
7577
* The suggested way would be to create an invokable class since it's hard to serialise anonymous functions (Laravel config cache):
7678
* - https://www.php.net/manual/en/language.oop5.magic.php#object.invoke

tests/LogFilterTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,41 @@ public function testLogFilterAllowsDelivery()
4545
$this->assertEquals(2, $logsSent);
4646
}
4747

48+
/**
49+
* @return void
50+
*/
51+
public function testServiceContainerDependency()
52+
{
53+
$logsSent = 0;
54+
55+
$callback = function() use(&$logsSent)
56+
{
57+
$logsSent++;
58+
};
59+
60+
$dependencyName = 'service-container-dependency';
61+
$dependencyCalled = false;
62+
63+
$this->app->bind($dependencyName, function() use(&$dependencyCalled) {
64+
return function() use(&$dependencyCalled) {
65+
$dependencyCalled = true;
66+
// FALSE, logs should not be filtered
67+
return false;
68+
};
69+
});
70+
71+
$this->app['config']->set('understand-laravel.log_filter', $dependencyName);
72+
73+
$handler = new CallbackHandler($callback);
74+
$this->app['understand.logger'] = new Logger($this->app['understand.fieldProvider'], $handler);
75+
76+
// trigger error
77+
$this->app['Psr\Log\LoggerInterface']->error('test');
78+
79+
$this->assertTrue($dependencyCalled);
80+
$this->assertEquals(1, $logsSent);
81+
}
82+
4883
/**
4984
* @return void
5085
*/

0 commit comments

Comments
 (0)