diff --git a/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php b/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php index 7914695678c7..7131ffd7ab9b 100644 --- a/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php +++ b/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Foundation\Application; use Illuminate\Log\LogManager; +use Monolog\Handler\NullHandler; use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\ErrorHandler\Error\FatalError; use Throwable; @@ -112,12 +113,33 @@ protected function ensureDeprecationLoggerIsConfigured() return; } + $this->ensureNullLogDriverIsConfigured(); + $driver = $config->get('logging.deprecations') ?? 'null'; $config->set('logging.channels.deprecations', $config->get("logging.channels.{$driver}")); }); } + /** + * Ensure the "null" log driver is configured. + * + * @return void + */ + protected function ensureNullLogDriverIsConfigured() + { + with($this->app['config'], function ($config) { + if ($config->get('logging.channels.null')) { + return; + } + + $config->set('logging.channels.null', [ + 'driver' => 'monolog', + 'handler' => NullHandler::class, + ]); + }); + } + /** * Handle an uncaught exception from the application. * diff --git a/tests/Foundation/Bootstrap/HandleExceptionsTest.php b/tests/Foundation/Bootstrap/HandleExceptionsTest.php index 14a314322268..6bb1ee150bda 100644 --- a/tests/Foundation/Bootstrap/HandleExceptionsTest.php +++ b/tests/Foundation/Bootstrap/HandleExceptionsTest.php @@ -8,6 +8,7 @@ use Illuminate\Foundation\Bootstrap\HandleExceptions; use Illuminate\Log\LogManager; use Mockery as m; +use Monolog\Handler\NullHandler; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -134,9 +135,49 @@ public function testEnsuresNullDeprecationsDriver() $logger->shouldReceive('channel')->andReturnSelf(); $logger->shouldReceive('warning'); + $this->handleExceptions->handleError( + E_USER_DEPRECATED, + 'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated', + '/home/user/laravel/routes/web.php', + 17 + ); + + $this->assertEquals( + NullHandler::class, + $this->config->get('logging.channels.deprecations.handler') + ); + } + + public function testEnsuresNullLogDriver() + { + $logger = m::mock(LogManager::class); + $this->container->instance(LogManager::class, $logger); + $logger->shouldReceive('channel')->andReturnSelf(); + $logger->shouldReceive('warning'); + + $this->handleExceptions->handleError( + E_USER_DEPRECATED, + 'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated', + '/home/user/laravel/routes/web.php', + 17 + ); + + $this->assertEquals( + NullHandler::class, + $this->config->get('logging.channels.deprecations.handler') + ); + } + + public function testDoNotOverrideExistingNullLogDriver() + { + $logger = m::mock(LogManager::class); + $this->container->instance(LogManager::class, $logger); + $logger->shouldReceive('channel')->andReturnSelf(); + $logger->shouldReceive('warning'); + $this->config->set('logging.channels.null', [ 'driver' => 'monolog', - 'handler' => NullHandler::class, + 'handler' => CustomNullHandler::class, ]); $this->handleExceptions->handleError( @@ -147,7 +188,7 @@ public function testEnsuresNullDeprecationsDriver() ); $this->assertEquals( - NullHandler::class, + CustomNullHandler::class, $this->config->get('logging.channels.deprecations.handler') ); } @@ -168,3 +209,7 @@ public function testIgnoreDeprecationIfLoggerUnresolvable() ); } } + +class CustomNullHandler extends NullHandler +{ +}