Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
32.63% |
31 / 95 |
|
27.78% |
5 / 18 |
CRAP | |
0.00% |
0 / 1 |
| common_Logger | |
32.63% |
31 / 95 |
|
27.78% |
5 / 18 |
722.41 | |
0.00% |
0 / 1 |
| singleton | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| log | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 | |||
| enable | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| disable | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| restore | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| t | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| d | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| i | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| w | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| e | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addTrace | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| f | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handleException | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| handlePHPErrors | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
240 | |||
| handlePHPShutdown | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| getContext | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
3.03 | |||
| 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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * 2013 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Abstraction for the System Logger |
| 30 | * |
| 31 | * @access public |
| 32 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 33 | * @package generis |
| 34 | */ |
| 35 | class common_Logger |
| 36 | { |
| 37 | use \oat\oatbox\log\LoggerAwareTrait; |
| 38 | |
| 39 | /** |
| 40 | * whenever or not the Logger is enabled |
| 41 | * |
| 42 | * @access private |
| 43 | * @var boolean |
| 44 | */ |
| 45 | private $enabled = true; |
| 46 | |
| 47 | /** |
| 48 | * a history of past states, to allow a restoration of the previous state |
| 49 | * |
| 50 | * @access private |
| 51 | * @var array |
| 52 | */ |
| 53 | private $stateStack = []; |
| 54 | |
| 55 | /** |
| 56 | * instance of the class Logger, to implement the singleton pattern |
| 57 | * |
| 58 | * @access private |
| 59 | * @var Logger |
| 60 | */ |
| 61 | private static $instance = null; |
| 62 | |
| 63 | /** |
| 64 | * The dispatcher of the Logger |
| 65 | * |
| 66 | * @access private |
| 67 | * @var Appender |
| 68 | */ |
| 69 | private $dispatcher = null; |
| 70 | |
| 71 | /** |
| 72 | * the lowest level of events representing the finest-grained processes |
| 73 | * |
| 74 | * @access public |
| 75 | * @var int |
| 76 | */ |
| 77 | public const TRACE_LEVEL = 0; |
| 78 | |
| 79 | /** |
| 80 | * the level of events representing fine grained informations for debugging |
| 81 | * |
| 82 | * @access public |
| 83 | * @var int |
| 84 | */ |
| 85 | public const DEBUG_LEVEL = 1; |
| 86 | |
| 87 | /** |
| 88 | * the level of information events that represent high level system events |
| 89 | * |
| 90 | * @access public |
| 91 | * @var int |
| 92 | */ |
| 93 | public const INFO_LEVEL = 2; |
| 94 | |
| 95 | /** |
| 96 | * the level of warning events that represent potential problems |
| 97 | * |
| 98 | * @access public |
| 99 | * @var int |
| 100 | */ |
| 101 | public const WARNING_LEVEL = 3; |
| 102 | |
| 103 | /** |
| 104 | * the level of error events that allow the system to continue |
| 105 | * |
| 106 | * @access public |
| 107 | * @var int |
| 108 | */ |
| 109 | public const ERROR_LEVEL = 4; |
| 110 | |
| 111 | /** |
| 112 | * the level of very severe error events that prevent the system to continue |
| 113 | * |
| 114 | * @access public |
| 115 | * @var int |
| 116 | */ |
| 117 | public const FATAL_LEVEL = 5; |
| 118 | |
| 119 | public const CONTEXT_ERROR_FILE = 'file'; |
| 120 | |
| 121 | public const CONTEXT_ERROR_LINE = 'line'; |
| 122 | |
| 123 | public const CONTEXT_TRACE = 'trace'; |
| 124 | |
| 125 | public const CONTEXT_EXCEPTION = 'exception'; |
| 126 | |
| 127 | /** |
| 128 | * Warnings that are acceptable in our projects |
| 129 | * invoked by the way generis/tao use abstract functions |
| 130 | * |
| 131 | * @access private |
| 132 | * @var array |
| 133 | */ |
| 134 | private $ACCEPTABLE_WARNINGS = []; |
| 135 | |
| 136 | // --- OPERATIONS --- |
| 137 | |
| 138 | /** |
| 139 | * returns the existing Logger instance or instantiates a new one |
| 140 | * |
| 141 | * @access public |
| 142 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 143 | * @return common_Logger |
| 144 | */ |
| 145 | public static function singleton() |
| 146 | { |
| 147 | if (is_null(self::$instance)) { |
| 148 | self::$instance = new self(); |
| 149 | } |
| 150 | return self::$instance; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Private constructor for singleton pattern |
| 155 | * |
| 156 | * @access private |
| 157 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 158 | * @return mixed |
| 159 | */ |
| 160 | private function __construct() |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * register the logger as errorhandler |
| 166 | * and shutdown function |
| 167 | * |
| 168 | * @access public |
| 169 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 170 | * @return mixed |
| 171 | */ |
| 172 | public function register() |
| 173 | { |
| 174 | // initialize the logger here to prevent fatal errors that occure if: |
| 175 | // while autoloading any class, an error gets thrown |
| 176 | // the logger initializes to handle this error, and failes to autoload his files |
| 177 | set_error_handler([$this, 'handlePHPErrors']); |
| 178 | register_shutdown_function([$this, 'handlePHPShutdown']); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Short description of method log |
| 183 | * |
| 184 | * @access public |
| 185 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 186 | * @param int $level |
| 187 | * @param string $message |
| 188 | * @param array $tags |
| 189 | * @return mixed |
| 190 | */ |
| 191 | public function log($level, $message, $tags = []) |
| 192 | { |
| 193 | if ($this->enabled) { |
| 194 | $this->disable(); |
| 195 | try { |
| 196 | if (defined('CONFIG_PATH')) { |
| 197 | // Gets the log context. |
| 198 | $context = $this->getContext(); |
| 199 | $context = array_merge($context, $tags); |
| 200 | if (!empty($context['file']) && !empty($context['line'])) { |
| 201 | $tags['file'] = $context['file']; |
| 202 | $tags['line'] = $context['line']; |
| 203 | } |
| 204 | |
| 205 | $this->getLogger()->log(common_log_Logger2Psr::getPsrLevelFromCommon($level), $message, $tags); |
| 206 | } |
| 207 | } catch (\Exception $e) { |
| 208 | // Unable to use the logger service to retrieve the logger |
| 209 | } |
| 210 | $this->restore(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * enables the logger, should not be used to restore a previous logger state |
| 216 | * |
| 217 | * @access public |
| 218 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 219 | * @return mixed |
| 220 | */ |
| 221 | public function enable() |
| 222 | { |
| 223 | |
| 224 | $this->stateStack[] = self::singleton()->enabled; |
| 225 | $this->enabled = true; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * disables the logger, should not be used to restore a previous logger |
| 230 | * |
| 231 | * @access public |
| 232 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 233 | * @return mixed |
| 234 | */ |
| 235 | public function disable() |
| 236 | { |
| 237 | |
| 238 | $this->stateStack[] = self::singleton()->enabled; |
| 239 | $this->enabled = false; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * restores the logger after its state was modified by enable() or disable() |
| 244 | * |
| 245 | * @access public |
| 246 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 247 | * @return mixed |
| 248 | */ |
| 249 | public function restore() |
| 250 | { |
| 251 | |
| 252 | if (count($this->stateStack) > 0) { |
| 253 | $this->enabled = array_pop($this->stateStack); |
| 254 | } else { |
| 255 | self::e("Tried to restore Log state that was never changed"); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * trace logs finest-grained processes informations |
| 261 | * |
| 262 | * @access public |
| 263 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 264 | * @param string $message |
| 265 | * @param array $tags |
| 266 | * @return mixed |
| 267 | */ |
| 268 | public static function t($message, $tags = []) |
| 269 | { |
| 270 | |
| 271 | self::singleton()->log(self::TRACE_LEVEL, $message, $tags); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * debug logs fine grained informations for debugging |
| 276 | * |
| 277 | * @access public |
| 278 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 279 | * @param string $message |
| 280 | * @param array $tags |
| 281 | * @return mixed |
| 282 | */ |
| 283 | public static function d($message, $tags = []) |
| 284 | { |
| 285 | |
| 286 | self::singleton()->log(self::DEBUG_LEVEL, $message, $tags); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | |
| 291 | /** |
| 292 | * info logs high level system events |
| 293 | * |
| 294 | * @access public |
| 295 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 296 | * @param string $message |
| 297 | * @param array $tags |
| 298 | * @return mixed |
| 299 | */ |
| 300 | public static function i($message, $tags = []) |
| 301 | { |
| 302 | |
| 303 | self::singleton()->log(self::INFO_LEVEL, $message, $tags); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * warning logs events that represent potential problems |
| 308 | * |
| 309 | * @access public |
| 310 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 311 | * @param string $message |
| 312 | * @param array $tags |
| 313 | * @return mixed |
| 314 | */ |
| 315 | public static function w($message, $tags = []) |
| 316 | { |
| 317 | |
| 318 | self::singleton()->log(self::WARNING_LEVEL, $message, $tags); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * error logs events that allow the system to continue |
| 323 | * |
| 324 | * @access public |
| 325 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 326 | * @param string $message |
| 327 | * @param array $tags |
| 328 | * @return mixed |
| 329 | */ |
| 330 | public static function e($message, $tags = []) |
| 331 | { |
| 332 | self::singleton()->log(self::ERROR_LEVEL, $message, self::addTrace($tags)); |
| 333 | } |
| 334 | |
| 335 | private static function addTrace(array $tags = []) |
| 336 | { |
| 337 | if (!isset($tags[self::CONTEXT_TRACE])) { |
| 338 | $trace = defined('DEBUG_BACKTRACE_IGNORE_ARGS') |
| 339 | ? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) |
| 340 | : debug_backtrace(false); |
| 341 | |
| 342 | // remove 2 last traces which are error handler itself. to make logs more readable and reduce size of a log |
| 343 | $tags[self::CONTEXT_TRACE] = array_slice($trace, 2); |
| 344 | } |
| 345 | |
| 346 | return $tags; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * fatal logs very severe error events that prevent the system to continue |
| 351 | * |
| 352 | * @access public |
| 353 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 354 | * @param string $message |
| 355 | * @param array $tags |
| 356 | * @return mixed |
| 357 | */ |
| 358 | public static function f($message, $tags = []) |
| 359 | { |
| 360 | self::singleton()->log(self::FATAL_LEVEL, $message, self::addTrace($tags)); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Short description of method handleException |
| 365 | * |
| 366 | * @access public |
| 367 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 368 | * @param Exception $exception |
| 369 | */ |
| 370 | public function handleException(Exception $exception) |
| 371 | { |
| 372 | $severity = method_exists($exception, 'getSeverity') ? $exception->getSeverity() : self::ERROR_LEVEL; |
| 373 | self::singleton() |
| 374 | ->log( |
| 375 | $severity, |
| 376 | $exception->getMessage(), |
| 377 | [ |
| 378 | self::CONTEXT_EXCEPTION => get_class($exception), |
| 379 | self::CONTEXT_ERROR_FILE => $exception->getFile(), |
| 380 | self::CONTEXT_ERROR_LINE => $exception->getLine(), |
| 381 | self::CONTEXT_TRACE => $exception->getTrace() |
| 382 | ] |
| 383 | ); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * A handler for php errors, should never be called manually |
| 388 | * |
| 389 | * @access public |
| 390 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 391 | * |
| 392 | * @param int $errorNumber |
| 393 | * @param string $errorString |
| 394 | * @param string $errorFile |
| 395 | * @param mixed $errorLine |
| 396 | * @param array $errorContext |
| 397 | * |
| 398 | * @return boolean |
| 399 | */ |
| 400 | public function handlePHPErrors( |
| 401 | $errorNumber, |
| 402 | $errorString, |
| 403 | $errorFile = null, |
| 404 | $errorLine = null, |
| 405 | $errorContext = [] |
| 406 | ) { |
| 407 | if (error_reporting() !== 0) { |
| 408 | if ($errorNumber === E_STRICT) { |
| 409 | foreach ($this->ACCEPTABLE_WARNINGS as $pattern) { |
| 410 | if (preg_match($pattern, $errorString) > 0) { |
| 411 | return false; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | switch ($errorNumber) { |
| 417 | case E_USER_ERROR: |
| 418 | case E_RECOVERABLE_ERROR: |
| 419 | $severity = self::FATAL_LEVEL; |
| 420 | break; |
| 421 | case E_WARNING: |
| 422 | case E_USER_WARNING: |
| 423 | $severity = self::ERROR_LEVEL; |
| 424 | break; |
| 425 | case E_NOTICE: |
| 426 | case E_USER_NOTICE: |
| 427 | $severity = self::WARNING_LEVEL; |
| 428 | break; |
| 429 | case E_DEPRECATED: |
| 430 | case E_USER_DEPRECATED: |
| 431 | case E_STRICT: |
| 432 | $severity = self::DEBUG_LEVEL; |
| 433 | break; |
| 434 | default: |
| 435 | self::d('Unsupported PHP error type: ' . $errorNumber, 'common_Logger'); |
| 436 | $severity = self::ERROR_LEVEL; |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | self::singleton()->log( |
| 441 | $severity, |
| 442 | sprintf('php error(%s): %s', $errorNumber, $errorString), |
| 443 | [ |
| 444 | 'PHPERROR', |
| 445 | self::CONTEXT_ERROR_FILE => $errorFile, |
| 446 | self::CONTEXT_ERROR_LINE => $errorLine, |
| 447 | self::CONTEXT_TRACE => self::addTrace() |
| 448 | ] |
| 449 | ); |
| 450 | } |
| 451 | |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * a workaround to catch fatal errors by handling the php shutdown, |
| 457 | * should never be called manually |
| 458 | * |
| 459 | * @access public |
| 460 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 461 | * @return mixed |
| 462 | */ |
| 463 | public function handlePHPShutdown() |
| 464 | { |
| 465 | $error = error_get_last(); |
| 466 | |
| 467 | if ($error !== null && ($error['type'] & (E_COMPILE_ERROR | E_ERROR | E_PARSE | E_CORE_ERROR)) !== 0) { |
| 468 | $msg = (isset($error['file'], $error['line'])) |
| 469 | ? 'php error(' . $error['type'] . ') in ' . $error['file'] . '@' . $error['line'] . ': ' |
| 470 | . $error['message'] |
| 471 | : 'php error(' . $error['type'] . '): ' . $error['message']; |
| 472 | self::singleton()->log(self::FATAL_LEVEL, $msg, ['PHPERROR']); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Returns the calling context. |
| 478 | * |
| 479 | * @return array |
| 480 | */ |
| 481 | protected function getContext() |
| 482 | { |
| 483 | $trace = debug_backtrace(); |
| 484 | |
| 485 | $file = isset($trace[2]['file']) |
| 486 | ? $trace[2]['file'] |
| 487 | : '' |
| 488 | ; |
| 489 | |
| 490 | $line = isset($trace[2]['line']) |
| 491 | ? $trace[2]['line'] |
| 492 | : '' |
| 493 | ; |
| 494 | |
| 495 | return [ |
| 496 | 'file' => $file, |
| 497 | 'line' => $line, |
| 498 | ]; |
| 499 | } |
| 500 | } |