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 Monolog\Handler\AbstractProcessingHandler; |
17
|
|
|
use Monolog\Logger; |
18
|
|
|
use Whoops\Exception\ErrorException as WhoopsErrorException; |
19
|
|
|
use Whoops\Exception\Inspector as WhoopsInspector; |
20
|
|
|
use Whoops\Handler\Handler; |
21
|
|
|
use Whoops\Handler\HandlerInterface as WhoopsHandlerInterface; |
22
|
|
|
use Whoops\Run as WhoopsRun; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Allow the use of Whoops\Handler\HandlerInterface handlers with Monolog |
26
|
|
|
* |
27
|
|
|
* @author John |
28
|
|
|
*/ |
29
|
|
|
class WhoopsHandler extends AbstractProcessingHandler |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var WhoopsHandlerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $whoopsHandler; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param WhoopsHandlerInterface $whoopsHandler |
38
|
|
|
* @param int $level |
39
|
|
|
* @param bool $bubble |
40
|
|
|
*/ |
41
|
9 |
|
public function __construct(WhoopsHandlerInterface $whoopsHandler, $level = Logger::DEBUG, $bubble = true) |
42
|
|
|
{ |
43
|
9 |
|
$this->whoopsHandler = $whoopsHandler; |
44
|
|
|
|
45
|
9 |
|
parent::__construct($level, $bubble); |
46
|
9 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $record |
50
|
|
|
*/ |
51
|
7 |
|
protected function write(array $record) |
52
|
|
|
{ |
53
|
7 |
|
$context = $record['context']; |
54
|
|
|
|
55
|
7 |
|
if (isset($context['exception']) |
56
|
|
|
&& ( |
57
|
2 |
|
$context['exception'] instanceof \Exception |
58
|
7 |
|
|| (PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable) |
59
|
|
|
)) { |
60
|
2 |
|
$this->writeException($context['exception']); |
61
|
5 |
|
} elseif (isset($context['file']) && isset($context['line'])) { |
62
|
1 |
|
$this->writeError($record); |
63
|
|
|
} |
64
|
7 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Whoops only deals with Exceptions. Create a WhoopsErrorException based on the error details and handle that |
68
|
|
|
* |
69
|
|
|
* @param array $record |
70
|
|
|
*/ |
71
|
1 |
|
protected function writeError(array $record) |
72
|
|
|
{ |
73
|
1 |
|
$exception = new WhoopsErrorException( |
74
|
1 |
|
$record['message'], |
75
|
1 |
|
$record['level'], |
76
|
1 |
|
0, |
77
|
1 |
|
$record['context']['file'], |
78
|
1 |
|
$record['context']['line'] |
79
|
|
|
); |
80
|
|
|
|
81
|
1 |
|
$this->writeException($exception); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param \Exception|\Throwable $exception |
86
|
|
|
*/ |
87
|
3 |
|
protected function writeException($exception) |
88
|
|
|
{ |
89
|
3 |
|
$whoopsInspector = new WhoopsInspector($exception); |
90
|
|
|
|
91
|
3 |
|
$this->whoopsHandler->setInspector($whoopsInspector); |
92
|
3 |
|
$this->whoopsHandler->setException($exception); |
93
|
3 |
|
$this->whoopsHandler->setRun(new WhoopsRun); |
94
|
|
|
|
95
|
3 |
|
$whoopsHandleResponse = $this->whoopsHandler->handle(); |
96
|
|
|
|
97
|
3 |
|
$this->processWhoopsBubbling($whoopsHandleResponse); |
98
|
3 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Map Whoops->handle() responses to Monolog bubbling |
102
|
|
|
* |
103
|
|
|
* @param int $whoopsHandleResponse response as returned from Whoops\Handler\Handler::handle() |
104
|
|
|
*/ |
105
|
3 |
|
protected function processWhoopsBubbling($whoopsHandleResponse) |
106
|
|
|
{ |
107
|
|
|
switch ($whoopsHandleResponse) { |
108
|
3 |
|
case Handler::LAST_HANDLER: |
109
|
3 |
|
case Handler::QUIT: |
110
|
|
|
// don't call further monolog handlers |
111
|
|
|
$this->setBubble(false); |
112
|
|
|
} |
113
|
3 |
|
} |
114
|
|
|
} |
115
|
|
|
|