1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facile\Sentry\Log; |
4
|
|
|
|
5
|
|
|
use Facile\Sentry\Common\Sanitizer\Sanitizer; |
6
|
|
|
use Facile\Sentry\Common\Sanitizer\SanitizerInterface; |
7
|
|
|
use Facile\Sentry\Common\Sender\Sender; |
8
|
|
|
use Facile\Sentry\Common\Sender\SenderInterface; |
9
|
|
|
use Psr\Log\InvalidArgumentException; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Psr\Log\LoggerTrait; |
12
|
|
|
use Psr\Log\LogLevel; |
13
|
|
|
use Raven_Client; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Logger. |
17
|
|
|
*/ |
18
|
|
|
class Logger implements LoggerInterface |
19
|
|
|
{ |
20
|
|
|
use LoggerTrait; |
21
|
|
|
|
22
|
|
|
/** @var Raven_Client */ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
/** @var SenderInterface */ |
26
|
|
|
private $sender; |
27
|
|
|
|
28
|
|
|
/** @var SanitizerInterface */ |
29
|
|
|
private $sanitizer; |
30
|
|
|
|
31
|
|
|
/** @var array */ |
32
|
|
|
protected $psrPriorityMap = [ |
33
|
|
|
LogLevel::EMERGENCY => Raven_Client::FATAL, |
34
|
|
|
LogLevel::ALERT => Raven_Client::ERROR, |
35
|
|
|
LogLevel::CRITICAL => Raven_Client::ERROR, |
36
|
|
|
LogLevel::ERROR => Raven_Client::ERROR, |
37
|
|
|
LogLevel::WARNING => Raven_Client::WARNING, |
38
|
|
|
LogLevel::NOTICE => Raven_Client::INFO, |
39
|
|
|
LogLevel::INFO => Raven_Client::INFO, |
40
|
|
|
LogLevel::DEBUG => Raven_Client::DEBUG, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Logger constructor. |
45
|
|
|
* |
46
|
|
|
* @param Raven_Client $client |
47
|
|
|
* @param SenderInterface|null $sender |
48
|
|
|
* @param SanitizerInterface|null $sanitizer |
49
|
|
|
*/ |
50
|
5 |
|
public function __construct( |
51
|
|
|
Raven_Client $client, |
52
|
|
|
SenderInterface $sender = null, |
53
|
|
|
SanitizerInterface $sanitizer = null |
54
|
|
|
) { |
55
|
5 |
|
$this->client = $client; |
56
|
5 |
|
if (! $sender) { |
57
|
2 |
|
$sender = new Sender($client, $sanitizer); |
58
|
2 |
|
$sender->getStackTrace()->addIgnoreBacktraceNamespace(__NAMESPACE__); |
|
|
|
|
59
|
|
|
} |
60
|
5 |
|
$this->sender = $sender; |
61
|
5 |
|
$this->sanitizer = $sanitizer ?: new Sanitizer(); |
62
|
5 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Logs with an arbitrary level. |
66
|
|
|
* |
67
|
|
|
* @param string $level |
68
|
|
|
* @param string|object $message |
69
|
|
|
* @param array $context |
70
|
|
|
* |
71
|
|
|
* @throws InvalidArgumentException |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
5 |
|
public function log($level, $message, array $context = []): void |
76
|
|
|
{ |
77
|
5 |
|
if (! \array_key_exists($level, $this->psrPriorityMap)) { |
78
|
1 |
|
throw new InvalidArgumentException(\sprintf( |
79
|
1 |
|
'$level must be one of PSR-3 log levels; received %s', |
80
|
1 |
|
\var_export($level, true) |
81
|
|
|
)); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
if (\is_object($message)) { |
85
|
2 |
|
if (! \method_exists($message, '__toString')) { |
86
|
1 |
|
throw new InvalidArgumentException( |
87
|
1 |
|
'$message must implement magic __toString() method' |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
$messageStr = $message->__toString(); |
92
|
|
|
} else { |
93
|
2 |
|
$messageStr = $message; |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
$priority = $this->psrPriorityMap[$level]; |
97
|
3 |
|
$message = $this->interpolate($messageStr, $context); |
98
|
|
|
|
99
|
3 |
|
$this->sender->send($priority, $message, $context); |
100
|
3 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $message |
104
|
|
|
* @param array $context |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
3 |
|
protected function interpolate(string $message, array $context = []): string |
109
|
|
|
{ |
110
|
3 |
|
$replace = []; |
111
|
|
|
/** @var array $context */ |
112
|
3 |
|
$context = $this->sanitizer->sanitize($context); |
113
|
3 |
|
foreach ($context as $key => $val) { |
114
|
3 |
|
if (\is_array($val)) { |
115
|
1 |
|
continue; |
116
|
|
|
} |
117
|
3 |
|
$replace['{' . $key . '}'] = (string) $val; |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
return \strtr($message, $replace); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This method has been deprecated.