Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 70 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| TaoMonolog | |
0.00% |
0 / 70 |
|
0.00% |
0 / 6 |
992 | |
0.00% |
0 / 1 |
| log | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| buildLogger | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
| buildHandler | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
90 | |||
| buildProcessor | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| buildFormatter | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
56 | |||
| buildObject | |
0.00% |
0 / 4 |
|
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) 2018 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\log\logger; |
| 23 | |
| 24 | use Monolog\Formatter\FormatterInterface; |
| 25 | use Monolog\Handler\HandlerInterface; |
| 26 | use Monolog\Logger; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use Psr\Log\LoggerInterface; |
| 29 | use Psr\Log\LoggerTrait; |
| 30 | |
| 31 | /** |
| 32 | * Class TaoMonolog |
| 33 | * |
| 34 | * A wrapper to acces monolog from tao platform |
| 35 | * Build the logger from configuration with handlers |
| 36 | * - see generis/config/header/log.conf.php |
| 37 | * |
| 38 | * @package oat\oatbox\log\logger |
| 39 | */ |
| 40 | class TaoMonolog extends ConfigurableService implements LoggerInterface |
| 41 | { |
| 42 | use LoggerTrait; |
| 43 | |
| 44 | public const HANDLERS_OPTION = 'handlers'; |
| 45 | |
| 46 | /** @var Logger null */ |
| 47 | protected $logger = null; |
| 48 | |
| 49 | /** |
| 50 | * @param mixed $level |
| 51 | * @param string $message |
| 52 | * @param array $context |
| 53 | * @throws \common_configuration_ComponentFactoryException |
| 54 | */ |
| 55 | public function log($level, $message, array $context = []) |
| 56 | { |
| 57 | if (is_null($this->logger)) { |
| 58 | $this->logger = $this->buildLogger(); |
| 59 | } |
| 60 | |
| 61 | $this->logger->log($level, $message, $context); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @return Logger |
| 66 | * @throws \common_configuration_ComponentFactoryException |
| 67 | */ |
| 68 | protected function buildLogger() |
| 69 | { |
| 70 | $logger = new Logger($this->getOption('name')); |
| 71 | |
| 72 | if ($this->hasOption(self::HANDLERS_OPTION)) { |
| 73 | foreach ($this->getOption(self::HANDLERS_OPTION) as $handlerOptions) { |
| 74 | $logger->pushHandler($this->buildHandler($handlerOptions)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if ($this->hasOption('processors')) { |
| 79 | $processorsOptions = $this->getOption('processors'); |
| 80 | if (!is_array($processorsOptions)) { |
| 81 | throw new \common_configuration_ComponentFactoryException( |
| 82 | 'Handler processors options as to be formatted as array' |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | foreach ($processorsOptions as $processorsOption) { |
| 87 | $logger->pushProcessor($this->buildProcessor($processorsOption)); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return $logger; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param array $options |
| 96 | * @return HandlerInterface |
| 97 | * @throws \common_configuration_ComponentFactoryException |
| 98 | */ |
| 99 | protected function buildHandler(array $options) |
| 100 | { |
| 101 | if (!isset($options['class'])) { |
| 102 | throw new \common_configuration_ComponentFactoryException( |
| 103 | 'Handler options has to contain a class attribute.' |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | if (!is_a($options['class'], HandlerInterface::class, true)) { |
| 108 | throw new \common_configuration_ComponentFactoryException( |
| 109 | 'Handler class option has to be a HandlerInterface.' |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | $handlerOptions = []; |
| 114 | if (isset($options['options'])) { |
| 115 | $handlerOptions = is_array($options['options']) ? $options['options'] : [$options['options']]; |
| 116 | } |
| 117 | /** @var HandlerInterface $handler */ |
| 118 | $handler = $this->buildObject($options['class'], $handlerOptions); |
| 119 | |
| 120 | if (isset($options['processors'])) { |
| 121 | $processorsOptions = $options['processors']; |
| 122 | if (!is_array($processorsOptions)) { |
| 123 | throw new \common_configuration_ComponentFactoryException( |
| 124 | 'Handler processors options as to be formatted as array' |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | foreach ($processorsOptions as $processorsOption) { |
| 129 | $handler->pushProcessor($this->buildProcessor($processorsOption)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (isset($options['formatter'])) { |
| 134 | $handler->setFormatter($this->buildFormatter($options['formatter'])); |
| 135 | } |
| 136 | |
| 137 | return $handler; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param $options |
| 142 | * @return callable |
| 143 | * @throws \common_configuration_ComponentFactoryException |
| 144 | */ |
| 145 | protected function buildProcessor($options) |
| 146 | { |
| 147 | if (is_object($options)) { |
| 148 | return $options; |
| 149 | } else { |
| 150 | if (!isset($options['class'])) { |
| 151 | throw new \common_configuration_ComponentFactoryException( |
| 152 | 'Processor options has to contain a class attribute.' |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | $processorOptions = []; |
| 157 | if (isset($options['options'])) { |
| 158 | $processorOptions = is_array($options['options']) ? $options['options'] : [$options['options']]; |
| 159 | } |
| 160 | |
| 161 | return $this->buildObject($options['class'], $processorOptions); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @param $options |
| 167 | * @return FormatterInterface |
| 168 | * @throws \common_configuration_ComponentFactoryException |
| 169 | */ |
| 170 | protected function buildFormatter($options) |
| 171 | { |
| 172 | if (is_object($options)) { |
| 173 | if (!is_a($options, FormatterInterface::class)) { |
| 174 | throw new \common_configuration_ComponentFactoryException('Formatter has to be a FormatterInterface.'); |
| 175 | } |
| 176 | return $options; |
| 177 | } else { |
| 178 | if (!isset($options['class'])) { |
| 179 | throw new \common_configuration_ComponentFactoryException( |
| 180 | 'Formatter options has to contain a class attribute.' |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | if (!is_a($options['class'], FormatterInterface::class, true)) { |
| 185 | throw new \common_configuration_ComponentFactoryException( |
| 186 | 'Formatter class option has to be a FormatterInterface.' |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | $formatterOptions = []; |
| 191 | if (isset($options['options'])) { |
| 192 | $formatterOptions = is_array($options['options']) ? $options['options'] : [$options['options']]; |
| 193 | } |
| 194 | |
| 195 | return $this->buildObject($options['class'], $formatterOptions); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @param $className |
| 201 | * @param array $args |
| 202 | * @return object |
| 203 | * @throws \common_configuration_ComponentFactoryException |
| 204 | */ |
| 205 | protected function buildObject($className, array $args) |
| 206 | { |
| 207 | try { |
| 208 | $class = new \ReflectionClass($className); |
| 209 | return $class->newInstanceArgs($args); |
| 210 | } catch (\ReflectionException $e) { |
| 211 | throw new \common_configuration_ComponentFactoryException('Unable to create object for logger', 0, $e); |
| 212 | } |
| 213 | } |
| 214 | } |