From 5f67205da7394b6ca789fe6bc2910522c0b03772 Mon Sep 17 00:00:00 2001 From: Denis Mysenko Date: Wed, 11 Jan 2023 15:34:29 +1100 Subject: [PATCH 1/3] r Allow any number of arguments to LogManager --- src/Illuminate/Log/LogManager.php | 64 +++++++++++++++++-------------- tests/Log/LogManagerTest.php | 23 +++++++++++ 2 files changed, 59 insertions(+), 28 deletions(-) diff --git a/src/Illuminate/Log/LogManager.php b/src/Illuminate/Log/LogManager.php index f528bc58eb55..dc441dc6930e 100644 --- a/src/Illuminate/Log/LogManager.php +++ b/src/Illuminate/Log/LogManager.php @@ -22,7 +22,7 @@ /** * @mixin \Illuminate\Log\Logger */ -class LogManager implements LoggerInterface +class LogManager { use ParsesLogConfiguration; @@ -595,12 +595,11 @@ public function getChannels() * System is unusable. * * @param string $message - * @param array $context * @return void */ - public function emergency($message, array $context = []): void + public function emergency($message, ...$context): void { - $this->driver()->emergency($message, $context); + $this->driver()->emergency($message, $this->mergeContexts($context)); } /** @@ -610,12 +609,11 @@ public function emergency($message, array $context = []): void * trigger the SMS alerts and wake you up. * * @param string $message - * @param array $context * @return void */ - public function alert($message, array $context = []): void + public function alert($message, ...$context): void { - $this->driver()->alert($message, $context); + $this->driver()->alert($message, $this->mergeContexts($context)); } /** @@ -624,12 +622,11 @@ public function alert($message, array $context = []): void * Example: Application component unavailable, unexpected exception. * * @param string $message - * @param array $context * @return void */ - public function critical($message, array $context = []): void + public function critical($message, ...$context): void { - $this->driver()->critical($message, $context); + $this->driver()->critical($message, $this->mergeContexts($context)); } /** @@ -637,12 +634,11 @@ public function critical($message, array $context = []): void * be logged and monitored. * * @param string $message - * @param array $context * @return void */ - public function error($message, array $context = []): void + public function error($message, ...$context): void { - $this->driver()->error($message, $context); + $this->driver()->error($message, $this->mergeContexts($context)); } /** @@ -652,24 +648,22 @@ public function error($message, array $context = []): void * that are not necessarily wrong. * * @param string $message - * @param array $context * @return void */ - public function warning($message, array $context = []): void + public function warning($message, ...$context): void { - $this->driver()->warning($message, $context); + $this->driver()->warning($message, $this->mergeContexts($context)); } /** * Normal but significant events. * * @param string $message - * @param array $context * @return void */ - public function notice($message, array $context = []): void + public function notice($message, ...$context): void { - $this->driver()->notice($message, $context); + $this->driver()->notice($message, $this->mergeContexts($context)); } /** @@ -678,24 +672,22 @@ public function notice($message, array $context = []): void * Example: User logs in, SQL logs. * * @param string $message - * @param array $context * @return void */ - public function info($message, array $context = []): void + public function info($message, ...$context): void { - $this->driver()->info($message, $context); + $this->driver()->info($message, $this->mergeContexts($context)); } /** * Detailed debug information. * * @param string $message - * @param array $context * @return void */ - public function debug($message, array $context = []): void + public function debug($message, ...$context): void { - $this->driver()->debug($message, $context); + $this->driver()->debug($message, $this->mergeContexts($context)); } /** @@ -703,12 +695,11 @@ public function debug($message, array $context = []): void * * @param mixed $level * @param string $message - * @param array $context * @return void */ - public function log($level, $message, array $context = []): void + public function log($level, $message, ...$context): void { - $this->driver()->log($level, $message, $context); + $this->driver()->log($level, $message, $this->mergeContexts($context)); } /** @@ -722,4 +713,21 @@ public function __call($method, $parameters) { return $this->driver()->$method(...$parameters); } + + /** + * @param $userPassedContext + * @return array + */ + protected function mergeContexts($userPassedContext) + { + return array_reduce($userPassedContext, function($carry, $argument) { + if (is_array($argument)) { + $carry = array_merge($carry, $argument); + } else { + $carry[] = $argument; + } + + return $carry; + }, []); + } } diff --git a/tests/Log/LogManagerTest.php b/tests/Log/LogManagerTest.php index 7f93512840ae..7376516c017a 100755 --- a/tests/Log/LogManagerTest.php +++ b/tests/Log/LogManagerTest.php @@ -661,6 +661,29 @@ public function testFlushSharedContext() $this->assertEmpty($manager->sharedContext()); } + public function testContextCanIncludeAnyNumberOfArguments() + { + $manager = new LogManager($this->app); + + $stack = $manager->stack(['single']); + $stack->listen(function ($message) use (&$context) { + $context = $message->context; + }); + + $object = (object)[]; + + $manager->error('foo', 'text', 15, [ + 'key' => 'value' + ], $object); + + $this->assertSame([ + 'text', + 15, + 'key' => 'value', + $object + ], $context); + } + public function testLogManagerCreateCustomFormatterWithTap() { $config = $this->app['config']; From ec50db7c0ce0151aa5a4f7825d6cdf256fd19b86 Mon Sep 17 00:00:00 2001 From: Denis Mysenko Date: Wed, 11 Jan 2023 17:11:14 +1100 Subject: [PATCH 2/3] f Linting --- src/Illuminate/Log/LogManager.php | 2 +- tests/Log/LogManagerTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Log/LogManager.php b/src/Illuminate/Log/LogManager.php index dc441dc6930e..57bb3e8faabb 100644 --- a/src/Illuminate/Log/LogManager.php +++ b/src/Illuminate/Log/LogManager.php @@ -720,7 +720,7 @@ public function __call($method, $parameters) */ protected function mergeContexts($userPassedContext) { - return array_reduce($userPassedContext, function($carry, $argument) { + return array_reduce($userPassedContext, function ($carry, $argument) { if (is_array($argument)) { $carry = array_merge($carry, $argument); } else { diff --git a/tests/Log/LogManagerTest.php b/tests/Log/LogManagerTest.php index 7376516c017a..680db863b0a9 100755 --- a/tests/Log/LogManagerTest.php +++ b/tests/Log/LogManagerTest.php @@ -670,17 +670,17 @@ public function testContextCanIncludeAnyNumberOfArguments() $context = $message->context; }); - $object = (object)[]; + $object = (object) []; $manager->error('foo', 'text', 15, [ - 'key' => 'value' + 'key' => 'value', ], $object); $this->assertSame([ 'text', 15, 'key' => 'value', - $object + $object, ], $context); } From d72eefa0473539768df1560a14a47eae8f0b0daf Mon Sep 17 00:00:00 2001 From: Denis Mysenko Date: Wed, 11 Jan 2023 21:14:19 +1100 Subject: [PATCH 3/3] r Bring back the interface, use a different approach --- src/Illuminate/Log/LogManager.php | 49 ++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/Illuminate/Log/LogManager.php b/src/Illuminate/Log/LogManager.php index 57bb3e8faabb..0d9d4c9f5d83 100644 --- a/src/Illuminate/Log/LogManager.php +++ b/src/Illuminate/Log/LogManager.php @@ -22,7 +22,7 @@ /** * @mixin \Illuminate\Log\Logger */ -class LogManager +class LogManager implements LoggerInterface { use ParsesLogConfiguration; @@ -595,10 +595,13 @@ public function getChannels() * System is unusable. * * @param string $message + * @param array $context * @return void */ - public function emergency($message, ...$context): void + public function emergency($message, $context = []): void { + $context = array_slice(func_get_args(), 1); + $this->driver()->emergency($message, $this->mergeContexts($context)); } @@ -609,10 +612,13 @@ public function emergency($message, ...$context): void * trigger the SMS alerts and wake you up. * * @param string $message + * @param array $context * @return void */ - public function alert($message, ...$context): void + public function alert($message, $context = []): void { + $context = array_slice(func_get_args(), 1); + $this->driver()->alert($message, $this->mergeContexts($context)); } @@ -622,10 +628,13 @@ public function alert($message, ...$context): void * Example: Application component unavailable, unexpected exception. * * @param string $message + * @param array $context * @return void */ - public function critical($message, ...$context): void + public function critical($message, $context = []): void { + $context = array_slice(func_get_args(), 1); + $this->driver()->critical($message, $this->mergeContexts($context)); } @@ -634,10 +643,13 @@ public function critical($message, ...$context): void * be logged and monitored. * * @param string $message + * @param array $context * @return void */ - public function error($message, ...$context): void + public function error($message, $context = []): void { + $context = array_slice(func_get_args(), 1); + $this->driver()->error($message, $this->mergeContexts($context)); } @@ -648,10 +660,13 @@ public function error($message, ...$context): void * that are not necessarily wrong. * * @param string $message + * @param array $context * @return void */ - public function warning($message, ...$context): void + public function warning($message, $context = []): void { + $context = array_slice(func_get_args(), 1); + $this->driver()->warning($message, $this->mergeContexts($context)); } @@ -659,10 +674,13 @@ public function warning($message, ...$context): void * Normal but significant events. * * @param string $message + * @param array $context * @return void */ - public function notice($message, ...$context): void + public function notice($message, $context = []): void { + $context = array_slice(func_get_args(), 1); + $this->driver()->notice($message, $this->mergeContexts($context)); } @@ -672,10 +690,13 @@ public function notice($message, ...$context): void * Example: User logs in, SQL logs. * * @param string $message + * @param array $context * @return void */ - public function info($message, ...$context): void + public function info($message, $context = []): void { + $context = array_slice(func_get_args(), 1); + $this->driver()->info($message, $this->mergeContexts($context)); } @@ -683,10 +704,13 @@ public function info($message, ...$context): void * Detailed debug information. * * @param string $message + * @param array $context * @return void */ - public function debug($message, ...$context): void + public function debug($message, $context = []): void { + $context = array_slice(func_get_args(), 1); + $this->driver()->debug($message, $this->mergeContexts($context)); } @@ -695,10 +719,13 @@ public function debug($message, ...$context): void * * @param mixed $level * @param string $message + * @param array $context * @return void */ - public function log($level, $message, ...$context): void + public function log($level, $message, $context = []): void { + $context = array_slice(func_get_args(), 2); + $this->driver()->log($level, $message, $this->mergeContexts($context)); } @@ -715,7 +742,7 @@ public function __call($method, $parameters) } /** - * @param $userPassedContext + * @param $userPassedContext * @return array */ protected function mergeContexts($userPassedContext)