Skip to content

Commit 710ba7f

Browse files
[9.x] Deprecation stack trace config option (#42235)
* Report proper deprecation level * Option to enable deprecation stack traces * Apply fixes from StyleCI Co-authored-by: StyleCI Bot <[email protected]>
1 parent 2b51648 commit 710ba7f

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

src/Illuminate/Foundation/Bootstrap/HandleExceptions.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function bootstrap(Application $app)
6868
public function handleError($level, $message, $file = '', $line = 0, $context = [])
6969
{
7070
if ($this->isDeprecation($level)) {
71-
return $this->handleDeprecation($message, $file, $line);
71+
return $this->handleDeprecationError($message, $file, $line, $level);
7272
}
7373

7474
if (error_reporting() & $level) {
@@ -83,8 +83,24 @@ public function handleError($level, $message, $file = '', $line = 0, $context =
8383
* @param string $file
8484
* @param int $line
8585
* @return void
86+
*
87+
* @deprecated Use handleDeprecationError instead.
8688
*/
8789
public function handleDeprecation($message, $file, $line)
90+
{
91+
$this->handleDeprecationError($message, $file, $line);
92+
}
93+
94+
/**
95+
* Reports a deprecation to the "deprecations" logger.
96+
*
97+
* @param string $message
98+
* @param string $file
99+
* @param int $line
100+
* @param int $level
101+
* @return void
102+
*/
103+
public function handleDeprecationError($message, $file, $line, $level = E_DEPRECATED)
88104
{
89105
if (! class_exists(LogManager::class)
90106
|| ! static::$app->hasBeenBootstrapped()
@@ -101,8 +117,16 @@ public function handleDeprecation($message, $file, $line)
101117

102118
$this->ensureDeprecationLoggerIsConfigured();
103119

104-
with($logger->channel('deprecations'), function ($log) use ($message, $file, $line) {
105-
$log->warning((string) new ErrorException($message, 0, E_DEPRECATED, $file, $line));
120+
$options = static::$app['config']->get('logging.deprecations') ?? [];
121+
122+
with($logger->channel('deprecations'), function ($log) use ($message, $file, $line, $level, $options) {
123+
if ($options['trace'] ?? false) {
124+
$log->warning((string) new ErrorException($message, 0, $level, $file, $line));
125+
} else {
126+
$log->warning(sprintf('%s in %s on line %s',
127+
$message, $file, $line
128+
));
129+
}
106130
});
107131
}
108132

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

121145
$this->ensureNullLogDriverIsConfigured();
122146

123-
$driver = $config->get('logging.deprecations') ?? 'null';
147+
$options = $config->get('logging.deprecations');
148+
149+
$driver = is_array($options) ? $options['channel'] : ($options ?? 'null');
124150

125151
$config->set('logging.channels.deprecations', $config->get("logging.channels.{$driver}"));
126152
});

tests/Foundation/Bootstrap/HandleExceptionsTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,31 @@ public function testPhpDeprecations()
4949
$logger = m::mock(LogManager::class);
5050
$this->app->instance(LogManager::class, $logger);
5151

52+
$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
53+
$logger->shouldReceive('warning')->with(sprintf('%s in %s on line %s',
54+
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated',
55+
'/home/user/laravel/routes/web.php',
56+
17
57+
));
58+
59+
$this->handleExceptions->handleError(
60+
E_DEPRECATED,
61+
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated',
62+
'/home/user/laravel/routes/web.php',
63+
17
64+
);
65+
}
66+
67+
public function testPhpDeprecationsWithStackTraces()
68+
{
69+
$logger = m::mock(LogManager::class);
70+
$this->app->instance(LogManager::class, $logger);
71+
72+
$this->config->set('logging.deprecations', [
73+
'channel' => 'null',
74+
'trace' => true,
75+
]);
76+
5277
$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
5378
$logger->shouldReceive('warning')->with(
5479
m::on(fn (string $message) => (bool) preg_match(
@@ -78,6 +103,31 @@ public function testUserDeprecations()
78103
$logger = m::mock(LogManager::class);
79104
$this->app->instance(LogManager::class, $logger);
80105

106+
$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
107+
$logger->shouldReceive('warning')->with(sprintf('%s in %s on line %s',
108+
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated',
109+
'/home/user/laravel/routes/web.php',
110+
17
111+
));
112+
113+
$this->handleExceptions->handleError(
114+
E_USER_DEPRECATED,
115+
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated',
116+
'/home/user/laravel/routes/web.php',
117+
17
118+
);
119+
}
120+
121+
public function testUserDeprecationsWithStackTraces()
122+
{
123+
$logger = m::mock(LogManager::class);
124+
$this->app->instance(LogManager::class, $logger);
125+
126+
$this->config->set('logging.deprecations', [
127+
'channel' => 'null',
128+
'trace' => true,
129+
]);
130+
81131
$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
82132
$logger->shouldReceive('warning')->with(
83133
m::on(fn (string $message) => (bool) preg_match(

0 commit comments

Comments
 (0)