[GH-ISSUE #1757] How to extend monolog levels in monolog 3 #744

Closed
opened 2026-03-04 02:17:35 +03:00 by kerem · 3 comments
Owner

Originally created by @lerminou on GitHub (Sep 7, 2022).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1757

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.)

Originally created by @lerminou on GitHub (Sep 7, 2022). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1757 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.)
kerem 2026-03-04 02:17:35 +03:00
  • closed this issue
  • added the
    Support
    label
Author
Owner

@stof commented on GitHub (Sep 7, 2022):

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.

<!-- gh-comment-id:1239045221 --> @stof commented on GitHub (Sep 7, 2022): 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.
Author
Owner

@lerminou commented on GitHub (Sep 7, 2022):

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

<!-- gh-comment-id:1239058372 --> @lerminou commented on GitHub (Sep 7, 2022): 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](https://github.com/Seldaek/monolog/commit/3cba75ec0926eec177a4a2cd6c977ecddd0fc7c1)) In Symfony, the DI can help us to use the new Logger service, but duplicate the Logger logic is not very interesting
Author
Owner

@Seldaek commented on GitHub (Oct 12, 2022):

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.

<!-- gh-comment-id:1276052089 --> @Seldaek commented on GitHub (Oct 12, 2022): 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.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#744
No description provided.