Skip to content

Commit be4da38

Browse files
authored
Added listener FlushLogContextListener for the logger context flushing (#51)
1 parent cd41168 commit be4da38

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs: # Docs: <https://help.github.com/en/articles/workflow-syntax-for-github-ac
2424
matrix:
2525
setup: ['basic', 'lowest']
2626
php: ['7.4', '8.0']
27-
rr: ['2.2.1'] # Releases: <https://github.com/spiral/roadrunner-binary/releases>
27+
rr: ['2.3.1'] # Releases: <https://github.com/spiral/roadrunner-binary/releases>
2828
coverage: ['true']
2929
include:
3030
- php: '7.4'

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## UNRELEASED
8+
9+
### Added
10+
11+
- Listener `FlushLogContextListener` for the logger context flushing
12+
713
## v5.0.2
814

915
### Fixed

src/Defaults.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public static function afterLoopIteration(): array
8383
{
8484
return [
8585
Listeners\FlushDumperStackListener::class,
86+
Listeners\FlushLogContextListener::class,
8687
Listeners\FlushArrayCacheListener::class,
8788
Listeners\ResetDatabaseRecordModificationStateListener::class,
8889
Listeners\ClearInstancesListener::class,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Listeners;
6+
7+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
8+
9+
/**
10+
* @link https://github.com/laravel/octane/blob/1.x/src/Listeners/FlushLogContext.php
11+
*/
12+
class FlushLogContextListener implements ListenerInterface
13+
{
14+
use Traits\InvokerTrait;
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function handle($event): void
20+
{
21+
if ($event instanceof WithApplication) {
22+
$app = $event->application();
23+
24+
if (!$app->resolved($log_abstract = 'log')) {
25+
return;
26+
}
27+
28+
/** @var \Illuminate\Log\LogManager $log_manager */
29+
$log_manager = $app->make($log_abstract);
30+
31+
/** @var \Psr\Log\LoggerInterface|\Illuminate\Log\Logger $logger */
32+
$logger = $log_manager->driver();
33+
34+
/**
35+
* Method `withoutContext` for the Logger available since Laravel v8.49.0.
36+
*
37+
* @link https://github.com/illuminate/log/blob/v8.49.0/Logger.php#L202-L212 Source code (v8.49.0)
38+
* @see \Illuminate\Log\Logger::withoutContext
39+
*/
40+
$this->invokeMethod($logger, 'withoutContext');
41+
}
42+
}
43+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;
6+
7+
use Mockery as m;
8+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
9+
use Spiral\RoadRunnerLaravel\Listeners\FlushLogContextListener;
10+
11+
/**
12+
* @covers \Spiral\RoadRunnerLaravel\Listeners\FlushLogContextListener
13+
*/
14+
class FlushLogContextListenerTest extends AbstractListenerTestCase
15+
{
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function testHandle(): void
20+
{
21+
/** @var \Illuminate\Log\LogManager $log_manager */
22+
$log_manager = $this->app->make('log');
23+
24+
$logger = $log_manager->driver();
25+
26+
if (! \method_exists($logger, 'withoutContext')) {
27+
$this->markTestSkipped('Current illuminate/log package version does now supports ::withoutContext()');
28+
}
29+
30+
$this->app->instance('log', m::mock($log_manager)
31+
->makePartial()
32+
->expects('driver')
33+
->withNoArgs()
34+
->andReturn(
35+
m::mock($logger)
36+
->makePartial()
37+
->expects('withoutContext')
38+
->withNoArgs()
39+
->andReturn($logger)
40+
->getMock()
41+
)
42+
->getMock());
43+
44+
/** @var m\MockInterface|WithApplication $event_mock */
45+
$event_mock = m::mock(WithApplication::class)
46+
->makePartial()
47+
->expects('application')
48+
->andReturn($this->app)
49+
->getMock();
50+
51+
$this->listenerFactory()->handle($event_mock);
52+
}
53+
54+
/**
55+
* @return FlushLogContextListener
56+
*/
57+
protected function listenerFactory(): FlushLogContextListener
58+
{
59+
return new FlushLogContextListener();
60+
}
61+
}

0 commit comments

Comments
 (0)