Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
22.22% |
2 / 9 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| common_log_Logger2Psr | |
22.22% |
2 / 9 |
|
0.00% |
0 / 4 |
30.05 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| log | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getCommonFromPsrLevel | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getPsrLevelFromCommon | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | use Psr\Log\LogLevel; |
| 23 | use Psr\Log\AbstractLogger; |
| 24 | |
| 25 | /** |
| 26 | * A wrapper for the old Logger |
| 27 | * |
| 28 | * @access public |
| 29 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 30 | * @package generis |
| 31 | */ |
| 32 | class common_log_Logger2Psr extends AbstractLogger |
| 33 | { |
| 34 | /** |
| 35 | * A map between the loggers |
| 36 | * @var array |
| 37 | */ |
| 38 | private static $map = [ |
| 39 | LogLevel::EMERGENCY => common_Logger::FATAL_LEVEL, |
| 40 | LogLevel::ALERT => common_Logger::FATAL_LEVEL, |
| 41 | LogLevel::CRITICAL => common_Logger::ERROR_LEVEL, |
| 42 | LogLevel::ERROR => common_Logger::ERROR_LEVEL, |
| 43 | LogLevel::WARNING => common_Logger::WARNING_LEVEL, |
| 44 | LogLevel::INFO => common_Logger::INFO_LEVEL, |
| 45 | LogLevel::NOTICE => common_Logger::DEBUG_LEVEL, |
| 46 | LogLevel::DEBUG => common_Logger::DEBUG_LEVEL, |
| 47 | ]; |
| 48 | |
| 49 | /** |
| 50 | * @var array The common_logger level - PSR3 log level conversion. |
| 51 | */ |
| 52 | private static $reverseMap = [ |
| 53 | common_Logger::TRACE_LEVEL => LogLevel::DEBUG, |
| 54 | 't' => LogLevel::DEBUG, |
| 55 | |
| 56 | common_Logger::DEBUG_LEVEL => LogLevel::DEBUG, |
| 57 | 'd' => LogLevel::DEBUG, |
| 58 | |
| 59 | common_Logger::INFO_LEVEL => LogLevel::INFO, |
| 60 | 'i' => LogLevel::INFO, |
| 61 | |
| 62 | common_Logger::WARNING_LEVEL => LogLevel::WARNING, |
| 63 | 'w' => LogLevel::WARNING, |
| 64 | |
| 65 | common_Logger::ERROR_LEVEL => LogLevel::ERROR, |
| 66 | 'e' => LogLevel::ERROR, |
| 67 | |
| 68 | common_Logger::FATAL_LEVEL => LogLevel::CRITICAL, |
| 69 | 'f' => LogLevel::CRITICAL, |
| 70 | ]; |
| 71 | |
| 72 | /** |
| 73 | * @var common_Logger |
| 74 | */ |
| 75 | private $logger; |
| 76 | |
| 77 | public function __construct(common_Logger $logger) |
| 78 | { |
| 79 | $this->logger = $logger; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * (non-PHPdoc) |
| 84 | * @see \Psr\Log\LoggerInterface::log() |
| 85 | */ |
| 86 | public function log($level, $message, array $context = []) |
| 87 | { |
| 88 | $errorLevel = isset(self::$map[$level]) ? self::$map[$level] : common_Logger::ERROR_LEVEL; |
| 89 | $this->logger->log($errorLevel, $message, $context); |
| 90 | } |
| 91 | |
| 92 | public static function getCommonFromPsrLevel($level) |
| 93 | { |
| 94 | if (empty(self::$map[$level])) { |
| 95 | throw new Exception('Invalid error level in Common level to PSR conversion: ' . $level); |
| 96 | } |
| 97 | |
| 98 | return self::$map[$level]; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns the PSR log level based on the common log level. |
| 103 | * |
| 104 | * @param string $level |
| 105 | * |
| 106 | * @return string The PSR log level. |
| 107 | * |
| 108 | * @throws Exception When invalid level was presented. |
| 109 | */ |
| 110 | public static function getPsrLevelFromCommon($level) |
| 111 | { |
| 112 | if (empty(self::$reverseMap[$level])) { |
| 113 | throw new Exception('Invalid error level in PSR to Common level conversion: ' . $level); |
| 114 | } |
| 115 | |
| 116 | return self::$reverseMap[$level]; |
| 117 | } |
| 118 | } |