This is a FingersCrossed handler that fills a missing gap in Monolog configuration:
It allows to have "primary" channels. If log is being written to one of these "primary" channels, it skips fingers crossed behaviour. This is combined with support for a Log Level. So you can still apply fingers crossed behaviour to message from primary channel, if it has low severity.
Best real world scenario of this is to be able to rely on >= INFO "app" logs always being written, no matter if there was an error or not (and if there was an error, ensure these log entries are not written twice).
This is something that's impossible to configure without writing custom handler, unless you are fine with duplicate log entries.
Via Composer
composer require ostrolucky/semantic-monolog-handler
Handler configured following way will write all the logs to standard error output if any of the logs reach "ERROR" level, which is a standard FingersCrossed behaviour.
However, on top of it, it will also immediately print logs from "app" channel, unless they are under "DEBUG" level - without triggering FingersCrossed handler (which would flush all the logs).
use Monolog\Level;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\StreamHandler;
use Ostrolucky\SemanticMonologHandler;
$logger = new SemanticLogHandler(
innerHandler: new StreamHandler(STDERR),
primaryChannels: ['app' => Level::Info],
activationStrategy: new ErrorLevelActivationStrategy(Level::Error),
);
Above PHP configuration can be reflected in Symfony like so:
monolog.yaml:
monolog:
handlers:
main:
type: service
id: Ostrolucky\SemanticMonologHandler\SemanticLogHandler
services:
Ostrolucky\SemanticMonologHandler\SemanticLogHandler:
autoconfigure: true
arguments:
- !service
class: Monolog\Handler\StreamHandler
arguments:
- !php/const STDERR
- app: !php/enum Psr\Log\LogLevel\Level::Error
- !service
class: Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy
arguments: [!php/enum Psr\Log\LogLevel\Level::Error]
MIT license. Please see License File for more information.