laravel/framework

Injecting a specific logger using Dependency Injection from AppServiceProvider creates bugs within the framework error handler logger

Ouverte

#55 435 ouverte le 16 avr. 2025

 (5 commentaires) (0 réaction) (0 personne assignée)PHP (11 951 forks)batch import
help wanted

Métriques du dépôt

Stars
 (34 872 étoiles)
Métriques de merge PR
 (Merge moyen 1j 22h) (129 PRs mergées en 30 j)

Description

Laravel Version

12.9.1

PHP Version

8.3.15

Database Driver & Version

No response

Description

When dynamically injecting a Logger in a class using dependency injection, the framework application container builds other dependencies. If one of these dependencies has a problem in its constructor (like a deprecation warning), then when trying to get the deprecation logger, the error handler asks to the container for a LogManager but ends up with a Logger, which makes the call to $logger->channel fail because a Logger has no channel method.

HandleExceptions.php :

       try {
            // HERE, asking for a LogManager, but getting a Logger instead
            $logger = static::$app->make(LogManager::class);
        } catch (Exception) {
            return;
        }

        $this->ensureDeprecationLoggerIsConfigured();

        $options = static::$app['config']->get('logging.deprecations') ?? [];

        // HERE, the call to channel fails and throws an error
        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
                ));
            }
        });

Image

I don't know why this happens as it was too complex for me to dig deeper inside the inner workings of the Application Container, but for sure this looks like a bug.

Steps To Reproduce

clone & run : (GET '/') https://github.com/hilnius/laravel-container-logger-issue

specific commit with the diff (based on a brand new 'laravel new' repository) https://github.com/hilnius/laravel-container-logger-issue/commit/e2903ff613be8174d42b0a3b20e933659138f2e3

Guide contributeur