1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dasao\SentryLogger\Log; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Raven_Client as SentryClient; |
7
|
|
|
use Throwable; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class SentryLogger |
11
|
|
|
* |
12
|
|
|
* PHP Version 7 |
13
|
|
|
* |
14
|
|
|
* @category PHP |
15
|
|
|
* @package Dasao\SentryLogger\Log |
16
|
|
|
* @author Dasao <[email protected]> |
17
|
|
|
* @copyright 2014-2017 Dasao |
18
|
|
|
* @license Proprietary http://www.das-ao.com |
19
|
|
|
*/ |
20
|
|
|
class SentryLogger implements SentryLoggerInterface |
21
|
|
|
{ |
22
|
|
|
/** @var SentryClient */ |
23
|
|
|
protected $sentryClient; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* SentryLogger constructor. |
27
|
|
|
* |
28
|
|
|
* @param SentryClient $sentryClient The sentry client. |
29
|
|
|
*/ |
30
|
|
|
public function __construct(SentryClient $sentryClient) |
31
|
|
|
{ |
32
|
|
|
$this->sentryClient = $sentryClient; |
33
|
|
|
$this->sentryClient->install(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Action must be taken immediately. |
38
|
|
|
* |
39
|
|
|
* @param string $message The message to log. |
40
|
|
|
* @param array $context The context data. |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function alert($message, array $context = []) |
45
|
|
|
{ |
46
|
|
|
$this->sentryClient->captureMessage($message, [ |
47
|
|
|
'extra' => $context, |
48
|
|
|
'level' => 'error', |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Critical conditions. |
54
|
|
|
* |
55
|
|
|
* @param string $message The message to log. |
56
|
|
|
* @param array $context The context data. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function critical($message, array $context = []) |
61
|
|
|
{ |
62
|
|
|
$this->sentryClient->captureMessage($message, [ |
63
|
|
|
'extra' => $context, |
64
|
|
|
'level' => 'fatal', |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Detailed debug information. |
70
|
|
|
* |
71
|
|
|
* @param string $message The message to log. |
72
|
|
|
* @param array $context The context data. |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function debug($message, array $context = []) |
77
|
|
|
{ |
78
|
|
|
$this->sentryClient->captureMessage($message, [ |
79
|
|
|
'extra' => $context, |
80
|
|
|
'level' => 'debug', |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* System is unusable. |
86
|
|
|
* |
87
|
|
|
* @param string $message The message to log. |
88
|
|
|
* @param array $context The context data. |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function emergency($message, array $context = []) |
93
|
|
|
{ |
94
|
|
|
$this->sentryClient->captureMessage($message, [ |
95
|
|
|
'extra' => $context, |
96
|
|
|
'level' => 'fatal', |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Runtime errors that do not require immediate action but should typically |
102
|
|
|
* be logged and monitored. |
103
|
|
|
* |
104
|
|
|
* @param string $message The message to log. |
105
|
|
|
* @param array $context The context data. |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function error($message, array $context = []) |
110
|
|
|
{ |
111
|
|
|
$this->sentryClient->captureMessage($message, [ |
112
|
|
|
'extra' => $context, |
113
|
|
|
'level' => 'error', |
114
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Log the exception. |
119
|
|
|
* |
120
|
|
|
* @param Throwable|Exception $exception The Exception to log. |
121
|
|
|
* @param array $context The context data. |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function exception($exception, array $context = []) |
126
|
|
|
{ |
127
|
|
|
$this->sentryClient->captureException($exception, $context); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Interesting events. |
132
|
|
|
* |
133
|
|
|
* @param string $message The message to log. |
134
|
|
|
* @param array $context The context data. |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function info($message, array $context = []) |
139
|
|
|
{ |
140
|
|
|
$this->sentryClient->captureMessage($message, [ |
141
|
|
|
'extra' => $context, |
142
|
|
|
'level' => 'info', |
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Logs with an arbitrary level. |
148
|
|
|
* |
149
|
|
|
* @param string|integer $level The log level. |
150
|
|
|
* @param string $message The message to log. |
151
|
|
|
* @param array $context The context data. |
152
|
|
|
* |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function log($level, $message, array $context = []) |
156
|
|
|
{ |
157
|
|
|
$this->sentryClient->captureMessage($message, [ |
158
|
|
|
'extra' => $context, |
159
|
|
|
'level' => $level, |
160
|
|
|
]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Normal but significant events. |
165
|
|
|
* |
166
|
|
|
* @param string $message The message to log. |
167
|
|
|
* @param array $context The context data. |
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
public function notice($message, array $context = []) |
172
|
|
|
{ |
173
|
|
|
$this->sentryClient->captureMessage($message, [ |
174
|
|
|
'extra' => $context, |
175
|
|
|
'level' => 'info', |
176
|
|
|
]); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Exceptional occurrences that are not errors. |
181
|
|
|
* |
182
|
|
|
* @param string $message The message to log. |
183
|
|
|
* @param array $context The context data. |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
public function warning($message, array $context = []) |
188
|
|
|
{ |
189
|
|
|
$this->sentryClient->captureMessage($message, [ |
190
|
|
|
'extra' => $context, |
191
|
|
|
'level' => 'warning', |
192
|
|
|
]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
} |
196
|
|
|
|