1 <?php declare(strict_types=1);
4 * This file is part of the Monolog package.
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Monolog\Handler\FingersCrossed;
17 use Monolog\LogRecord;
20 * Channel and Error level based monolog activation strategy. Allows to trigger activation
21 * based on level per channel. e.g. trigger activation on level 'ERROR' by default, except
22 * for records of the 'sql' channel; those should trigger activation on level 'WARN'.
27 * $activationStrategy = new ChannelLevelActivationStrategy(
30 * 'request' => Level::Alert,
31 * 'sensitive' => Level::Error,
34 * $handler = new FingersCrossedHandler(new StreamHandler('php://stderr'), $activationStrategy);
37 * @author Mike Meessen <netmikey@gmail.com>
39 class ChannelLevelActivationStrategy implements ActivationStrategyInterface
41 private Level $defaultActionLevel;
44 * @var array<string, Level>
46 private array $channelToActionLevel;
49 * @param int|string|Level|LogLevel::* $defaultActionLevel The default action level to be used if the record's category doesn't match any
50 * @param array<string, int|string|Level|LogLevel::*> $channelToActionLevel An array that maps channel names to action levels.
52 * @phpstan-param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $defaultActionLevel
53 * @phpstan-param array<string, value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::*> $channelToActionLevel
55 public function __construct(int|string|Level $defaultActionLevel, array $channelToActionLevel = [])
57 $this->defaultActionLevel = Logger::toMonologLevel($defaultActionLevel);
58 $this->channelToActionLevel = array_map(Logger::toMonologLevel(...), $channelToActionLevel);
61 public function isHandlerActivated(LogRecord $record): bool
63 if (isset($this->channelToActionLevel[$record->channel])) {
64 return $record->level->value >= $this->channelToActionLevel[$record->channel]->value;
67 return $record->level->value >= $this->defaultActionLevel->value;