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
|
|
|
|
14
|
|
|
namespace Graze\Monolog\Handler; |
15
|
|
|
|
16
|
|
|
use Graze\Monolog\Formatter\RaygunFormatter; |
17
|
|
|
use Monolog\Handler\AbstractProcessingHandler; |
18
|
|
|
use Monolog\Logger; |
19
|
|
|
use Raygun4php\RaygunClient; |
20
|
|
|
|
21
|
|
|
class RaygunHandler extends AbstractProcessingHandler |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var RaygunClient |
25
|
|
|
*/ |
26
|
|
|
protected $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param RaygunClient $client |
30
|
|
|
* @param int $level |
31
|
|
|
* @param bool $bubble |
32
|
|
|
*/ |
33
|
12 |
|
public function __construct(RaygunClient $client, $level = Logger::DEBUG, $bubble = true) |
34
|
|
|
{ |
35
|
12 |
|
$this->client = $client; |
36
|
|
|
|
37
|
12 |
|
parent::__construct($level, $bubble); |
38
|
12 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $record |
42
|
|
|
*/ |
43
|
9 |
|
protected function write(array $record) |
44
|
|
|
{ |
45
|
9 |
|
$context = $record['context']; |
46
|
|
|
|
47
|
9 |
|
if (isset($context['exception']) |
48
|
|
|
&& ( |
49
|
3 |
|
$context['exception'] instanceof \Exception |
50
|
9 |
|
|| (PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable) |
51
|
|
|
) |
52
|
|
|
) { |
53
|
3 |
|
$this->writeException( |
54
|
3 |
|
$record, |
55
|
3 |
|
$record['formatted']['tags'], |
56
|
3 |
|
$record['formatted']['custom_data'], |
57
|
3 |
|
$record['formatted']['timestamp'] |
58
|
|
|
); |
59
|
6 |
|
} elseif (isset($context['file']) && isset($context['line'])) { |
60
|
2 |
|
$this->writeError( |
61
|
2 |
|
$record['formatted'], |
62
|
2 |
|
$record['formatted']['tags'], |
63
|
2 |
|
$record['formatted']['custom_data'], |
64
|
2 |
|
$record['formatted']['timestamp'] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
// do nothing if its not an exception or an error |
68
|
9 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $record |
72
|
|
|
* @param array $tags |
73
|
|
|
* @param array $customData |
74
|
|
|
* @param int|float $timestamp |
75
|
|
|
*/ |
76
|
2 |
|
protected function writeError(array $record, array $tags = [], array $customData = [], $timestamp = null) |
77
|
|
|
{ |
78
|
2 |
|
$context = $record['context']; |
79
|
2 |
|
$this->client->SendError( |
80
|
2 |
|
0, |
81
|
2 |
|
$record['message'], |
82
|
2 |
|
$context['file'], |
83
|
2 |
|
$context['line'], |
84
|
2 |
|
$tags, |
85
|
2 |
|
$customData, |
86
|
2 |
|
$timestamp |
|
|
|
|
87
|
|
|
); |
88
|
2 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $record |
92
|
|
|
* @param array $tags |
93
|
|
|
* @param array $customData |
94
|
|
|
* @param int|float $timestamp |
95
|
|
|
*/ |
96
|
3 |
|
protected function writeException(array $record, array $tags = [], array $customData = [], $timestamp = null) |
97
|
|
|
{ |
98
|
3 |
|
$this->client->SendException($record['context']['exception'], $tags, $customData, $timestamp); |
|
|
|
|
99
|
3 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return \Monolog\Formatter\FormatterInterface |
103
|
|
|
*/ |
104
|
6 |
|
protected function getDefaultFormatter() |
105
|
|
|
{ |
106
|
6 |
|
return new RaygunFormatter(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|