[GH-ISSUE #421] Support for PSR3 LogLevel? #144

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

Originally created by @polyfractal on GitHub (Sep 16, 2014).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/421

This issue was raised over at the Elasticsearch-PHP library: https://github.com/elasticsearch/elasticsearch-php/issues/125

The library is instantiating Monolog with a handler and a processor. Importantly, the logging level is being set with the LogLevel PSR3 class:

$logLevel = Log\LogLevel::WARNING;  // <== using LogLevel:: instead of Logger::

$log       = new Logger('log');
$handler   = new StreamHandler(
    $logPath,
    $logLevel,
   ...
);
$processor = new IntrospectionProcessor();

$log->pushHandler($handler);
$log->pushProcessor($processor);

...

This proves problematic, since it seems Monolog is using it's own Logger class for log level enum instead of LogLevel?

Is there a better way to configure Monolog in a way that still allows interoperability with other loggers? I'm happy to convert the LogLevel enum into the Logger enum if there isn't another way, but wanted to make sure I wasn't doing something silly :)

Originally created by @polyfractal on GitHub (Sep 16, 2014). Original GitHub issue: https://github.com/Seldaek/monolog/issues/421 This issue was raised over at the Elasticsearch-PHP library: https://github.com/elasticsearch/elasticsearch-php/issues/125 The library is instantiating Monolog with a handler and a processor. Importantly, the logging level is being set with the LogLevel PSR3 class: ``` php $logLevel = Log\LogLevel::WARNING; // <== using LogLevel:: instead of Logger:: $log = new Logger('log'); $handler = new StreamHandler( $logPath, $logLevel, ... ); $processor = new IntrospectionProcessor(); $log->pushHandler($handler); $log->pushProcessor($processor); ... ``` This proves problematic, since it seems Monolog is using it's own `Logger` class for log level enum instead of `LogLevel`? Is there a better way to configure Monolog in a way that still allows interoperability with other loggers? I'm happy to convert the `LogLevel` enum into the `Logger` enum if there isn't another way, but wanted to make sure I wasn't doing something silly :)
kerem 2026-03-04 02:12:35 +03:00
  • closed this issue
  • added the
    Feature
    label
Author
Owner

@stof commented on GitHub (Sep 20, 2014):

PSR-3 log levels are support for calling code, and are converted to Monolog levels. So the handler should always use Monolog levels to handle the level filtering, as it is what it will get

<!-- gh-comment-id:56251026 --> @stof commented on GitHub (Sep 20, 2014): PSR-3 log levels are support for calling code, and are converted to Monolog levels. So the handler should always use Monolog levels to handle the level filtering, as it is what it will get
Author
Owner

@stof commented on GitHub (Sep 20, 2014):

And the reason why all Monolog internals does not use PSR-3 constants is simple: Monolog was written at least 2 years before PSR-3

<!-- gh-comment-id:56251395 --> @stof commented on GitHub (Sep 20, 2014): And the reason why all Monolog internals does not use PSR-3 constants is simple: Monolog was written at least 2 years before PSR-3
Author
Owner

@polyfractal commented on GitHub (Sep 22, 2014):

And the reason why all Monolog internals does not use PSR-3 constants is simple: Monolog was written at least 2 years before PSR-3

Ah, that makes sense :) Thanks for the help!

Would it be possible to add a static "helper" method to Monolog that converts from PSR3 LogLevel to Monolog's LogLevel? E.g. ES-PHP users configure with LogLevel, internally the library calls the helper shim to convert LogLevel to Logger and then instantiates the handler chain using the appropriate Logger values.

Even though the handler instantiation isn't "user land code", it's still a public API which things like the ES library interact with.

I can easily add a helper shim to ES-PHP, but if it was in Monolog that would be even better so that changes in Monolog don't break ES-PHP :)

<!-- gh-comment-id:56372730 --> @polyfractal commented on GitHub (Sep 22, 2014): > And the reason why all Monolog internals does not use PSR-3 constants is simple: Monolog was written at least 2 years before PSR-3 Ah, that makes sense :) Thanks for the help! Would it be possible to add a static "helper" method to Monolog that converts from PSR3 `LogLevel` to Monolog's `LogLevel`? E.g. ES-PHP users configure with `LogLevel`, internally the library calls the helper shim to convert `LogLevel` to `Logger` and then instantiates the handler chain using the appropriate `Logger` values. Even though the handler instantiation isn't "user land code", it's still a public API which things like the ES library interact with. I can easily add a helper shim to ES-PHP, but if it was in Monolog that would be even better so that changes in Monolog don't break ES-PHP :)
Author
Owner

@Seldaek commented on GitHub (Sep 22, 2014):

Any breaking changes to the log level will happen in 2.0 (if there are changes at all, it will be to normalize on the PSR-3 levels I suppose). If you require monolog ~1.0 you're safe.

That said yes it would be a good idea to add support for PSR-3 levels already in a 1.x release to make migration to a 2.0 easier. They don't conflict so it should be kinda easy to support them on all external/public API points.

<!-- gh-comment-id:56373510 --> @Seldaek commented on GitHub (Sep 22, 2014): Any breaking changes to the log level will happen in 2.0 (if there are changes at all, it will be to normalize on the PSR-3 levels I suppose). If you require monolog ~1.0 you're safe. That said yes it would be a good idea to add support for PSR-3 levels already in a 1.x release to make migration to a 2.0 easier. They don't conflict so it should be kinda easy to support them on all external/public API points.
Author
Owner

@polyfractal commented on GitHub (Sep 23, 2014):

Great, thanks! I'll add a shim in ES-PHP and keep an eye on Monolog for similar functionality in the future (or just 2.0)

<!-- gh-comment-id:56516369 --> @polyfractal commented on GitHub (Sep 23, 2014): Great, thanks! I'll add a shim in ES-PHP and keep an eye on Monolog for similar functionality in the future (or just 2.0)
Author
Owner

@polyfractal commented on GitHub (Sep 30, 2014):

Awesome! Thanks for the quick turnaround on this :)

<!-- gh-comment-id:57309805 --> @polyfractal commented on GitHub (Sep 30, 2014): Awesome! Thanks for the quick turnaround on this :)
Author
Owner

@Seldaek commented on GitHub (Sep 30, 2014):

1.11 is tagged so you could depend on monolog ~1.11 in ES-PHP if you want to drop your shim.

<!-- gh-comment-id:57313741 --> @Seldaek commented on GitHub (Sep 30, 2014): 1.11 is tagged so you could depend on monolog `~1.11` in ES-PHP if you want to drop your shim.
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#144
No description provided.