diff --git a/src/Illuminate/Log/LogManager.php b/src/Illuminate/Log/LogManager.php index f528bc58eb55..0d9d4c9f5d83 100644 --- a/src/Illuminate/Log/LogManager.php +++ b/src/Illuminate/Log/LogManager.php @@ -598,9 +598,11 @@ public function getChannels() * @param array $context * @return void */ - public function emergency($message, array $context = []): void + public function emergency($message, $context = []): void { - $this->driver()->emergency($message, $context); + $context = array_slice(func_get_args(), 1); + + $this->driver()->emergency($message, $this->mergeContexts($context)); } /** @@ -613,9 +615,11 @@ public function emergency($message, array $context = []): void * @param array $context * @return void */ - public function alert($message, array $context = []): void + public function alert($message, $context = []): void { - $this->driver()->alert($message, $context); + $context = array_slice(func_get_args(), 1); + + $this->driver()->alert($message, $this->mergeContexts($context)); } /** @@ -627,9 +631,11 @@ public function alert($message, array $context = []): void * @param array $context * @return void */ - public function critical($message, array $context = []): void + public function critical($message, $context = []): void { - $this->driver()->critical($message, $context); + $context = array_slice(func_get_args(), 1); + + $this->driver()->critical($message, $this->mergeContexts($context)); } /** @@ -640,9 +646,11 @@ public function critical($message, array $context = []): void * @param array $context * @return void */ - public function error($message, array $context = []): void + public function error($message, $context = []): void { - $this->driver()->error($message, $context); + $context = array_slice(func_get_args(), 1); + + $this->driver()->error($message, $this->mergeContexts($context)); } /** @@ -655,9 +663,11 @@ public function error($message, array $context = []): void * @param array $context * @return void */ - public function warning($message, array $context = []): void + public function warning($message, $context = []): void { - $this->driver()->warning($message, $context); + $context = array_slice(func_get_args(), 1); + + $this->driver()->warning($message, $this->mergeContexts($context)); } /** @@ -667,9 +677,11 @@ public function warning($message, array $context = []): void * @param array $context * @return void */ - public function notice($message, array $context = []): void + public function notice($message, $context = []): void { - $this->driver()->notice($message, $context); + $context = array_slice(func_get_args(), 1); + + $this->driver()->notice($message, $this->mergeContexts($context)); } /** @@ -681,9 +693,11 @@ public function notice($message, array $context = []): void * @param array $context * @return void */ - public function info($message, array $context = []): void + public function info($message, $context = []): void { - $this->driver()->info($message, $context); + $context = array_slice(func_get_args(), 1); + + $this->driver()->info($message, $this->mergeContexts($context)); } /** @@ -693,9 +707,11 @@ public function info($message, array $context = []): void * @param array $context * @return void */ - public function debug($message, array $context = []): void + public function debug($message, $context = []): void { - $this->driver()->debug($message, $context); + $context = array_slice(func_get_args(), 1); + + $this->driver()->debug($message, $this->mergeContexts($context)); } /** @@ -706,9 +722,11 @@ public function debug($message, array $context = []): void * @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); + $context = array_slice(func_get_args(), 2); + + $this->driver()->log($level, $message, $this->mergeContexts($context)); } /** @@ -722,4 +740,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..680db863b0a9 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'];