|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2014 SURFnet bv |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Surfnet\StepupBundle\Monolog\Formatter; |
|
20
|
|
|
|
|
21
|
|
|
use Gelf\Message; |
|
22
|
|
|
use Monolog\Formatter\FormatterInterface; |
|
23
|
|
|
use Monolog\Formatter\GelfMessageFormatter; |
|
24
|
|
|
|
|
25
|
|
|
class FullMessageExceptionGelfMessageFormatter implements FormatterInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var GelfMessageFormatter |
|
29
|
|
|
*/ |
|
30
|
|
|
private $formatter; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param GelfMessageFormatter $formatter |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(GelfMessageFormatter $formatter) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->formatter = $formatter; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function format(array $record) |
|
41
|
|
|
{ |
|
42
|
|
|
if (!isset($record['context']['exception']) || !$record['context']['exception'] instanceof \Exception) { |
|
43
|
|
|
return $this->formatter->format($record); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** @var \Exception $exception */ |
|
47
|
|
|
$exception = $record['context']['exception']; |
|
48
|
|
|
$fullMessage = sprintf( |
|
49
|
|
|
"%s: \"%s\"\n\n%s", |
|
50
|
|
|
get_class($exception), |
|
51
|
|
|
$exception->getMessage(), |
|
52
|
|
|
$exception->getTraceAsString() |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
unset($record['context']['exception']); |
|
56
|
|
|
|
|
57
|
|
|
/** @var Message $message */ |
|
58
|
|
|
$message = $this->formatter->format($record); |
|
59
|
|
|
$message->setFullMessage($fullMessage); |
|
60
|
|
|
|
|
61
|
|
|
return $message; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function formatBatch(array $records) |
|
65
|
|
|
{ |
|
66
|
|
|
return array_map([$this, 'format'], $records); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|