1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Monolog Extensions |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @see http://github.com/graze/MonologExtensions/blob/master/LICENSE |
11
|
|
|
* @link http://github.com/graze/MonologExtensions |
12
|
|
|
*/ |
13
|
|
|
namespace Graze\Monolog\Handler; |
14
|
|
|
|
15
|
|
|
use Graze\Monolog\Formatter\RaygunFormatter; |
16
|
|
|
use Monolog\Handler\AbstractProcessingHandler; |
17
|
|
|
use Monolog\Logger; |
18
|
|
|
use Raygun4php\RaygunClient; |
19
|
|
|
|
20
|
|
|
class RaygunHandler extends AbstractProcessingHandler |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var RaygunClient |
24
|
|
|
*/ |
25
|
|
|
protected $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param RaygunClient $client |
29
|
|
|
* @param int $level |
30
|
|
|
* @param bool $bubble |
31
|
|
|
*/ |
32
|
|
|
public function __construct(RaygunClient $client, $level = Logger::DEBUG, $bubble = true) |
33
|
|
|
{ |
34
|
|
|
$this->client = $client; |
35
|
|
|
|
36
|
|
|
parent::__construct($level, $bubble); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function isHandling(array $record) |
43
|
|
|
{ |
44
|
|
|
if(parent::isHandling($record)) { |
|
|
|
|
45
|
|
|
$context = $record['context']; |
46
|
|
|
|
47
|
|
|
//Ensure only valid records will be handled and no InvalidArgumentException will be thrown |
48
|
|
|
if ((isset($context['exception']) && |
|
|
|
|
49
|
|
|
( |
50
|
|
|
$context['exception'] instanceof \Exception || |
51
|
|
|
(PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable) |
52
|
|
|
) |
53
|
|
|
) || (isset($context['file']) && $context['line']) |
54
|
|
|
) { |
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $record |
63
|
|
|
*/ |
64
|
|
|
protected function write(array $record) |
65
|
|
|
{ |
66
|
|
|
$context = $record['context']; |
67
|
|
|
|
68
|
|
|
if (isset($context['exception']) && |
69
|
|
|
( |
70
|
|
|
$context['exception'] instanceof \Exception || |
71
|
|
|
(PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable) |
72
|
|
|
) |
73
|
|
|
) { |
74
|
|
|
$this->writeException( |
75
|
|
|
$record, |
76
|
|
|
$record['formatted']['tags'], |
77
|
|
|
$record['formatted']['custom_data'], |
78
|
|
|
$record['formatted']['timestamp'] |
79
|
|
|
); |
80
|
|
|
} elseif (isset($context['file']) && $context['line']) { |
81
|
|
|
$this->writeError( |
82
|
|
|
$record['formatted'], |
83
|
|
|
$record['formatted']['tags'], |
84
|
|
|
$record['formatted']['custom_data'], |
85
|
|
|
$record['formatted']['timestamp'] |
86
|
|
|
); |
87
|
|
|
} else { |
88
|
|
|
throw new \InvalidArgumentException('Invalid record given.'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $record |
94
|
|
|
* @param array $tags |
95
|
|
|
* @param array $customData |
96
|
|
|
* @param int|float $timestamp |
97
|
|
|
*/ |
98
|
|
|
protected function writeError(array $record, array $tags = array(), array $customData = array(), $timestamp = null) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$context = $record['context']; |
101
|
|
|
$this->client->SendError( |
102
|
|
|
0, |
103
|
|
|
$record['message'], |
104
|
|
|
$context['file'], |
105
|
|
|
$context['line'], |
106
|
|
|
$tags, |
107
|
|
|
$customData, |
108
|
|
|
$timestamp |
|
|
|
|
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $record |
114
|
|
|
* @param array $tags |
115
|
|
|
* @param array $customData |
116
|
|
|
* @param int|float $timestamp |
117
|
|
|
*/ |
118
|
|
|
protected function writeException(array $record, array $tags = array(), array $customData = array(), $timestamp = null) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$this->client->SendException($record['context']['exception'], $tags, $customData, $timestamp); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return \Monolog\Formatter\FormatterInterface |
125
|
|
|
*/ |
126
|
|
|
protected function getDefaultFormatter() |
127
|
|
|
{ |
128
|
|
|
return new RaygunFormatter(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|