Skip to content

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

Open
@hilnius

Description

@hilnius

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)
hilnius/laravel-container-logger-issue@e2903ff

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions