From de64d9958829beee13792340c249f910aac4df6e Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Fri, 3 Mar 2023 17:01:33 +0100 Subject: [PATCH] add processors to logging (placeholders) --- src/Illuminate/Log/LogManager.php | 24 ++++++++++++---- tests/Log/LogManagerTest.php | 46 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Log/LogManager.php b/src/Illuminate/Log/LogManager.php index bf6f1d969d83..47d01a37e6d2 100644 --- a/src/Illuminate/Log/LogManager.php +++ b/src/Illuminate/Log/LogManager.php @@ -16,6 +16,8 @@ use Monolog\Handler\SyslogHandler; use Monolog\Handler\WhatFailureGroupHandler; use Monolog\Logger as Monolog; +use Monolog\Processor\ProcessorInterface; +use Monolog\Processor\PsrLogMessageProcessor; use Psr\Log\LoggerInterface; use Throwable; @@ -293,7 +295,7 @@ protected function createSingleDriver(array $config) $config['bubble'] ?? true, $config['permission'] ?? null, $config['locking'] ?? false ), $config ), - ]); + ], $config['with_placeholders'] ?? true ? [new PsrLogMessageProcessor()] : []); } /** @@ -309,7 +311,7 @@ protected function createDailyDriver(array $config) $config['path'], $config['days'] ?? 7, $this->level($config), $config['bubble'] ?? true, $config['permission'] ?? null, $config['locking'] ?? false ), $config), - ]); + ], $config['with_placeholders'] ?? true ? [new PsrLogMessageProcessor()] : []); } /** @@ -333,7 +335,7 @@ protected function createSlackDriver(array $config) $config['bubble'] ?? true, $config['exclude_fields'] ?? [] ), $config), - ]); + ], $config['with_placeholders'] ?? true ? [new PsrLogMessageProcessor()] : []); } /** @@ -349,7 +351,7 @@ protected function createSyslogDriver(array $config) Str::snake($this->app['config']['app.name'], '-'), $config['facility'] ?? LOG_USER, $this->level($config) ), $config), - ]); + ], $config['with_placeholders'] ?? true ? [new PsrLogMessageProcessor()] : []); } /** @@ -364,7 +366,7 @@ protected function createErrorlogDriver(array $config) $this->prepareHandler(new ErrorLogHandler( $config['type'] ?? ErrorLogHandler::OPERATING_SYSTEM, $this->level($config) )), - ]); + ], $config['with_placeholders'] ?? true ? [new PsrLogMessageProcessor()] : []); } /** @@ -383,6 +385,14 @@ protected function createMonologDriver(array $config) $config['handler'].' must be an instance of '.HandlerInterface::class ); } + collect($config['processors'] ?? [])->each(function ($processor) { + $processor = $processor['processor'] ?? $processor; + if (! is_a($processor, ProcessorInterface::class, true)) { + throw new InvalidArgumentException( + $processor.' must be an instance of '.ProcessorInterface::class + ); + } + }); $with = array_merge( ['level' => $this->level($config)], @@ -392,7 +402,9 @@ protected function createMonologDriver(array $config) return new Monolog($this->parseChannel($config), [$this->prepareHandler( $this->app->make($config['handler'], $with), $config - )]); + )], collect($config['processors'] ?? [])->map(fn ($processor) => + $this->app->make($processor['processor'] ?? $processor, $processor['with'] ?? []) + )->toArray()); } /** diff --git a/tests/Log/LogManagerTest.php b/tests/Log/LogManagerTest.php index b394b6bebbc2..47c963726dc2 100755 --- a/tests/Log/LogManagerTest.php +++ b/tests/Log/LogManagerTest.php @@ -15,6 +15,8 @@ use Monolog\Handler\SyslogHandler; use Monolog\Level; use Monolog\Logger as Monolog; +use Monolog\Processor\MemoryUsageProcessor; +use Monolog\Processor\PsrLogMessageProcessor; use Monolog\Processor\UidProcessor; use Orchestra\Testbench\TestCase; use ReflectionProperty; @@ -63,6 +65,7 @@ public function testStackChannel() 'stream' => 'php://stderr', 'bubble' => false, ], + 'processors' => [PsrLogMessageProcessor::class], ]); $config->set('logging.channels.stdout', [ @@ -84,6 +87,7 @@ public function testStackChannel() $this->assertInstanceOf(Logger::class, $logger); $this->assertCount(2, $handlers); $this->assertInstanceOf(StreamHandler::class, $handlers[0]); + $this->assertInstanceOf(PsrLogMessageProcessor::class, $logger->getLogger()->getProcessors()[0]); $this->assertInstanceOf(StreamHandler::class, $handlers[1]); $this->assertEquals(Level::Notice, $handlers[0]->getLevel()); $this->assertEquals(Level::Info, $handlers[1]->getLevel()); @@ -211,6 +215,39 @@ public function testLogManagerCreatesMonologHandlerWithProperFormatter() $this->assertInstanceOf(NullHandler::class, $handler); } + public function testLogManagerCreatesMonologHandlerWithProcessors() + { + $config = $this->app->make('config'); + $config->set('logging.channels.memory', [ + 'driver' => 'monolog', + 'name' => 'memory', + 'handler' => StreamHandler::class, + 'with' => [ + 'stream' => 'php://stderr', + ], + 'processors' => [ + MemoryUsageProcessor::class, + ['processor' => PsrLogMessageProcessor::class, 'with' => ['removeUsedContextFields' => true]], + ], + ]); + + $manager = new LogManager($this->app); + + // create logger with handler specified from configuration + $logger = $manager->channel('memory'); + $handler = $logger->getLogger()->getHandlers()[0]; + $processors = $logger->getLogger()->getProcessors(); + + $this->assertInstanceOf(StreamHandler::class, $handler); + $this->assertInstanceOf(MemoryUsageProcessor::class, $processors[0]); + $this->assertInstanceOf(PsrLogMessageProcessor::class, $processors[1]); + + $removeUsedContextFields = new ReflectionProperty(get_class($processors[1]), 'removeUsedContextFields'); + $removeUsedContextFields->setAccessible(true); + + $this->assertTrue($removeUsedContextFields->getValue($processors[1])); + } + public function testItUtilisesTheNullDriverDuringTestsWhenNullDriverUsed() { $config = $this->app->make('config'); @@ -264,6 +301,7 @@ public function testLogManagerCreateSingleDriverWithConfiguredFormatter() $this->assertInstanceOf(StreamHandler::class, $handler); $this->assertInstanceOf(LineFormatter::class, $formatter); + $this->assertInstanceOf(PsrLogMessageProcessor::class, $logger->getLogger()->getProcessors()[0]); $config->set('logging.channels.formattedsingle', [ 'driver' => 'single', @@ -273,6 +311,7 @@ public function testLogManagerCreateSingleDriverWithConfiguredFormatter() 'formatter_with' => [ 'dateFormat' => 'Y/m/d--test', ], + 'with_placeholders' => false, ]); $logger = $manager->channel('formattedsingle'); @@ -281,6 +320,7 @@ public function testLogManagerCreateSingleDriverWithConfiguredFormatter() $this->assertInstanceOf(StreamHandler::class, $handler); $this->assertInstanceOf(HtmlFormatter::class, $formatter); + $this->assertEmpty($logger->getLogger()->getProcessors()); $dateFormat = new ReflectionProperty(get_class($formatter), 'dateFormat'); $dateFormat->setAccessible(true); @@ -306,6 +346,7 @@ public function testLogManagerCreateDailyDriverWithConfiguredFormatter() $this->assertInstanceOf(StreamHandler::class, $handler); $this->assertInstanceOf(LineFormatter::class, $formatter); + $this->assertInstanceOf(PsrLogMessageProcessor::class, $logger->getLogger()->getProcessors()[0]); $config->set('logging.channels.formatteddaily', [ 'driver' => 'daily', @@ -315,6 +356,7 @@ public function testLogManagerCreateDailyDriverWithConfiguredFormatter() 'formatter_with' => [ 'dateFormat' => 'Y/m/d--test', ], + 'with_placeholders' => false, ]); $logger = $manager->channel('formatteddaily'); @@ -323,6 +365,7 @@ public function testLogManagerCreateDailyDriverWithConfiguredFormatter() $this->assertInstanceOf(StreamHandler::class, $handler); $this->assertInstanceOf(HtmlFormatter::class, $formatter); + $this->assertEmpty($logger->getLogger()->getProcessors()); $dateFormat = new ReflectionProperty(get_class($formatter), 'dateFormat'); $dateFormat->setAccessible(true); @@ -347,6 +390,7 @@ public function testLogManagerCreateSyslogDriverWithConfiguredFormatter() $this->assertInstanceOf(SyslogHandler::class, $handler); $this->assertInstanceOf(LineFormatter::class, $formatter); + $this->assertInstanceOf(PsrLogMessageProcessor::class, $logger->getLogger()->getProcessors()[0]); $config->set('logging.channels.formattedsyslog', [ 'driver' => 'syslog', @@ -355,6 +399,7 @@ public function testLogManagerCreateSyslogDriverWithConfiguredFormatter() 'formatter_with' => [ 'dateFormat' => 'Y/m/d--test', ], + 'with_placeholders' => false, ]); $logger = $manager->channel('formattedsyslog'); @@ -363,6 +408,7 @@ public function testLogManagerCreateSyslogDriverWithConfiguredFormatter() $this->assertInstanceOf(SyslogHandler::class, $handler); $this->assertInstanceOf(HtmlFormatter::class, $formatter); + $this->assertEmpty($logger->getLogger()->getProcessors()); $dateFormat = new ReflectionProperty(get_class($formatter), 'dateFormat'); $dateFormat->setAccessible(true);