|
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
|
|
|
/** |
|
23
|
|
|
* @var Raven_Client |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $client; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var SenderInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $sender; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var SanitizerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $sanitizer; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $psrPriorityMap = [ |
|
39
|
|
|
LogLevel::EMERGENCY => Raven_Client::FATAL, |
|
40
|
|
|
LogLevel::ALERT => Raven_Client::ERROR, |
|
41
|
|
|
LogLevel::CRITICAL => Raven_Client::ERROR, |
|
42
|
|
|
LogLevel::ERROR => Raven_Client::ERROR, |
|
43
|
|
|
LogLevel::WARNING => Raven_Client::WARNING, |
|
44
|
|
|
LogLevel::NOTICE => Raven_Client::INFO, |
|
45
|
|
|
LogLevel::INFO => Raven_Client::INFO, |
|
46
|
|
|
LogLevel::DEBUG => Raven_Client::DEBUG, |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
const DEBUG = 'debug'; |
|
50
|
|
|
const INFO = 'info'; |
|
51
|
|
|
const WARN = 'warning'; |
|
52
|
|
|
const WARNING = 'warning'; |
|
53
|
|
|
const ERROR = 'error'; |
|
54
|
|
|
const FATAL = 'fatal'; |
|
55
|
|
|
|
|
56
|
5 |
|
public function __construct( |
|
57
|
|
|
Raven_Client $client, |
|
58
|
|
|
SenderInterface $sender = null, |
|
59
|
|
|
SanitizerInterface $sanitizer = null |
|
60
|
|
|
) { |
|
61
|
5 |
|
$this->client = $client; |
|
62
|
5 |
|
$this->sender = $sender ?: new Sender($client, $sanitizer); |
|
63
|
5 |
|
$this->sanitizer = $sanitizer ?: new Sanitizer(); |
|
64
|
5 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Logs with an arbitrary level. |
|
68
|
|
|
* |
|
69
|
|
|
* @param mixed $level |
|
70
|
|
|
* @param string $message |
|
71
|
|
|
* @param array $context |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
* |
|
75
|
|
|
* @throws InvalidArgumentException |
|
76
|
|
|
*/ |
|
77
|
5 |
|
public function log($level, $message, array $context = []) |
|
78
|
|
|
{ |
|
79
|
5 |
|
if (! array_key_exists($level, $this->psrPriorityMap)) { |
|
80
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
81
|
1 |
|
'$level must be one of PSR-3 log levels; received %s', |
|
82
|
1 |
|
var_export($level, 1) |
|
83
|
|
|
)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
if (is_object($message) && ! method_exists($message, '__toString')) { |
|
87
|
1 |
|
throw new InvalidArgumentException( |
|
88
|
1 |
|
'$message must implement magic __toString() method' |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
3 |
|
$priority = $this->psrPriorityMap[$level]; |
|
93
|
3 |
|
$message = $this->interpolate((string) $message, $context); |
|
94
|
|
|
|
|
95
|
3 |
|
$this->sender->send($priority, $message, $context); |
|
96
|
3 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $message |
|
100
|
|
|
* @param array $context |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
3 |
|
protected function interpolate(string $message, array $context = []): string |
|
105
|
|
|
{ |
|
106
|
3 |
|
$replace = []; |
|
107
|
|
|
/** @var array $context */ |
|
108
|
3 |
|
$context = $this->sanitizer->sanitize($context); |
|
109
|
3 |
|
foreach ($context as $key => $val) { |
|
110
|
3 |
|
if (is_array($val)) { |
|
111
|
1 |
|
continue; |
|
112
|
|
|
} |
|
113
|
3 |
|
$replace['{'.$key.'}'] = (string) $val; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
3 |
|
return strtr($message, $replace); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|