Skip to content

Commit f53be1f

Browse files
wimulkemanWim Ulkeman
andauthored
chore (logging): add stop_buffering config option (#44071)
Adding the stop_buffering config option. The FingersCrossedHandler Logger will first start buffering log records until a record exceeds the configured action_level. When the action_level is exceeded the buffered records will be flushed to the provided log location. By default the FingersCrossedHandler will stop buffering log records after its first flush (due to the $stopBuffering property). All records pushed to the log handler afterwards will be flushed directly to the log location. With this config update the log handler can be configured to resume the buffering after a flush occurs. This can be useful for applications that rely heavily on gather debug log records which only are useful when an error logging is passed. In the old setup all the debug records after an error record would be flushed to the log location. @see https://github.com/Seldaek/monolog/blob/main/src/Monolog/Handler/FingersCrossedHandler.php#L73 Co-authored-by: Wim Ulkeman <[email protected]>
1 parent 585da09 commit f53be1f

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/Illuminate/Log/LogManager.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,13 @@ protected function prepareHandlers(array $handlers)
417417
protected function prepareHandler(HandlerInterface $handler, array $config = [])
418418
{
419419
if (isset($config['action_level'])) {
420-
$handler = new FingersCrossedHandler($handler, $this->actionLevel($config));
420+
$handler = new FingersCrossedHandler(
421+
$handler,
422+
$this->actionLevel($config),
423+
0,
424+
true,
425+
$config['stop_buffering'] ?? true
426+
);
421427
}
422428

423429
if (! $handler instanceof FormattableHandlerInterface) {

tests/Log/LogManagerTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,67 @@ public function testWrappingHandlerInFingersCrossedWhenActionLevelIsUsed()
496496
$this->assertEquals(Monolog::DEBUG, $expectedStreamHandler->getLevel());
497497
}
498498

499+
public function testFingersCrossedHandlerStopsRecordBufferingAfterFirstFlushByDefault()
500+
{
501+
$config = $this->app['config'];
502+
503+
$config->set('logging.channels.fingerscrossed', [
504+
'driver' => 'monolog',
505+
'handler' => StreamHandler::class,
506+
'level' => 'debug',
507+
'action_level' => 'critical',
508+
'with' => [
509+
'stream' => 'php://stderr',
510+
'bubble' => false,
511+
],
512+
]);
513+
514+
$manager = new LogManager($this->app);
515+
516+
// create logger with handler specified from configuration
517+
$logger = $manager->channel('fingerscrossed');
518+
$handlers = $logger->getLogger()->getHandlers();
519+
520+
$expectedFingersCrossedHandler = $handlers[0];
521+
522+
$stopBufferingProp = new ReflectionProperty(get_class($expectedFingersCrossedHandler), 'stopBuffering');
523+
$stopBufferingProp->setAccessible(true);
524+
$stopBufferingValue = $stopBufferingProp->getValue($expectedFingersCrossedHandler);
525+
526+
$this->assertTrue($stopBufferingValue);
527+
}
528+
529+
public function testFingersCrossedHandlerCanBeConfiguredToResumeBufferingAfterFlushing()
530+
{
531+
$config = $this->app['config'];
532+
533+
$config->set('logging.channels.fingerscrossed', [
534+
'driver' => 'monolog',
535+
'handler' => StreamHandler::class,
536+
'level' => 'debug',
537+
'action_level' => 'critical',
538+
'stop_buffering' => false,
539+
'with' => [
540+
'stream' => 'php://stderr',
541+
'bubble' => false,
542+
],
543+
]);
544+
545+
$manager = new LogManager($this->app);
546+
547+
// create logger with handler specified from configuration
548+
$logger = $manager->channel('fingerscrossed');
549+
$handlers = $logger->getLogger()->getHandlers();
550+
551+
$expectedFingersCrossedHandler = $handlers[0];
552+
553+
$stopBufferingProp = new ReflectionProperty(get_class($expectedFingersCrossedHandler), 'stopBuffering');
554+
$stopBufferingProp->setAccessible(true);
555+
$stopBufferingValue = $stopBufferingProp->getValue($expectedFingersCrossedHandler);
556+
557+
$this->assertFalse($stopBufferingValue);
558+
}
559+
499560
public function testItSharesContextWithAlreadyResolvedChannels()
500561
{
501562
$manager = new LogManager($this->app);

0 commit comments

Comments
 (0)