Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Fixes logging deprecations when null driver do not exist #39809

Merged
merged 1 commit into from Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
{
}