Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| VerboseLogger | |
0.00% |
0 / 15 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| log | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| logMessage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getFormattedMessage | |
0.00% |
0 / 4 |
|
0.00% |
0 / 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; |
| 23 | |
| 24 | use Psr\Log\AbstractLogger; |
| 25 | use Psr\Log\LogLevel; |
| 26 | |
| 27 | class VerboseLogger extends AbstractLogger |
| 28 | { |
| 29 | /** |
| 30 | * @var int The position of logger verbosity. |
| 31 | */ |
| 32 | protected $levelPosition; |
| 33 | |
| 34 | /** |
| 35 | * @var array Level priority list. |
| 36 | */ |
| 37 | protected $levels = [ |
| 38 | LogLevel::EMERGENCY, |
| 39 | LogLevel::ALERT, |
| 40 | LogLevel::CRITICAL, |
| 41 | LogLevel::ERROR, |
| 42 | LogLevel::WARNING, |
| 43 | LogLevel::NOTICE, |
| 44 | LogLevel::INFO, |
| 45 | LogLevel::DEBUG, |
| 46 | ]; |
| 47 | |
| 48 | /** |
| 49 | * VerboseLogger constructor. |
| 50 | * |
| 51 | * @param $minimumLevel |
| 52 | * @throws \common_Exception |
| 53 | */ |
| 54 | public function __construct($minimumLevel) |
| 55 | { |
| 56 | if (!in_array($minimumLevel, $this->levels, true)) { |
| 57 | throw new \common_Exception('Level "' . $minimumLevel . '" is not managed by verbose logger'); |
| 58 | } |
| 59 | $this->levelPosition = array_search($minimumLevel, $this->levels); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Log message following minimum level of verbosity |
| 64 | * If $level is bigger than minimum verbosity required |
| 65 | * Set color associated to $level |
| 66 | * |
| 67 | * @param mixed $level |
| 68 | * @param string $message |
| 69 | * @param array $context |
| 70 | */ |
| 71 | public function log($level, $message, array $context = []) |
| 72 | { |
| 73 | $this->logMessage( |
| 74 | $level, |
| 75 | $this->getFormattedMessage($level, $message) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Log message following minimum level of verbosity |
| 81 | * If $level is bigger than minimum verbosity required |
| 82 | * |
| 83 | * @param mixed $level |
| 84 | * @param string $message |
| 85 | */ |
| 86 | protected function logMessage($level, $message) |
| 87 | { |
| 88 | if (array_search($level, $this->levels) > $this->levelPosition) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | echo $message; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the formatted message. |
| 97 | * |
| 98 | * @param $level |
| 99 | * @param $message |
| 100 | * |
| 101 | * @return string |
| 102 | */ |
| 103 | public function getFormattedMessage($level, $message) |
| 104 | { |
| 105 | return '[' . (new \DateTime())->format('Y-m-d H:i') . ']' |
| 106 | . str_pad('[' . $level . ']', 12) |
| 107 | . $message |
| 108 | . PHP_EOL; |
| 109 | } |
| 110 | } |