1 | <?php |
||
25 | class SentryLogger |
||
26 | { |
||
27 | use Configurable; |
||
28 | |||
29 | /** |
||
30 | * @var SentryAdaptor |
||
31 | */ |
||
32 | public $adaptor = null; |
||
33 | |||
34 | /** |
||
35 | * Stipulates what gets shown in the Sentry UI, should some metric not be |
||
36 | * available for any reason. |
||
37 | * |
||
38 | * @const string |
||
39 | */ |
||
40 | const SLW_NOOP = 'Unavailable'; |
||
41 | |||
42 | /** |
||
43 | * A static constructor as per {@link Zend_Log_FactoryInterface}. |
||
44 | * |
||
45 | * @param array $config An array of optional additional configuration for |
||
46 | * passing custom information to Sentry. See the README |
||
47 | * for more detail. |
||
48 | * @return SentryLogger |
||
49 | */ |
||
50 | public static function factory(array $config = []) : SentryLogger |
||
51 | { |
||
52 | $env = $config['env'] ?? []; |
||
53 | $user = $config['user'] ?? []; |
||
54 | $tags = $config['tags'] ?? []; |
||
55 | $extra = $config['extra'] ?? []; |
||
56 | $level = $config['level'] ?? []; |
||
57 | $logger = Injector::inst()->create(static::class); |
||
58 | |||
59 | // Set default environment |
||
60 | $env = $env ?: $logger->defaultEnv(); |
||
61 | // Set any available user data |
||
62 | $user = $user ?: []; |
||
63 | // Set any available tags available in SS config |
||
64 | $tags = array_merge($logger->defaultTags(), $tags); |
||
65 | // Set any available additional (extra) data |
||
66 | $extra = array_merge($logger->defaultExtra(), $extra); |
||
67 | |||
68 | $logger->adaptor->setContext('env', $env); |
||
69 | $logger->adaptor->setContext('tags', $tags); |
||
70 | $logger->adaptor->setContext('extra', $extra); |
||
71 | $logger->adaptor->setContext('user', $user); |
||
72 | $logger->adaptor->setContext('level', $level); |
||
73 | |||
74 | return $logger; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @return SentryAdaptor |
||
79 | */ |
||
80 | public function getAdaptor() : SentryAdaptor |
||
81 | { |
||
82 | return $this->adaptor; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Returns a default environment when one isn't passed to the factory() |
||
87 | * method. |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function defaultEnv() : string |
||
95 | |||
96 | /** |
||
97 | * Returns a default set of additional "tags" we wish to send to Sentry. |
||
98 | * By default, Sentry reports on several mertrics, and we're already sending |
||
99 | * {@link Member} data. But there are additional data that would be useful |
||
100 | * for debugging via the Sentry UI. |
||
101 | * |
||
102 | * These data can augment that which is sent to Sentry at setup |
||
103 | * time in _config.php. See the README for more detail. |
||
104 | * |
||
105 | * N.b. Tags can be used to group messages within the Sentry UI itself, so there |
||
106 | * should only be "static" data being sent, not something that can drastically |
||
107 | * or minutely change, such as memory usage for example. |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | public function defaultTags() : array |
||
120 | |||
121 | /** |
||
122 | * Returns a default set of extra data to show upon selecting a message for |
||
123 | * analysis in the Sentry UI. This can augment the data sent to Sentry at setup |
||
124 | * time in _config.php as well as at runtime when calling SS_Log itself. |
||
125 | * See the README for more detail. |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | public function defaultExtra() : array |
||
135 | |||
136 | /** |
||
137 | * Return the version of $pkg taken from composer.lock. |
||
138 | * |
||
139 | * @param string $pkg e.g. "silverstripe/framework" |
||
140 | * @return string |
||
141 | */ |
||
142 | public function getPackageInfo(string $pkg) : string |
||
160 | |||
161 | /** |
||
162 | * What sort of request is this? (A harder question to answer than you might |
||
163 | * think: http://stackoverflow.com/questions/6275363/what-is-the-correct-terminology-for-a-non-ajax-request) |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getRequestType() : string |
||
174 | |||
175 | /** |
||
176 | * Return peak memory usage. |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getPeakMemory() : string |
||
186 | |||
187 | /** |
||
188 | * Basic User-Agent check and return. |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | public function getUserAgent() : string |
||
202 | |||
203 | /** |
||
204 | * Basic request method check and return. |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getReqMethod() : string |
||
218 | |||
219 | /** |
||
220 | * @return string |
||
221 | */ |
||
222 | public function getSAPI() : string |
||
226 | |||
227 | /** |
||
228 | * Returns the client IP address which originated this request. |
||
229 | * Lifted and modified from SilverStripe 3's SS_HTTPRequest. |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | public static function get_ip() : string |
||
234 | { |
||
235 | $headerOverrideIP = null; |
||
236 | |||
237 | if (defined('TRUSTED_PROXY')) { |
||
238 | $headers = (defined('SS_TRUSTED_PROXY_IP_HEADER')) ? |
||
239 | [SS_TRUSTED_PROXY_IP_HEADER] : |
||
240 | null; |
||
241 | |||
242 | if (!$headers) { |
||
243 | // Backwards compatible defaults |
||
244 | $headers = ['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR']; |
||
245 | } |
||
246 | |||
247 | foreach ($headers as $header) { |
||
248 | if (!empty($_SERVER[$header])) { |
||
249 | $headerOverrideIP = $_SERVER[$header]; |
||
250 | |||
251 | break; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | |||
256 | $proxy = Injector::inst()->create(TrustedProxyMiddleware::class); |
||
257 | |||
258 | if ($headerOverrideIP) { |
||
259 | return $proxy->getIPFromHeaderValue($headerOverrideIP); |
||
260 | } |
||
261 | |||
262 | if (isset($_SERVER['REMOTE_ADDR'])) { |
||
263 | return $_SERVER['REMOTE_ADDR']; |
||
264 | } |
||
265 | |||
266 | return ''; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Returns a default set of additional data specific to the user's part in |
||
271 | * the request. |
||
272 | * |
||
273 | * @param mixed Member|null $member |
||
274 | * @return array |
||
275 | */ |
||
276 | public static function user_data(Member $member = null) : array |
||
277 | { |
||
278 | if (!$member) { |
||
279 | $member = Security::getCurrentUser(); |
||
280 | } |
||
281 | |||
282 | return [ |
||
283 | 'IPAddress' => self::get_ip() ?: self::SLW_NOOP, |
||
284 | 'ID' => $member ? $member->getField('ID') : self::SLW_NOOP, |
||
285 | 'Email' => $member ? $member->getField('Email') : self::SLW_NOOP, |
||
286 | ]; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Manually extract or generate a suitable backtrace. This is especially useful |
||
291 | * in non-exception reports such as those that use Sentry\Client::captureMessage(). |
||
292 | * |
||
293 | * @param array $record |
||
294 | * @return array |
||
295 | */ |
||
296 | public static function backtrace(array $record) : array |
||
331 | } |
||
332 |