Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ColoredVerboseLogger | |
0.00% |
0 / 13 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
| log | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| getLevelColor | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getDefaultColor | |
0.00% |
0 / 1 |
|
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 ColoredVerboseLogger extends VerboseLogger |
| 28 | { |
| 29 | /** |
| 30 | * @var array List of colors associated to a level |
| 31 | */ |
| 32 | protected $colors = [ |
| 33 | LogLevel::EMERGENCY => '1;31', // red |
| 34 | LogLevel::ALERT => '1;31', // red |
| 35 | LogLevel::CRITICAL => '1;31', // red |
| 36 | LogLevel::ERROR => '1;31', // red |
| 37 | LogLevel::WARNING => '1;33', // yellow |
| 38 | LogLevel::NOTICE => '1;34', // light blue |
| 39 | LogLevel::INFO => '0;32', // green |
| 40 | LogLevel::DEBUG => '0;37', // light grey |
| 41 | ]; |
| 42 | |
| 43 | /** |
| 44 | * Log message following minimum level of verbosity |
| 45 | * If $level is bigger than minimum verbosity required |
| 46 | * Set color associated to $level |
| 47 | * |
| 48 | * @param mixed $level |
| 49 | * @param string $message |
| 50 | * @param array $context |
| 51 | */ |
| 52 | public function log($level, $message, array $context = []) |
| 53 | { |
| 54 | $this->logMessage( |
| 55 | $level, |
| 56 | ( |
| 57 | $this->getLevelColor($level) |
| 58 | . $this->getFormattedMessage($level, $message) |
| 59 | . $this->getDefaultColor() |
| 60 | ) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get the CLI color associated to given $level |
| 66 | * |
| 67 | * @param $level |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | protected function getLevelColor($level) |
| 72 | { |
| 73 | if (array_key_exists($level, $this->colors)) { |
| 74 | return "\033[" . $this->colors[$level] . 'm'; |
| 75 | } else { |
| 76 | return $this->getDefaultColor(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Set the default CLI color e.q. dark grey |
| 82 | * |
| 83 | * @return string |
| 84 | */ |
| 85 | protected function getDefaultColor() |
| 86 | { |
| 87 | return "\033[0m"; // Dark grey |
| 88 | } |
| 89 | } |