OpenXE/classes/Components/Logger/Handler/AbstractLogHandler.php
2021-05-21 08:49:41 +02:00

55 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Xentral\Components\Logger\Handler;
use Xentral\Components\Logger\Exception\InvalidArgumentException;
use Xentral\Components\Logger\LogLevel;
abstract class AbstractLogHandler implements LogHandlerInterface
{
/** @var int[] LEVEL_ORDER */
protected const LEVEL_ORDER = [
LogLevel::EMERGENCY => 8,
LogLevel::ALERT => 7,
LogLevel::CRITICAL => 6,
LogLevel::ERROR => 5,
LogLevel::WARNING => 4,
LogLevel::NOTICE => 3,
LogLevel::INFO => 2,
LogLevel::DEBUG => 1,
];
/** @var string $minimumLevel */
protected $minimumLevel;
/**
* @param string $level
*
* @return void
*/
protected function setMinimumLevel(string $level): void
{
if (!array_key_exists($level, self::LEVEL_ORDER)) {
throw new InvalidArgumentException(sprintf('Unrecognised loglevel "%s".', $level));
}
$this->minimumLevel = $level;
}
/**
* @param string $level
*
* @return bool
*/
public function canHandle(string $level): bool
{
if (!array_key_exists($level, self::LEVEL_ORDER)) {
return false;
}
return self::LEVEL_ORDER[$level] >= self::LEVEL_ORDER[$this->minimumLevel];
}
}