Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 9 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| VerboseLoggerFactory | |
0.00% |
0 / 9 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getInstance | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| getLogLevel | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 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\LogLevel; |
| 25 | |
| 26 | class VerboseLoggerFactory |
| 27 | { |
| 28 | /** |
| 29 | * @var array The verbose parameters and the connected log levels. |
| 30 | */ |
| 31 | private static $levels = [ |
| 32 | '-vvvv' => LogLevel::DEBUG, |
| 33 | '--verbose 4' => LogLevel::DEBUG, |
| 34 | |
| 35 | '-vvv' => LogLevel::INFO, |
| 36 | '--verbose 3' => LogLevel::INFO, |
| 37 | |
| 38 | '-vv' => LogLevel::NOTICE, |
| 39 | '--verbose 2' => LogLevel::NOTICE, |
| 40 | |
| 41 | '-v' => LogLevel::ERROR, |
| 42 | '--verbose 1' => LogLevel::ERROR, |
| 43 | ]; |
| 44 | |
| 45 | /** |
| 46 | * Make sure it can't be instantiated. |
| 47 | */ |
| 48 | private function __construct() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the configured instance of the verbose logger based on the arguments. |
| 54 | * |
| 55 | * @param array $arguments The cli arguments. |
| 56 | * |
| 57 | * @return ColoredVerboseLogger|VerboseLogger |
| 58 | */ |
| 59 | public static function getInstance(array $arguments) |
| 60 | { |
| 61 | if (in_array('-nc', $arguments) || in_array('--no-color', $arguments)) { |
| 62 | return new VerboseLogger(static::getLogLevel($arguments)); |
| 63 | } |
| 64 | |
| 65 | return new ColoredVerboseLogger(static::getLogLevel($arguments)); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the current log level based on the verbosity parameters. |
| 70 | * |
| 71 | * @param array $arguments |
| 72 | * |
| 73 | * @return string Current log level or as default LogLevel::ERROR |
| 74 | */ |
| 75 | protected static function getLogLevel(array $arguments) |
| 76 | { |
| 77 | $argumentsForSearch = implode(' ', $arguments); |
| 78 | foreach (static::$levels as $argumentPattern => $currentLevel) { |
| 79 | if (strpos($argumentsForSearch, $argumentPattern) !== false) { |
| 80 | return $currentLevel; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return LogLevel::ERROR; |
| 85 | } |
| 86 | } |