monolog icon indicating copy to clipboard operation
monolog copied to clipboard

Can't use GroupHandler in FingersCrossedHandler

Open tomasbro opened this issue 3 years ago • 0 comments

When I try to use GroupHandler in FingersCrossedHandler I got an exception:

UnexpectedValueException(code: 0): The nested handler of type Monolog\\Handler\\GroupHandler does not support formatters. at /app/vendor/monolog/monolog/src/Monolog/Handler/FingersCrossedHandler.php:237

FingersCrossedHandler expects that wrapped handler will implement FormattableHandlerInterface and GroupHandler doesn't implement it.

tomasbro avatar Aug 08 '22 08:08 tomasbro

Could you add the code to show what you are trying to do?

mimmi20 avatar Aug 17 '22 05:08 mimmi20

This should work fine as long as you don't configure a formatter on the FingersCrossHandler, because then it must forward it to the nested handler and that will fail on GroupHandler.

But if you configure the formatters where they belong it should work fine.

Seldaek avatar Aug 20 '22 12:08 Seldaek

Well, FingersCrossedHandler implements FormattableHandlerInterface so it means you can use setFormatter() method, but it works fine only if the "inner" handler also implements FromattablehandlerInterface. GroupHandler doesn't implement it so it means you can't use it in FingersCrossedHandler and try to set formatter. Kinda understand it, but Laravel doesn't :)

I was using Laravel and in LogManager it automatically tries apply formatter if handler implements FormattableHandlerInterface. LogManager:

    protected function prepareHandler(HandlerInterface $handler, array $config = [])
    {
        if (isset($config['action_level'])) {
            $handler = new FingersCrossedHandler($handler, $this->actionLevel($config));
        }

        if (! $handler instanceof FormattableHandlerInterface) {
            return $handler;
        }

        if (! isset($config['formatter'])) {
            $handler->setFormatter($this->formatter());
        } elseif ($config['formatter'] !== 'default') {
            $handler->setFormatter($this->app->make($config['formatter'], $config['formatter_with'] ?? []));
        }

        return $handler;
    }

I will find workaround. Probably I will need to write own Handler which extends FingersCrossedHandler.

You can close this one, I think. Thanks

tomasbro avatar Aug 20 '22 16:08 tomasbro

Ok, IMO this should be reported to laravel and fixed there. Having a default formatter across all handlers makes little sense as they have such different needs. So if a specific formatter isn't configured on a given handler it shouldn't call setFormatter at all I'd say.

Every handler comes with its own sensible default formatter anyway out of the box so overriding that with a single one at framework level is just harmful.

Seldaek avatar Aug 20 '22 18:08 Seldaek