monolog icon indicating copy to clipboard operation
monolog copied to clipboard

How to extend monolog levels in monolog 3

Open lerminou opened this issue 2 years ago • 2 comments

Monolog version 3

Hi, We were using Monolog 2 and after upgrading to monolog 3, our app is not working anymore.

In monolog2 we have this piece of code in a custom Logger child class to extend monolog levels to add an 'AUDIT' level , for tracability, we need to keep these logs for a long time.

class Logger extends MonologLogger implements LoggerInterface
{
    public static function addLegalLevel()
    {
        self::$levels[LoggerInterface::AUDIT] = 'AUDIT';
    }
}

Now in monolog 3, levels are defined with an ENUM, which cannot be extended. And the \Monolog\Logger class is referencing this ENUM.

What's the new way to extend monolog levels ?

(we Use Symfony too.)

lerminou avatar Sep 07 '22 07:09 lerminou

Levels are not something that is extendable (that was not really a supported usage of Monolog 2 btw, more a hack of the implementation, and it would not pass static analysis checks).

To me, this is something you should solve with a dedicated channel of MonologBundle (i.e. a dedicated Logger instance with a specific config) rather than a special level.

stof avatar Sep 07 '22 08:09 stof

Oh, that's a bad news, thanks for the quick response.

Monolog a quite a standard and used is all php libraries. What's the motivation to remove the possiblity to extend it (I just saw a commit to mark Logger as final)

In Symfony, the DI can help us to use the new Logger service, but duplicate the Logger logic is not very interesting

lerminou avatar Sep 07 '22 08:09 lerminou

IMO custom levels was always a bad idea, because many handlers are built to handle the specific levels monolog ships with. And many log storage backends are also only using the standard 8 levels.

So yes as @stof mentioned using a custom channel so you can store these logs in a special place probably makes most sense. Or tag the logs somehow using a special context/extra data that lets you identify them.

Seldaek avatar Oct 12 '22 12:10 Seldaek