Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.43% |
30 / 42 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| TaoLog | |
71.43% |
30 / 42 |
|
50.00% |
1 / 2 |
16.94 | |
0.00% |
0 / 1 |
| log | |
69.23% |
27 / 39 |
|
0.00% |
0 / 1 |
14.52 | |||
| getDispatcher | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 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) 2017 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\log\logger; |
| 23 | |
| 24 | use oat\oatbox\service\ConfigurableService; |
| 25 | use Psr\Log\LoggerInterface; |
| 26 | use Psr\Log\LoggerTrait; |
| 27 | |
| 28 | /** |
| 29 | * Class TaoLog |
| 30 | * |
| 31 | * Tao internal appenders to store logs |
| 32 | * |
| 33 | * @package oat\oatbox\log\logger |
| 34 | * @deprecated This wrapper is for backward compatibility purpose, use TaoMonolog for newer logger |
| 35 | */ |
| 36 | class TaoLog extends ConfigurableService implements LoggerInterface |
| 37 | { |
| 38 | use LoggerTrait; |
| 39 | |
| 40 | public const OPTION_APPENDERS = 'appenders'; |
| 41 | |
| 42 | /** @var \common_log_Dispatcher */ |
| 43 | private $dispatcher; |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * @param mixed $level |
| 48 | * @param string $message |
| 49 | * @param array $context |
| 50 | * @throws \Exception |
| 51 | */ |
| 52 | public function log($level, $message, array $context = []) |
| 53 | { |
| 54 | if (isset($context['trace'])) { |
| 55 | $stack = $context['trace']; |
| 56 | unset($context['trace']); |
| 57 | } else { |
| 58 | $stack = defined('DEBUG_BACKTRACE_IGNORE_ARGS') |
| 59 | ? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) |
| 60 | : debug_backtrace(false); |
| 61 | array_shift($stack); |
| 62 | } |
| 63 | |
| 64 | $current = isset($stack[2]) ? $stack[2] : end($stack); |
| 65 | $current = array_merge($current, $context); |
| 66 | |
| 67 | if (isset($current['file'], $current['line'])) { |
| 68 | $errorFile = $current['file']; |
| 69 | $errorLine = $current['line']; |
| 70 | } elseif (isset($current['class'], $context['function'])) { |
| 71 | $errorFile = $context['class']; |
| 72 | $errorLine = $context['function']; |
| 73 | } else { |
| 74 | $errorFile = $errorLine = 'undefined'; |
| 75 | } |
| 76 | |
| 77 | if (PHP_SAPI !== 'cli') { |
| 78 | $requestURI = $_SERVER['REQUEST_URI']; |
| 79 | } else { |
| 80 | $requestURI = implode(' ', $_SERVER['argv']); |
| 81 | } |
| 82 | |
| 83 | //reformat input |
| 84 | if (is_object($message)) { |
| 85 | $message = 'Message is object of type ' . gettype($message); |
| 86 | |
| 87 | //show content of logged object only from debug level |
| 88 | if ($level <= \common_Logger::DEBUG_LEVEL) { |
| 89 | $message .= ' : ' . PHP_EOL . var_export($message, true); |
| 90 | } |
| 91 | //same for arrays |
| 92 | } elseif (is_array($message) && $level <= \common_Logger::DEBUG_LEVEL) { |
| 93 | $message = 'Message is an array : ' . PHP_EOL . var_export($message, true); |
| 94 | } else { |
| 95 | $message = (string) $message; |
| 96 | } |
| 97 | $level = \common_log_Logger2Psr::getCommonFromPsrLevel($level); |
| 98 | $this->getDispatcher()->log( |
| 99 | new \common_log_Item( |
| 100 | $message, |
| 101 | $level, |
| 102 | time(), |
| 103 | $stack, |
| 104 | $context, |
| 105 | $requestURI, |
| 106 | $errorFile, |
| 107 | $errorLine |
| 108 | ) |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns the dispatcher |
| 114 | * |
| 115 | * @return Appender |
| 116 | */ |
| 117 | private function getDispatcher() |
| 118 | { |
| 119 | if (is_null($this->dispatcher)) { |
| 120 | $this->dispatcher = new \common_log_Dispatcher($this->getOption(self::OPTION_APPENDERS)); |
| 121 | } |
| 122 | return $this->dispatcher; |
| 123 | } |
| 124 | } |