Passed
Push — master ( b0f5d7...c171ec )
by Sergey
02:30
created

PsrLogger::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 3
1
<?php
2
3
namespace PhpConsole;
4
5
use Psr\Log\AbstractLogger;
6
use Psr\Log\InvalidArgumentException;
7
use Psr\Log\LogLevel;
8
9
/**
10
 * Implementation of PSR-3 logger interface https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
11
 * IMPORTANT: https://github.com/php-fig/log must be installed & autoloaded
12
 *
13
 * @package PhpConsole
14
 * @version 3.1
15
 * @link http://consle.com
16
 * @author Sergey Barbushin http://linkedin.com/in/barbushin
17
 * @copyright © Sergey Barbushin, 2011-2013. All rights reserved.
18
 * @license http://www.opensource.org/licenses/BSD-3-Clause "The BSD 3-Clause License"
19
 */
20
class PsrLogger extends AbstractLogger {
21
22
	public static $debugLevels = array(
23
		LogLevel::NOTICE => 'notice',
24
		LogLevel::INFO => 'info',
25
		LogLevel::DEBUG => 'debug',
26
	);
27
28
	public static $errorsLevels = array(
29
		LogLevel::EMERGENCY => 'PSR_EMERGENCY',
30
		LogLevel::ALERT => 'PSR_ALERT',
31
		LogLevel::CRITICAL => 'PSR_CRITICAL',
32
		LogLevel::ERROR => 'PSR_ERROR',
33
		LogLevel::WARNING => 'PSR_WARNING',
34
	);
35
36
	/** @var  Connector */
37
	protected $connector;
38
	/** @var  Dumper */
39
	protected $contextDumper;
40
	protected $ignoreTraceCalls;
41
42
	/**
43
	 * @param Connector|null $connector
44
	 * @param Dumper|null $contextDumper
45
	 * @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
46
	 */
47 15
	public function __construct(Connector $connector = null, Dumper $contextDumper = null, $ignoreTraceCalls = 1) {
48 15
		$this->connector = $connector ?: Connector::getInstance();
49 15
		$this->contextDumper = $contextDumper ?: $this->connector->getDumper();
50 15
		$this->ignoreTraceCalls = $ignoreTraceCalls;
51 15
	}
52
53
	/**
54
	 * Logs with an arbitrary level.
55
	 *
56
	 */
57 14
	public function log($level, $message, array $context = array()) {
58 14
		if(is_object($message) && is_callable($message, '__toString')) {
59
			$message = (string)$message;
60
		}
61 14
		$message = $this->fetchMessageContext($message, $context);
62
63 14
		if(isset(static::$debugLevels[$level])) {
64 5
			$this->connector->getDebugDispatcher()->dispatchDebug($message, static::$debugLevels[$level], $this->ignoreTraceCalls);
65
		}
66 9
		elseif(isset(static::$errorsLevels[$level])) {
67 8
			if(isset($context['exception']) && ($context['exception'] instanceof \Exception || $context['exception'] instanceof \Throwable)) {
68 1
				$this->connector->getErrorsDispatcher()->dispatchException($context['exception']);
69
			}
70
			else {
71 8
				$this->connector->getErrorsDispatcher()->dispatchError(static::$errorsLevels[$level], $message, null, null, $this->ignoreTraceCalls);
72
			}
73
		}
74
		else {
75 1
			throw new InvalidArgumentException('Unknown log level "' . $level . '"');
76
		}
77 13
	}
78
79 14
	protected function fetchMessageContext($message, array $context) {
80 14
		$replace = array();
81 14
		foreach($context as $key => $value) {
82 12
			$replace['{' . $key . '}'] = $this->contextDumper->dump($value);
83
		}
84 14
		return strtr($message, $replace);
85
	}
86
}
87