/var/www/html_it/wp-content/plugins/query-monitor/collectors/logger.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php declare(strict_types 1);
/**
 * PSR-3 compatible logging collector.
 *
 * @package query-monitor
 */

if ( ! defined'ABSPATH' ) ) {
    exit;
}

/**
 * @extends QM_DataCollector<QM_Data_Logger>
 * @phpstan-type LogMessage WP_Error|Throwable|string|bool|null
 */
class QM_Collector_Logger extends QM_DataCollector {

    public 
$id 'logger';

    public const 
EMERGENCY 'emergency';
    public const 
ALERT 'alert';
    public const 
CRITICAL 'critical';
    public const 
ERROR 'error';
    public const 
WARNING 'warning';
    public const 
NOTICE 'notice';
    public const 
INFO 'info';
    public const 
DEBUG 'debug';

    public function 
get_storage(): QM_Data {
        return new 
QM_Data_Logger();
    }

    
/**
     * @return void
     */
    
public function set_up() {
        
parent::set_up();

        
$this->data->counts array_fill_keys$this->get_levels(), );

        foreach ( 
$this->get_levels() as $level ) {
            
add_action"qm/{$level}", array( $this$level ), 10);
        }

        
add_action'qm/assert', array( $this'assert' ), 10);
        
add_action'qm/log', array( $this'log' ), 10);
    }

    
/**
     * @return void
     */
    
public function tear_down() {
        foreach ( 
$this->get_levels() as $level ) {
            
remove_action"qm/{$level}", array( $this$level ), 10 );
        }

        
remove_action'qm/log', array( $this'log' ), 10 );

        
parent::tear_down();
    }

    
/**
     * @param mixed $message
     * @param array<string, mixed> $context
     * @phpstan-param LogMessage $message
     * @return void
     */
    
public function emergency$message, array $context = array() ) {
        
$this->storeself::EMERGENCY$message$context );
    }

    
/**
     * @param mixed $message
     * @param array<string, mixed> $context
     * @phpstan-param LogMessage $message
     * @return void
     */
    
public function alert$message, array $context = array() ) {
        
$this->storeself::ALERT$message$context );
    }

    
/**
     * @param mixed $message
     * @param array<string, mixed> $context
     * @phpstan-param LogMessage $message
     * @return void
     */
    
public function critical$message, array $context = array() ) {
        
$this->storeself::CRITICAL$message$context );
    }

    
/**
     * @param mixed $message
     * @param array<string, mixed> $context
     * @phpstan-param LogMessage $message
     * @return void
     */
    
public function error$message, array $context = array() ) {
        
$this->storeself::ERROR$message$context );
    }

    
/**
     * @param mixed $message
     * @param array<string, mixed> $context
     * @phpstan-param LogMessage $message
     * @return void
     */
    
public function warning$message, array $context = array() ) {
        
$this->storeself::WARNING$message$context );
    }

    
/**
     * @param mixed $message
     * @param array<string, mixed> $context
     * @phpstan-param LogMessage $message
     * @return void
     */
    
public function notice$message, array $context = array() ) {
        
$this->storeself::NOTICE$message$context );
    }

    
/**
     * @param mixed $message
     * @param array<string, mixed> $context
     * @phpstan-param LogMessage $message
     * @return void
     */
    
public function info$message, array $context = array() ) {
        
$this->storeself::INFO$message$context );
    }

    
/**
     * @param mixed $message
     * @param array<string, mixed> $context
     * @phpstan-param LogMessage $message
     * @return void
     */
    
public function debug$message, array $context = array() ) {
        
$this->storeself::DEBUG$message$context );
    }

    
/**
     * @param mixed $assertion
     * @param string $message
     * @param ?mixed $value
     * @return void
     */
    
public function assert$assertionstring $message ''$value null ) {
        
$prefix null;

        if ( 
$assertion ) {
            
$level self::DEBUG;

            if ( 
$message ) {
                
$message sprintf(
                    
/* translators: %s: Assertion message */
                    
__'Assertion passed: %s''query-monitor' ),
                    
$message
                
);
            } else {
                
$message __'Assertion passed''query-monitor' );
            }
        } else {
            
$level self::ERROR;

            if ( 
$message ) {
                
$message sprintf(
                    
/* translators: %s: Assertion message */
                    
__'Assertion failed: %s''query-monitor' ),
                    
$message
                
);
            } else {
                
$message __'Assertion failed''query-monitor' );
            }

            if ( 
$value !== null ) {
                
$prefix $message;
                
$message $value;
            }
        }

        
$this->store$level$message, array(), $prefix );
    }

    
/**
     * @param string $level
     * @param mixed $message
     * @param array<string, mixed> $context
     * @phpstan-param self::* $level
     * @phpstan-param LogMessage $message
     * @return void
     */
    
public function log$level$message, array $context = array() ) {
        if ( ! 
in_array$level$this->get_levels(), true ) ) {
            throw new 
InvalidArgumentException'Unsupported log level' );
        }

        
$this->store$level$message$context );
    }

    
/**
     * @param string $level
     * @param mixed $message
     * @param array<string, mixed> $context
     * @param ?string $prefix
     * @phpstan-param self::* $level
     * @phpstan-param LogMessage $message
     * @return void
     */
    
protected function store$level$message, array $context = array(), ?string $prefix null ) {
        
$trace = new QM_Backtrace( array(
            
'ignore_hook' => array(
                
current_filter() => true,
            ),
        ) );

        if ( 
$message instanceof WP_Error ) {
            
$message sprintf(
                
'WP_Error: %s (%s)',
                
$message->get_error_message(),
                
$message->get_error_code()
            );
        }

        if ( 
$message instanceof Throwable ) {
            
$message sprintf(
                
'%1$s: %2$s',
                
get_class$message ),
                
$message->getMessage()
            );
        }

        if ( ! 
is_string$message ) ) {
            if ( 
null === $message ) {
                
$message 'null';
            } elseif ( 
false === $message ) {
                
$message 'false';
            } elseif ( 
true === $message ) {
                
$message 'true';
            }

            
$message print_r$messagetrue );
        } elseif ( 
'' === trim$message ) ) {
            
$message '(Empty string)';
        }

        
$this->data->counts$level ]++;
        
$this->data->logs[] = array(
            
'message' => self::interpolate$message$context$prefix ),
            
'filtered_trace' => $trace->get_filtered_trace(),
            
'component' => $trace->get_component(),
            
'level' => $level,
        );
    }

    
/**
     * @param string $message
     * @param array<string, mixed> $context
     * @param ?string $prefix
     * @return string
     */
    
protected static function interpolate$message, array $context = array(), ?string $prefix null ) {
        
// build a replacement array with braces around the context keys
        
$replace = array();

        foreach ( 
$context as $key => $val ) {
            
// check that the value can be casted to string
            
if ( is_bool$val ) ) {
                
$replace"{{$key}}" ] = ( $val 'true' 'false' );
            } elseif ( 
is_scalar$val ) ) {
                
$replace"{{$key}}" ] = $val;
            } elseif ( 
is_object$val ) ) {
                
$replace"{{$key}}" ] = sprintf'[%s]'get_class$val ) );
            } else {
                
$replace"{{$key}}" ] = sprintf'[%s]'gettype$val ) );
            }
        }

        
// interpolate replacement values into the message and return
        
$message strtr$message$replace );

        if ( 
$prefix !== null ) {
            
$message $prefix "\n" $message;
        }

        return 
$message;
    }

    
/**
     * @return void
     */
    
public function process() {
        if ( empty( 
$this->data->logs ) ) {
            return;
        }

        
$components = array();

        foreach ( 
$this->data->logs as $row ) {
            
$component $row['component'];
            
$components$component->name ] = $component->name;
        }

        
$this->data->components $components;
    }

    
/**
     * @return array<int, string>
     * @phpstan-return list<self::*>
     */
    
public function get_levels() {
        return array(
            
self::EMERGENCY,
            
self::ALERT,
            
self::CRITICAL,
            
self::ERROR,
            
self::WARNING,
            
self::NOTICE,
            
self::INFO,
            
self::DEBUG,
        );
    }

    
/**
     * @return array<int, string>
     * @phpstan-return list<self::*>
     */
    
public function get_warning_levels() {
        return array(
            
self::EMERGENCY,
            
self::ALERT,
            
self::CRITICAL,
            
self::ERROR,
            
self::WARNING,
        );
    }

}

# Load early in case a plugin wants to log a message early in the bootstrap process
QM_Collectors::add( new QM_Collector_Logger() );