3

If I write some PHP code with a deprecated notice, it displays on a local environment.

Example: index.php

<?php

function super(string $test='', string $essai)
{
    return 'toto';
}

super('sy', 1);

exit;

Displays:

Deprecated: Optional parameter $test declared before required parameter $essai is implicitly treated as a required parameter in /var/www/public/index.php on line 9

Which is fine.

But the exact same code in a controller in Laravel does not display, and is not stored in any log file. I set the config app.env to "local", app.debug to true and app.log_level to "debug". Any idea what I am missing ?

Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
melicerte
  • 363
  • 4
  • 12

1 Answers1

4

According to the docs:

Logging Deprecation Warnings

PHP, Laravel, and other libraries often notify their users that some of their features have been deprecated and will be removed in a future version. If you would like to log these deprecation warnings, you may specify your preferred deprecations log channel in your application's config/logging.php configuration file:

'deprecations' => env('LOG_DEPRECATIONS_CHANNEL', 'null'),

'channels' => [
    ...
]

Or, you may define a log channel named deprecations. If a log channel with this name exists, it will always be used to log deprecations:

'channels' => [
    'deprecations' => [
        'driver' => 'single',
        'path' => storage_path('logs/php-deprecation-warnings.log'),
    ],
],
Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
  • 1
    Thanks for pointing the doc ! Unfortunately, it does not work well. Some deprecations are always logged, some are logged sometimes, some are never logged. I'll stick with phpstan to detect deprecated code. – melicerte Jan 26 '22 at 16:24
  • @melicerte you would be better using both. Not all deprecation issues can be found by static analysis. – Jason Jun 06 '22 at 11:29
  • I would if it worked – melicerte Jun 07 '22 at 13:32
  • 1
    As a temporary measure, you can add `$log->warning(new \Exception('Stack trace'));` to `Illuminate/Foundation/Bootstrap/HandleExceptions::handleDeprecation()` just after the existing `$log->warning()` and a full stack trace will be added to each deprecation log entry. it can generate tonnes of log very quickly, but is often necessary to trace the real source of the deprecation notices. This should be an option in the framework that can be enabled to locate the deprecation source IMO, but hopefully as a quick hack you find it useful. – Jason Jan 10 '23 at 13:27