Skip to content

Commit ddeee72

Browse files
[12.x] Do not dispatch MessageLogged twice (#56713)
* failing test * passing test * Update Logger.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent f73517f commit ddeee72

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Illuminate/Log/Logger.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ public function listen(Closure $callback)
245245
*/
246246
protected function fireLogEvent($level, $message, array $context = [])
247247
{
248+
// Avoid dispatching the event multiple times if our logger instance is the LogManager...
249+
if ($this->logger instanceof LogManager &&
250+
$this->logger->getEventDispatcher() !== null) {
251+
return;
252+
}
253+
248254
// If the event dispatcher is set, we will pass along the parameters to the
249255
// log listeners. These are useful for building profilers or other tools
250256
// that aggregate all of the log messages for a given "request" cycle.

tests/Integration/Log/LoggingIntegrationTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Illuminate\Tests\Integration\Log;
44

5+
use Illuminate\Log\Events\MessageLogged;
6+
use Illuminate\Log\Logger;
7+
use Illuminate\Support\Facades\Event;
58
use Illuminate\Support\Facades\Log;
69
use Orchestra\Testbench\TestCase;
710

@@ -13,4 +16,13 @@ public function testLoggingCanBeRunWithoutEncounteringExceptions()
1316

1417
Log::info('Hello World');
1518
}
19+
20+
public function testCallingLoggerDirectlyDispatchesOneEvent()
21+
{
22+
Event::fake([MessageLogged::class]);
23+
24+
$this->app->make(Logger::class)->debug('my debug message');
25+
26+
Event::assertDispatchedTimes(MessageLogged::class, 1);
27+
}
1628
}

0 commit comments

Comments
 (0)