1<?php declare(strict_types=1); 2 3/* 4 * This file is part of the Monolog package. 5 * 6 * (c) Jordi Boggiano <j.boggiano@seld.be> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Monolog\Handler\FingersCrossed; 13 14use Monolog\Logger; 15use Psr\Log\LogLevel; 16 17/** 18 * Channel and Error level based monolog activation strategy. Allows to trigger activation 19 * based on level per channel. e.g. trigger activation on level 'ERROR' by default, except 20 * for records of the 'sql' channel; those should trigger activation on level 'WARN'. 21 * 22 * Example: 23 * 24 * <code> 25 * $activationStrategy = new ChannelLevelActivationStrategy( 26 * Logger::CRITICAL, 27 * array( 28 * 'request' => Logger::ALERT, 29 * 'sensitive' => Logger::ERROR, 30 * ) 31 * ); 32 * $handler = new FingersCrossedHandler(new StreamHandler('php://stderr'), $activationStrategy); 33 * </code> 34 * 35 * @author Mike Meessen <netmikey@gmail.com> 36 * 37 * @phpstan-import-type Record from \Monolog\Logger 38 * @phpstan-import-type Level from \Monolog\Logger 39 * @phpstan-import-type LevelName from \Monolog\Logger 40 */ 41class ChannelLevelActivationStrategy implements ActivationStrategyInterface 42{ 43 /** 44 * @var Level 45 */ 46 private $defaultActionLevel; 47 48 /** 49 * @var array<string, Level> 50 */ 51 private $channelToActionLevel; 52 53 /** 54 * @param int|string $defaultActionLevel The default action level to be used if the record's category doesn't match any 55 * @param array<string, int> $channelToActionLevel An array that maps channel names to action levels. 56 * 57 * @phpstan-param array<string, Level> $channelToActionLevel 58 * @phpstan-param Level|LevelName|LogLevel::* $defaultActionLevel 59 */ 60 public function __construct($defaultActionLevel, array $channelToActionLevel = []) 61 { 62 $this->defaultActionLevel = Logger::toMonologLevel($defaultActionLevel); 63 $this->channelToActionLevel = array_map('Monolog\Logger::toMonologLevel', $channelToActionLevel); 64 } 65 66 /** 67 * @phpstan-param Record $record 68 */ 69 public function isHandlerActivated(array $record): bool 70 { 71 if (isset($this->channelToActionLevel[$record['channel']])) { 72 return $record['level'] >= $this->channelToActionLevel[$record['channel']]; 73 } 74 75 return $record['level'] >= $this->defaultActionLevel; 76 } 77} 78