* @package generis */ class common_log_Logger2Psr extends AbstractLogger { /** * A map between the loggers * @var array */ private static $map = [ LogLevel::EMERGENCY => common_Logger::FATAL_LEVEL, LogLevel::ALERT => common_Logger::FATAL_LEVEL, LogLevel::CRITICAL => common_Logger::ERROR_LEVEL, LogLevel::ERROR => common_Logger::ERROR_LEVEL, LogLevel::WARNING => common_Logger::WARNING_LEVEL, LogLevel::INFO => common_Logger::INFO_LEVEL, LogLevel::NOTICE => common_Logger::DEBUG_LEVEL, LogLevel::DEBUG => common_Logger::DEBUG_LEVEL, ]; /** * @var array The common_logger level - PSR3 log level conversion. */ private static $reverseMap = [ common_Logger::TRACE_LEVEL => LogLevel::DEBUG, 't' => LogLevel::DEBUG, common_Logger::DEBUG_LEVEL => LogLevel::DEBUG, 'd' => LogLevel::DEBUG, common_Logger::INFO_LEVEL => LogLevel::INFO, 'i' => LogLevel::INFO, common_Logger::WARNING_LEVEL => LogLevel::WARNING, 'w' => LogLevel::WARNING, common_Logger::ERROR_LEVEL => LogLevel::ERROR, 'e' => LogLevel::ERROR, common_Logger::FATAL_LEVEL => LogLevel::CRITICAL, 'f' => LogLevel::CRITICAL, ]; /** * @var common_Logger */ private $logger; public function __construct(common_Logger $logger) { $this->logger = $logger; } /** * (non-PHPdoc) * @see \Psr\Log\LoggerInterface::log() */ public function log($level, $message, array $context = []) { $errorLevel = isset(self::$map[$level]) ? self::$map[$level] : common_Logger::ERROR_LEVEL; $this->logger->log($errorLevel, $message, $context); } public static function getCommonFromPsrLevel($level) { if (empty(self::$map[$level])) { throw new Exception('Invalid error level in Common level to PSR conversion: ' . $level); } return self::$map[$level]; } /** * Returns the PSR log level based on the common log level. * * @param string $level * * @return string The PSR log level. * * @throws Exception When invalid level was presented. */ public static function getPsrLevelFromCommon($level) { if (empty(self::$reverseMap[$level])) { throw new Exception('Invalid error level in PSR to Common level conversion: ' . $level); } return self::$reverseMap[$level]; } }