friendica-addons/monolog/vendor/monolog/monolog/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php

78 lines
2.4 KiB
PHP
Raw Normal View History

2022-10-17 21:25:03 +02:00
<?php declare(strict_types=1);
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Handler\FingersCrossed;
use Monolog\Logger;
use Psr\Log\LogLevel;
/**
* Channel and Error level based monolog activation strategy. Allows to trigger activation
* based on level per channel. e.g. trigger activation on level 'ERROR' by default, except
* for records of the 'sql' channel; those should trigger activation on level 'WARN'.
*
* Example:
*
* <code>
* $activationStrategy = new ChannelLevelActivationStrategy(
2023-01-18 00:17:49 +01:00
* Logger::CRITICAL,
2022-10-17 21:25:03 +02:00
* array(
2023-01-18 00:17:49 +01:00
* 'request' => Logger::ALERT,
* 'sensitive' => Logger::ERROR,
2022-10-17 21:25:03 +02:00
* )
* );
* $handler = new FingersCrossedHandler(new StreamHandler('php://stderr'), $activationStrategy);
* </code>
*
* @author Mike Meessen <netmikey@gmail.com>
2023-01-18 00:17:49 +01:00
*
* @phpstan-import-type Record from \Monolog\Logger
* @phpstan-import-type Level from \Monolog\Logger
* @phpstan-import-type LevelName from \Monolog\Logger
2022-10-17 21:25:03 +02:00
*/
class ChannelLevelActivationStrategy implements ActivationStrategyInterface
{
2023-01-18 00:17:49 +01:00
/**
* @var Level
*/
private $defaultActionLevel;
2022-10-17 21:25:03 +02:00
/**
* @var array<string, Level>
*/
2023-01-18 00:17:49 +01:00
private $channelToActionLevel;
2022-10-17 21:25:03 +02:00
/**
2023-01-18 00:17:49 +01:00
* @param int|string $defaultActionLevel The default action level to be used if the record's category doesn't match any
* @param array<string, int> $channelToActionLevel An array that maps channel names to action levels.
2022-10-17 21:25:03 +02:00
*
2023-01-18 00:17:49 +01:00
* @phpstan-param array<string, Level> $channelToActionLevel
* @phpstan-param Level|LevelName|LogLevel::* $defaultActionLevel
2022-10-17 21:25:03 +02:00
*/
2023-01-18 00:17:49 +01:00
public function __construct($defaultActionLevel, array $channelToActionLevel = [])
2022-10-17 21:25:03 +02:00
{
$this->defaultActionLevel = Logger::toMonologLevel($defaultActionLevel);
2023-01-18 00:17:49 +01:00
$this->channelToActionLevel = array_map('Monolog\Logger::toMonologLevel', $channelToActionLevel);
2022-10-17 21:25:03 +02:00
}
2023-01-18 00:17:49 +01:00
/**
* @phpstan-param Record $record
*/
public function isHandlerActivated(array $record): bool
2022-10-17 21:25:03 +02:00
{
2023-01-18 00:17:49 +01:00
if (isset($this->channelToActionLevel[$record['channel']])) {
return $record['level'] >= $this->channelToActionLevel[$record['channel']];
2022-10-17 21:25:03 +02:00
}
2023-01-18 00:17:49 +01:00
return $record['level'] >= $this->defaultActionLevel;
2022-10-17 21:25:03 +02:00
}
}