1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facile\SentryModule\Log\Writer; |
4
|
|
|
|
5
|
|
|
use Facile\SentryModule\Service\Client; |
6
|
|
|
use Zend\Log\Writer\AbstractWriter; |
7
|
|
|
use Zend\Log\Logger; |
8
|
|
|
use \Raven_Client; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Sentry |
12
|
|
|
* |
13
|
|
|
* @package Facile\SentryModule\Log\Writer |
14
|
|
|
*/ |
15
|
|
|
class Sentry extends AbstractWriter |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Client |
19
|
|
|
*/ |
20
|
|
|
protected $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $priorityMap = [ |
26
|
|
|
Logger::EMERG => Raven_Client::FATAL, |
27
|
|
|
Logger::ALERT => Raven_Client::ERROR, |
28
|
|
|
Logger::CRIT => Raven_Client::ERROR, |
29
|
|
|
Logger::ERR => Raven_Client::ERROR, |
30
|
|
|
Logger::WARN => Raven_Client::WARNING, |
31
|
|
|
Logger::NOTICE => Raven_Client::INFO, |
32
|
|
|
Logger::INFO => Raven_Client::INFO, |
33
|
|
|
Logger::DEBUG => Raven_Client::DEBUG, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Sentry constructor. |
38
|
|
|
* |
39
|
|
|
* @param array $options |
40
|
|
|
* @throws \RuntimeException |
41
|
|
|
* @throws \Zend\Log\Exception\InvalidArgumentException |
42
|
|
|
*/ |
43
|
3 |
|
public function __construct(array $options) |
44
|
|
|
{ |
45
|
3 |
|
parent::__construct($options); |
46
|
|
|
|
47
|
3 |
|
if (!array_key_exists('client', $options)) { |
48
|
1 |
|
throw new \RuntimeException('No client specified in options'); |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
$this->client = $options['client']; |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Write a message to the log |
56
|
|
|
* |
57
|
|
|
* @param array $event log data event |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
1 |
|
protected function doWrite(array $event) |
61
|
|
|
{ |
62
|
1 |
|
$priority = $this->priorityMap[$event['priority']]; |
63
|
|
|
|
64
|
1 |
|
$this->client->getRaven()->captureMessage( |
65
|
1 |
|
$event['message'], |
66
|
1 |
|
$this->sanitizeContextData($event['extra']), |
67
|
|
|
$priority |
68
|
1 |
|
); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $context |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
1 |
|
protected function sanitizeContextData(array $context) |
77
|
|
|
{ |
78
|
1 |
|
array_walk_recursive($context, [$this, 'sanitizeContextItem']); |
79
|
|
|
|
80
|
1 |
|
return $context; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param mixed $value |
85
|
|
|
*/ |
86
|
1 |
|
protected function sanitizeContextItem(&$value) |
87
|
|
|
{ |
88
|
1 |
|
if ($value instanceof \Traversable) { |
89
|
1 |
|
$value = $this->sanitizeContextData(iterator_to_array($value)); |
90
|
1 |
|
} |
91
|
1 |
|
if (is_object($value)) { |
92
|
1 |
|
$value = method_exists($value, '__toString') ? (string) $value : get_class($value); |
93
|
1 |
|
} elseif (is_resource($value)) { |
94
|
1 |
|
$value = get_resource_type($value); |
95
|
1 |
|
} |
96
|
1 |
|
} |
97
|
|
|
} |
98
|
|
|
|