laravel/framework

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

Open

#55,435 opened on Apr 16, 2025

View on GitHub
 (5 comments) (0 reactions) (0 assignees)PHP (34,705 stars) (11,882 forks)batch import
help wanted

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

Contributor guide

Injecting a specific logger using Dependency Injection from AppServiceProvider creates bugs within the framework error handler logger · laravel/framework#55435 | Good First Issue