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
|
|
|
|