Skip to content

Commit

Permalink
Fixes logging deprecations when null driver do not exist (#39809)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Nov 29, 2021
1 parent f47d18f commit 606ac0a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/Illuminate/Foundation/Bootstrap/HandleExceptions.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
49 changes: 47 additions & 2 deletions tests/Foundation/Bootstrap/HandleExceptionsTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand All @@ -147,7 +188,7 @@ public function testEnsuresNullDeprecationsDriver()
);

$this->assertEquals(
NullHandler::class,
CustomNullHandler::class,
$this->config->get('logging.channels.deprecations.handler')
);
}
Expand All @@ -168,3 +209,7 @@ public function testIgnoreDeprecationIfLoggerUnresolvable()
);
}
}

class CustomNullHandler extends NullHandler
{
}

0 comments on commit 606ac0a

Please sign in to comment.