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

[9.x] Deprecation stack trace config option #42235

Merged
merged 3 commits into from
May 3, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/Illuminate/Foundation/Bootstrap/HandleExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function bootstrap(Application $app)
public function handleError($level, $message, $file = '', $line = 0, $context = [])
{
if ($this->isDeprecation($level)) {
return $this->handleDeprecation($message, $file, $line);
return $this->handleDeprecationError($message, $file, $line, $level);
}

if (error_reporting() & $level) {
Expand All @@ -83,8 +83,24 @@ public function handleError($level, $message, $file = '', $line = 0, $context =
* @param string $file
* @param int $line
* @return void
*
* @deprecated Use handleDeprecationError instead.
*/
public function handleDeprecation($message, $file, $line)
{
$this->handleDeprecationError($message, $file, $line);
}

/**
* Reports a deprecation to the "deprecations" logger.
*
* @param string $message
* @param string $file
* @param int $line
* @param int $level
* @return void
*/
public function handleDeprecationError($message, $file, $line, $level = E_DEPRECATED)
{
if (! class_exists(LogManager::class)
|| ! static::$app->hasBeenBootstrapped()
Expand All @@ -101,8 +117,16 @@ public function handleDeprecation($message, $file, $line)

$this->ensureDeprecationLoggerIsConfigured();

with($logger->channel('deprecations'), function ($log) use ($message, $file, $line) {
$log->warning((string) new ErrorException($message, 0, E_DEPRECATED, $file, $line));
$options = static::$app['config']->get('logging.deprecations') ?? [];

with($logger->channel('deprecations'), function ($log) use ($message, $file, $line, $level, $options) {
if ($options['trace'] ?? false) {
$log->warning((string) new ErrorException($message, 0, $level, $file, $line));
} else {
$log->warning(sprintf('%s in %s on line %s',
$message, $file, $line
));
}
});
}

Expand All @@ -120,7 +144,9 @@ protected function ensureDeprecationLoggerIsConfigured()

$this->ensureNullLogDriverIsConfigured();

$driver = $config->get('logging.deprecations') ?? 'null';
$options = $config->get('logging.deprecations');

$driver = is_array($options) ? $options['channel'] : ($options ?? 'null');

$config->set('logging.channels.deprecations', $config->get("logging.channels.{$driver}"));
});
Expand Down
50 changes: 50 additions & 0 deletions tests/Foundation/Bootstrap/HandleExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ public function testPhpDeprecations()
$logger = m::mock(LogManager::class);
$this->app->instance(LogManager::class, $logger);

$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
$logger->shouldReceive('warning')->with(sprintf('%s in %s on line %s',
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated',
'/home/user/laravel/routes/web.php',
17
));

$this->handleExceptions->handleError(
E_DEPRECATED,
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated',
'/home/user/laravel/routes/web.php',
17
);
}

public function testPhpDeprecationsWithStackTraces()
{
$logger = m::mock(LogManager::class);
$this->app->instance(LogManager::class, $logger);

$this->config->set('logging.deprecations', [
'channel' => 'null',
'trace' => true,
]);

$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
$logger->shouldReceive('warning')->with(
m::on(fn (string $message) => (bool) preg_match(
Expand Down Expand Up @@ -78,6 +103,31 @@ public function testUserDeprecations()
$logger = m::mock(LogManager::class);
$this->app->instance(LogManager::class, $logger);

$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
$logger->shouldReceive('warning')->with(sprintf('%s in %s on line %s',
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated',
'/home/user/laravel/routes/web.php',
17
));

$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
);
}

public function testUserDeprecationsWithStackTraces()
{
$logger = m::mock(LogManager::class);
$this->app->instance(LogManager::class, $logger);

$this->config->set('logging.deprecations', [
'channel' => 'null',
'trace' => true,
]);

$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
$logger->shouldReceive('warning')->with(
m::on(fn (string $message) => (bool) preg_match(
Expand Down