Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 50 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
BacktraceProcessor | |
0.00% |
0 / 50 |
|
0.00% |
0 / 3 |
462 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
__invoke | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
240 | |||
isTheClassSkippable | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA; |
19 | * |
20 | */ |
21 | |
22 | namespace oat\oatbox\log\logger\processor; |
23 | |
24 | use Psr\Log\LogLevel; |
25 | |
26 | /** |
27 | * Class BacktraceProcessor |
28 | * |
29 | * A processor to add the trace to the log, only with a level superior to given level |
30 | * |
31 | * Inspired from \Monolog\Processor\IntrospectionProcessor |
32 | * |
33 | * @package oat\oatbox\log\logger\processor |
34 | */ |
35 | class BacktraceProcessor |
36 | { |
37 | /** |
38 | * Trace offset name under the log extra offset. |
39 | */ |
40 | public const TRACE_OFFSET = 'trace'; |
41 | |
42 | /** |
43 | * @var array |
44 | */ |
45 | private $classKeywordsToSkip = [ |
46 | 'Monolog\\', |
47 | '\\TaoMonolog', |
48 | '\\LoggerService', |
49 | 'common_Logger', |
50 | ]; |
51 | |
52 | /** |
53 | * @var bool |
54 | */ |
55 | private $skipLoggerClasses; |
56 | |
57 | /** |
58 | * @var string |
59 | */ |
60 | protected $level; |
61 | |
62 | /** |
63 | * BacktraceProcessor constructor. |
64 | * |
65 | * @param string $level |
66 | * @param bool $skipLoggerClasses |
67 | * @param array $classKeywordsToSkip |
68 | */ |
69 | public function __construct($level = LogLevel::DEBUG, $skipLoggerClasses = false, $classKeywordsToSkip = []) |
70 | { |
71 | $this->level = $level; |
72 | $this->skipLoggerClasses = $skipLoggerClasses; |
73 | $this->classKeywordsToSkip = array_merge( |
74 | $this->classKeywordsToSkip, |
75 | $classKeywordsToSkip |
76 | ); |
77 | } |
78 | |
79 | /** |
80 | * Returns the record decorated with the backtrace. |
81 | * |
82 | * @param array $record |
83 | * |
84 | * @return array |
85 | */ |
86 | public function __invoke(array $record) |
87 | { |
88 | // return if the level is not high enough |
89 | if ($record['level'] < $this->level) { |
90 | return $record; |
91 | } |
92 | |
93 | /* |
94 | * http://php.net/manual/en/function.debug-backtrace.php |
95 | * As of 5.3.6, DEBUG_BACKTRACE_IGNORE_ARGS option was added. |
96 | * Any version less than 5.3.6 must use the DEBUG_BACKTRACE_IGNORE_ARGS constant value '2'. |
97 | */ |
98 | $trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS); |
99 | |
100 | // skip first since it's always the current method |
101 | array_shift($trace); |
102 | // the call_user_func call is also skipped |
103 | array_shift($trace); |
104 | |
105 | foreach ($trace as $key => $row) { |
106 | // If we need to skip the trace row. |
107 | if ($this->isTheClassSkippable($row)) { |
108 | unset($trace[$key]); |
109 | |
110 | continue; |
111 | } |
112 | |
113 | if (isset($trace[$key]['object'])) { |
114 | unset($trace[$key]['object']); |
115 | } |
116 | |
117 | if (isset($trace[$key]['args'])) { |
118 | $vars = []; |
119 | foreach ($trace[$key]['args'] as $k => $v) { |
120 | switch (gettype($v)) { |
121 | case 'boolean': |
122 | case 'integer': |
123 | case 'double': |
124 | $vars[$k] = (string)$v; |
125 | break; |
126 | case 'string': |
127 | $vars[$k] = strlen($v) > 128 ? 'string(' . strlen($v) . ')' : $v; |
128 | break; |
129 | case 'class': |
130 | $vars[$k] = get_class($v); |
131 | break; |
132 | default: |
133 | $vars[$k] = gettype($v); |
134 | } |
135 | } |
136 | $trace[$key]['args'] = $vars; |
137 | } |
138 | } |
139 | |
140 | // we should have the call source now |
141 | $record['extra'] = array_merge( |
142 | $record['extra'], |
143 | [ |
144 | static::TRACE_OFFSET => array_values($trace) |
145 | ] |
146 | ); |
147 | |
148 | return $record; |
149 | } |
150 | |
151 | /** |
152 | * Returns TRUE if the given trace is skippable. |
153 | * |
154 | * @param array $trace |
155 | * |
156 | * @return bool |
157 | */ |
158 | private function isTheClassSkippable(array $trace) |
159 | { |
160 | if ($this->skipLoggerClasses === false) { |
161 | return false; |
162 | } |
163 | |
164 | if (empty($trace['class'])) { |
165 | return false; |
166 | } |
167 | |
168 | foreach ($this->classKeywordsToSkip as $current) { |
169 | if (strpos($trace['class'], $current) !== false) { |
170 | return true; |
171 | } |
172 | } |
173 | |
174 | return false; |
175 | } |
176 | } |