|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Contains class LineFormatter. |
|
5
|
|
|
* |
|
6
|
|
|
* PHP version 7.0+ |
|
7
|
|
|
* |
|
8
|
|
|
* LICENSE: |
|
9
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
|
10
|
|
|
* which can be used to access the Eve Online API data and place it into a |
|
11
|
|
|
* database. |
|
12
|
|
|
* Copyright (C) 2016 Michael Cummings |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify it |
|
15
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
|
16
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
|
17
|
|
|
* option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
20
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
|
22
|
|
|
* for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
25
|
|
|
* along with this program. If not, see |
|
26
|
|
|
* <http://spdx.org/licenses/LGPL-3.0.html>. |
|
27
|
|
|
* |
|
28
|
|
|
* You should be able to find a copy of this license in the COPYING-LESSER.md |
|
29
|
|
|
* file. A copy of the GNU GPL should also be available in the COPYING.md file. |
|
30
|
|
|
* |
|
31
|
|
|
* @copyright 2016 Michael Cummings |
|
32
|
|
|
* @license LGPL-3.0+ |
|
33
|
|
|
* @author Michael Cummings <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
namespace Yapeal\Log; |
|
36
|
|
|
|
|
37
|
|
|
use Monolog\Formatter\LineFormatter as MLineFormatter; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Class LineFormatter. |
|
41
|
|
|
*/ |
|
42
|
|
|
class LineFormatter extends MLineFormatter |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
|
|
public function isPrettyJson(): bool |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->prettyJson; |
|
50
|
|
|
} |
|
51
|
|
|
/** |
|
52
|
|
|
* @param bool $value |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setPrettyJson(bool $value = true) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->prettyJson = $value; |
|
57
|
|
|
if ($value) { |
|
58
|
|
|
$this->allowInlineLineBreaks($value); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
/** |
|
62
|
|
|
* @param mixed $data |
|
63
|
|
|
* |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
* @throws \InvalidArgumentException |
|
66
|
|
|
* @throws \RuntimeException |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function normalize($data) |
|
69
|
|
|
{ |
|
70
|
|
|
if ($data instanceof \DateTime) { |
|
71
|
|
|
return substr($data->format($this->dateFormat), 0, 14); |
|
72
|
|
|
} |
|
73
|
|
|
return parent::normalize($data); |
|
74
|
|
|
} |
|
75
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection */ |
|
76
|
|
|
/** |
|
77
|
|
|
* @param \Throwable $exc |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
* @throws \InvalidArgumentException |
|
81
|
|
|
* @throws \RuntimeException |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function normalizeException($exc): array |
|
84
|
|
|
{ |
|
85
|
|
|
if (!$exc instanceof \Throwable) { |
|
86
|
|
|
throw new \InvalidArgumentException('Throwable expected, got ' . gettype($exc) . ' / ' . get_class($exc)); |
|
87
|
|
|
} |
|
88
|
|
|
$data = [ |
|
89
|
|
|
'class' => get_class($exc), |
|
90
|
|
|
'message' => $exc->getMessage(), |
|
91
|
|
|
'code' => $exc->getCode(), |
|
92
|
|
|
'file' => str_replace('\\', '/', $exc->getFile()) . ':' . $exc->getLine() |
|
93
|
|
|
]; |
|
94
|
|
|
if ($this->includeStacktraces) { |
|
95
|
|
|
foreach ($exc->getTrace() as $frame) { |
|
96
|
|
|
if (isset($frame['file'])) { |
|
97
|
|
|
$data['trace'][] = str_replace('\\', '/', $frame['file']) . ':' . $frame['line']; |
|
98
|
|
|
} elseif (isset($frame['function']) && $frame['function'] === '{closure}') { |
|
99
|
|
|
// We should again normalize the frames, because it might contain invalid items |
|
100
|
|
|
$data['trace'][] = $this->normalize($frame['function']); |
|
101
|
|
|
} else { |
|
102
|
|
|
// We should again normalize the frames, because it might contain invalid items |
|
103
|
|
|
$data['trace'][] = $this->toJson($this->normalize($frame), true); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
if (null !== $previous = $exc->getPrevious()) { |
|
108
|
|
|
$data['previous'] = $this->normalizeException($previous); |
|
109
|
|
|
} |
|
110
|
|
|
return $data; |
|
111
|
|
|
} |
|
112
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection */ |
|
113
|
|
|
/** |
|
114
|
|
|
* Return the JSON representation of a value |
|
115
|
|
|
* |
|
116
|
|
|
* @param mixed $data |
|
117
|
|
|
* @param bool $ignoreErrors |
|
118
|
|
|
* |
|
119
|
|
|
* @throws \RuntimeException if encoding fails and errors are not ignored |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function toJson($data, $ignoreErrors = false): string |
|
123
|
|
|
{ |
|
124
|
|
|
$options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION; |
|
125
|
|
|
if ($this->isPrettyJson()) { |
|
126
|
|
|
$options |= JSON_PRETTY_PRINT; |
|
127
|
|
|
} |
|
128
|
|
|
// suppress json_encode errors since it's twitchy with some inputs |
|
129
|
|
|
if ($ignoreErrors) { |
|
130
|
|
|
/** @noinspection PhpUsageOfSilenceOperatorInspection */ |
|
131
|
|
|
return @json_encode($data, $options); |
|
132
|
|
|
} |
|
133
|
|
|
$json = json_encode($data, $options); |
|
134
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
135
|
|
|
$json = json_last_error_msg(); |
|
136
|
|
|
} |
|
137
|
|
|
return $json; |
|
138
|
|
|
} |
|
139
|
|
|
/** |
|
140
|
|
|
* @var bool $prettyJson |
|
141
|
|
|
*/ |
|
142
|
|
|
private $prettyJson = false; |
|
143
|
|
|
} |
|
144
|
|
|
|