1 | <?php |
---|
2 | /** |
---|
3 | * WordPress Logging API |
---|
4 | * |
---|
5 | * This pluggable file can be replaced by plugins |
---|
6 | * by enabling WP_DEBUG and having a file in |
---|
7 | * wp-content folder named "debug.php". |
---|
8 | * |
---|
9 | * There are also filters which allow a basic |
---|
10 | * plugin to halt displaying certain types and |
---|
11 | * severity levels of logging messages. There are |
---|
12 | * also filters for customizing how the logging |
---|
13 | * messages are displayed in the footer of a page. |
---|
14 | * |
---|
15 | * The reason you would want to replace this file |
---|
16 | * is if you want to log messages to files or to |
---|
17 | * a database (barring a failed connection to the |
---|
18 | * database). |
---|
19 | * |
---|
20 | * @since 2.5 |
---|
21 | * @package WordPress |
---|
22 | */ |
---|
23 | |
---|
24 | /** |
---|
25 | * Notice Severity for use in wp_log() third parameter. |
---|
26 | * |
---|
27 | * The notice severity are basic notices that let the user |
---|
28 | * know what WordPress is doing. They aren't WordPress |
---|
29 | * problems and should not be used for WordPress problems. |
---|
30 | * |
---|
31 | * An example, would be to note that WordPress is shutting |
---|
32 | * down and when WordPress starts up. Usually, these notes |
---|
33 | * will be disabled by debuggers seeking higher problems |
---|
34 | * and enabled by debuggers seeking more fine tuned |
---|
35 | * locations of problems. |
---|
36 | * |
---|
37 | * @since 2.5 |
---|
38 | */ |
---|
39 | define('WP_LOG_SEVERITY_NOTE', 1); |
---|
40 | |
---|
41 | /** |
---|
42 | * Low Severity level for use in wp_log third parameter. |
---|
43 | * |
---|
44 | * Low severity levels are reserved for areas that might |
---|
45 | * cause problems or areas that WordPress considers that |
---|
46 | * the user should look over in more depth. |
---|
47 | * |
---|
48 | * @since 2.5 |
---|
49 | */ |
---|
50 | define('WP_LOG_SEVERITY_LOW', 2); |
---|
51 | |
---|
52 | /** |
---|
53 | * Medium Severity level for use in wp_log third parameter. |
---|
54 | * |
---|
55 | * Medium severity levels are reserved for areas that |
---|
56 | * WordPres considers to be problems, but ignores or fixes |
---|
57 | * for the user anyway. The problem could cause problems, |
---|
58 | * but have a lower risk for causing damage. |
---|
59 | * |
---|
60 | * The user might want to consider taking action to resolve |
---|
61 | * the issue in the future, but generally are nothing to |
---|
62 | * worry about. |
---|
63 | * |
---|
64 | * @since 2.5 |
---|
65 | */ |
---|
66 | define('WP_LOG_SEVERITY_MEDIUM', 4); |
---|
67 | |
---|
68 | /** |
---|
69 | * Warning Severity level for use in wp_log third parameter. |
---|
70 | * |
---|
71 | * The user should seek to resolve the problem area as |
---|
72 | * quickly as possible as the problem poses some problem |
---|
73 | * area that should be addressed by the user. The problem |
---|
74 | * generally didn't cause any problem for the issuer and |
---|
75 | * was able to continue execution. |
---|
76 | * |
---|
77 | * @since 2.5 |
---|
78 | */ |
---|
79 | define('WP_LOG_SEVERITY_WARNING', 8); |
---|
80 | |
---|
81 | /** |
---|
82 | * High Severity level for use in wp_log third parameter. |
---|
83 | * |
---|
84 | * The issuer was able to continue execution or halted |
---|
85 | * execution, but did not bring down WordPress in the |
---|
86 | * process. WordPress was able to continue but the failed |
---|
87 | * process will continue to fail, unless the user does |
---|
88 | * something to fix the problem. |
---|
89 | * |
---|
90 | * The user should also report the problem to the |
---|
91 | * party that maintains the issuer process. |
---|
92 | * |
---|
93 | * @since 2.5 |
---|
94 | */ |
---|
95 | define('WP_LOG_SEVERITY_HIGH', 16); |
---|
96 | |
---|
97 | /** |
---|
98 | * Error Severity level for use in wp_log third parameter. |
---|
99 | * |
---|
100 | * The issuer halted the execution of its process and |
---|
101 | * took down the WordPress process as a result. The user |
---|
102 | * should report the problem to the maintainer of the |
---|
103 | * process which issued the error. |
---|
104 | * |
---|
105 | * @since 2.5 |
---|
106 | */ |
---|
107 | define('WP_LOG_SEVERITY_ERROR', 32); |
---|
108 | |
---|
109 | /** |
---|
110 | * XMLRPC logging constant |
---|
111 | * |
---|
112 | * Used for readability of XMLRPC messages. |
---|
113 | * |
---|
114 | * @since 2.5 |
---|
115 | */ |
---|
116 | define('WP_LOG_XMLRPC', 'xmlrpc'); |
---|
117 | |
---|
118 | /** |
---|
119 | * WordPress Object Cache logging constant |
---|
120 | * |
---|
121 | * Used for readability of Object Cache messages. |
---|
122 | * |
---|
123 | * @since 2.5 |
---|
124 | */ |
---|
125 | define('WP_LOG_CACHE', 'cache'); |
---|
126 | |
---|
127 | /** |
---|
128 | * WordPress Database logging constant |
---|
129 | * |
---|
130 | * Used for readability of WordPress Database messages. |
---|
131 | * |
---|
132 | * @since 2.5 |
---|
133 | */ |
---|
134 | define('WP_LOG_DATABASE', 'wpdb'); |
---|
135 | |
---|
136 | /** |
---|
137 | * WordPress plugins logging constant |
---|
138 | * |
---|
139 | * @since 2.5 |
---|
140 | */ |
---|
141 | define('WP_LOG_PLUGIN', 'plugin'); |
---|
142 | |
---|
143 | /** |
---|
144 | * PHP Errors logging constant |
---|
145 | * |
---|
146 | * @since 2.5 |
---|
147 | */ |
---|
148 | define('WP_LOG_PHP_ERROR', 'php_error'); |
---|
149 | |
---|
150 | /** |
---|
151 | * PHP 5 Exceptions logging constant |
---|
152 | * |
---|
153 | * @since 2.5 |
---|
154 | */ |
---|
155 | define('WP_LOG_EXCEPTIONS', 'exceptions'); |
---|
156 | |
---|
157 | /** |
---|
158 | * wp_log() - Log a message based on severity of the problem. |
---|
159 | * |
---|
160 | * @since 2.5 |
---|
161 | * @global array $wp_log_messages Stores all of the log messages |
---|
162 | * @uses has_filter() Checks to see if filters exist for preventing logging. |
---|
163 | * @uses apply_filters() Calls 'wp_log_severity' to see if the severity |
---|
164 | * should be logged. |
---|
165 | * @uses apply_filters() Calls 'wp_log_type' to see if type should be logged. |
---|
166 | * @uses apply_filters() Calls 'wp_log_$type_type' to see if a specific type |
---|
167 | * should be logged. |
---|
168 | * |
---|
169 | * @param string $type The area which the message was logged. |
---|
170 | * @param string $message The message to display to the user about the problem. |
---|
171 | * @param int $severity Severity level of the log message |
---|
172 | * @return bool False if failed to log message, true if message was logged. |
---|
173 | */ |
---|
174 | function wp_log($type, $message, $severity) { |
---|
175 | |
---|
176 | // If WP_DEBUG is not defined, then don't process log message |
---|
177 | if( !defined('WP_DEBUG') ) |
---|
178 | return false; |
---|
179 | |
---|
180 | global $wp_log_messages; |
---|
181 | |
---|
182 | if( !is_array( $wp_log_messages ) ) |
---|
183 | $wp_log_messages = array(); |
---|
184 | |
---|
185 | // Allow for a plugin (if plugin API is loaded) to prevent logging severities |
---|
186 | if( function_exists('has_filter') ) { |
---|
187 | if( has_filter('wp_log_severity') ) |
---|
188 | if ( false === apply_filters('wp_log_severity', true, $severity) ) |
---|
189 | return false; |
---|
190 | |
---|
191 | if( has_filter('wp_log_type') || has_filter("wp_log_{$type}_type") ) { |
---|
192 | $log_type = apply_filters('wp_log_type', true, $type); |
---|
193 | $log_type_with_type = apply_filters("wp_log_{$type}_type", true); |
---|
194 | |
---|
195 | if( false === $log_type || false === $log_type_with_type ) |
---|
196 | return false; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | $wp_log_messages[$type][$severity][] = array( $message, debug_backtrace() ); |
---|
201 | |
---|
202 | return true; |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * wp_plugin_log() - Logs plugin messages by severity and plugin name |
---|
207 | * |
---|
208 | * This is supposed to be used by plugin problems and assigns the log |
---|
209 | * messages to a general type with the plugin name that will be referenced |
---|
210 | * later. |
---|
211 | * |
---|
212 | * @param string $plugin Plugin Name |
---|
213 | * @param string $message Message to give the user about the plugin problem |
---|
214 | * @param int|string $severity Plugin log message severity of the problem |
---|
215 | * @return bool False if log message was not logged and true if message was logged. |
---|
216 | */ |
---|
217 | function wp_plugin_log($plugin, $message, $severity) { |
---|
218 | |
---|
219 | // If WP_DEBUG is not defined, then don't process log message |
---|
220 | if( !defined('WP_DEBUG') ) |
---|
221 | return false; |
---|
222 | |
---|
223 | global $wp_plugin_log_messages; |
---|
224 | |
---|
225 | if( !is_array( $wp_log_messages ) ) |
---|
226 | $wp_log_messages = array(); |
---|
227 | |
---|
228 | $wp_plugin_log_messages[$plugin][$severity][] = array( $message, debug_backtrace() ); |
---|
229 | |
---|
230 | return true; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * wp_log_shutdown() - Display the log messages to the user |
---|
235 | * |
---|
236 | * Will display the messages in the theme footer ('wp_footer'). |
---|
237 | * |
---|
238 | * @since 2.5 |
---|
239 | */ |
---|
240 | function wp_log_shutdown() { |
---|
241 | // If WP_DEBUG is not defined, then don't process log message |
---|
242 | if( !defined('WP_DEBUG') ) |
---|
243 | return; |
---|
244 | |
---|
245 | global $wp_log_messages, $wp_plugin_log_messages; |
---|
246 | |
---|
247 | if( !is_array($wp_log_messages) || empty($wp_log_messages) ) |
---|
248 | return; |
---|
249 | |
---|
250 | $log_content = ''; |
---|
251 | |
---|
252 | uasort($wp_log_messages, 'strnatcasecmp'); |
---|
253 | |
---|
254 | $log_content .= apply_filters('wp_log_display_wordpress', '<h1>WordPress Log Messages</h1>'); |
---|
255 | |
---|
256 | $log_content .= apply_filters('wp_log_display_start', '<div align="left" style="font-size: 10px;">'); |
---|
257 | $log_content .= wp_log_display( $wp_log_messages ); |
---|
258 | $log_content .= apply_filters('wp_log_display_end', '</div>'); |
---|
259 | |
---|
260 | $log_content .= apply_filters('wp_log_display_plugins', '<h1>Plugin Log Messages</h1>'); |
---|
261 | |
---|
262 | $log_content .= apply_filters('wp_log_display_start', '<div align="left" style="font-size: 10px;">'); |
---|
263 | $log_content .= wp_log_display( $wp_plugin_log_messages ); |
---|
264 | $log_content .= apply_filters('wp_log_display_end', '</div>'); |
---|
265 | |
---|
266 | echo $log_content; |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * wp_log_display() - Converts log message array to display form |
---|
271 | * |
---|
272 | * @since 2.5 |
---|
273 | * @param array $log_messages |
---|
274 | * @return string |
---|
275 | */ |
---|
276 | function wp_log_display($log_messages) { |
---|
277 | $log_content = ''; |
---|
278 | |
---|
279 | foreach( $log_messages as $type => $log_type_array ) { |
---|
280 | $log_type = apply_filters('wp_log_type', true, $type); |
---|
281 | $log_type_with_type = apply_filters("wp_log_{$type}_type", true); |
---|
282 | |
---|
283 | // Catch types that shouldn't have been logged. |
---|
284 | if( false === $log_type || false === $log_type_with_type ) |
---|
285 | continue; |
---|
286 | |
---|
287 | $log_content .= apply_filters('wp_log_display_type_start', "<h2>{$type}</h2><ul>", $type); |
---|
288 | |
---|
289 | uksort($log_type_array, 'strnatcasecmp'); |
---|
290 | |
---|
291 | foreach( $log_type_array as $severity => $log_messages ) { |
---|
292 | |
---|
293 | // Catch severity items that shouldn't be displayed |
---|
294 | if ( false === apply_filters('wp_log_severity', true, $severity) ) |
---|
295 | continue; |
---|
296 | |
---|
297 | $severity_display = wp_log_severity_display_name($severity); |
---|
298 | $log_content .= apply_filters('wp_log_display_severity_start', '<li><h3>' . $severity_display . '</h3><br /><br />', $severity, $severity_display); |
---|
299 | |
---|
300 | $log_content .= "<ol>\n"; |
---|
301 | |
---|
302 | foreach( $log_messages as $log_message ) { |
---|
303 | $log_content .= "<li>\n"; |
---|
304 | // See if we need to add the backtrace. |
---|
305 | $log_content .= '<strong>'. $log_message[0] .'</strong><br /><br />'; |
---|
306 | $backtrace = apply_filters('wp_log_backtrace', $log_message[1]); |
---|
307 | |
---|
308 | // Format the backtrace display |
---|
309 | $log_content .= "<table style=\"font-size: 10px;\">\n<tr><th>file</th><th>line</th><th>function</th><th>args</th></tr>\n"; |
---|
310 | |
---|
311 | for($i=0; $i < count($backtrace); ++$i) { |
---|
312 | if( 'wp_log' == $backtrace_message['function'] ) |
---|
313 | continue; |
---|
314 | |
---|
315 | $log_content .= "<tr". ( ($i%2) == 0 ? ' style="background-color:#ffffff;"' : '') .">\n"; |
---|
316 | $log_content .= "<td>". $backtrace[$i]['file'] ."</td>\n"; |
---|
317 | $log_content .= "<td>". $backtrace[$i]['line'] ."</td>\n"; |
---|
318 | $log_content .= "<td>". $backtrace[$i]['function'] ."</td>\n"; |
---|
319 | $log_content .= "<td>". implode("<br />\n", $backtrace[$i]['args']) ."</td>\n"; |
---|
320 | $log_content .= "</tr>"; |
---|
321 | } |
---|
322 | |
---|
323 | $log_content .= "</table><br /><br />\n"; |
---|
324 | $log_content .= "</li>\n"; |
---|
325 | } |
---|
326 | $log_content .= "</ol>\n"; |
---|
327 | |
---|
328 | $log_content .= apply_filters('wp_log_display_severity_end', '</li>'); |
---|
329 | } |
---|
330 | |
---|
331 | $log_content .= apply_filters('wp_log_display_type_end', "</ul>\n\n"); |
---|
332 | } |
---|
333 | |
---|
334 | return $log_content; |
---|
335 | } |
---|
336 | |
---|
337 | /** |
---|
338 | * wp_log_severity_display_name() - Converts numeric severity to string |
---|
339 | * |
---|
340 | * @param int $severity |
---|
341 | * @return string Name of the severity |
---|
342 | */ |
---|
343 | function wp_log_severity_display_name($severity) { |
---|
344 | switch($severity) { |
---|
345 | case WP_LOG_SEVERITY_NOTE: return 'note'; |
---|
346 | case WP_LOG_SEVERITY_LOW: return 'low'; |
---|
347 | case WP_LOG_SEVERITY_MEDIUM: return 'medium'; |
---|
348 | case WP_LOG_SEVERITY_WARNING: return 'warning'; |
---|
349 | case WP_LOG_SEVERITY_HIGH: return 'high'; |
---|
350 | case WP_LOG_SEVERITY_ERROR: return 'error'; |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | function wp_log_error_handler($iError, $strError, $strFile, $strLine, $arrContext) { |
---|
355 | |
---|
356 | // If WP_DEBUG is not defined, then don't process error message |
---|
357 | if( !defined('WP_DEBUG') ) |
---|
358 | return false; |
---|
359 | |
---|
360 | global $wp_log_messages; |
---|
361 | |
---|
362 | if( defined('E_RECOVERABLE_ERROR') && E_RECOVERABLE_ERROR === $iError ) { |
---|
363 | $wp_log_messages[WP_LOG_PHP_ERROR][WP_LOG_SEVERITY_HIGH][] = array( |
---|
364 | $strError, |
---|
365 | array_merge( |
---|
366 | array(array( |
---|
367 | 'file' => $strFile, |
---|
368 | 'line' => $strLine, |
---|
369 | 'function' => '', |
---|
370 | 'args' => array() |
---|
371 | )), |
---|
372 | (array) debug_backtrace() |
---|
373 | ) |
---|
374 | ); |
---|
375 | return true; |
---|
376 | } |
---|
377 | |
---|
378 | switch( $iError ) { |
---|
379 | case E_ERROR: |
---|
380 | $wp_log_messages[WP_LOG_PHP_ERROR][WP_LOG_SEVERITY_ERROR][] = array( |
---|
381 | $strError, |
---|
382 | array_merge( |
---|
383 | array(array( |
---|
384 | 'file' => $strFile, |
---|
385 | 'line' => $strLine, |
---|
386 | 'function' => '', |
---|
387 | 'args' => array() |
---|
388 | )), |
---|
389 | (array) debug_backtrace() |
---|
390 | ) |
---|
391 | ); |
---|
392 | wp_log_shutdown(); |
---|
393 | wp_die(); |
---|
394 | break; |
---|
395 | |
---|
396 | case E_USER_ERROR: |
---|
397 | $wp_log_messages[WP_LOG_PHP_ERROR][WP_LOG_SEVERITY_HIGH][] = array( |
---|
398 | $strError, |
---|
399 | array_merge( |
---|
400 | array(array( |
---|
401 | 'file' => $strFile, |
---|
402 | 'line' => $strLine, |
---|
403 | 'function' => '', |
---|
404 | 'args' => array() |
---|
405 | )), |
---|
406 | (array) debug_backtrace() |
---|
407 | ) |
---|
408 | ); |
---|
409 | var_dump($wp_log_messages); |
---|
410 | break; |
---|
411 | |
---|
412 | case E_USER_WARNING: |
---|
413 | case E_WARNING: |
---|
414 | $wp_log_messages[WP_LOG_PHP_ERROR][WP_LOG_SEVERITY_WARNING][] = array( |
---|
415 | $strError, |
---|
416 | array_merge( |
---|
417 | array(array( |
---|
418 | 'file' => $strFile, |
---|
419 | 'line' => $strLine, |
---|
420 | 'function' => '', |
---|
421 | 'args' => array() |
---|
422 | )), |
---|
423 | (array) debug_backtrace() |
---|
424 | ) |
---|
425 | ); |
---|
426 | break; |
---|
427 | |
---|
428 | case E_USER_NOTICE: |
---|
429 | case E_NOTICE: |
---|
430 | $wp_log_messages[WP_LOG_PHP_ERROR][WP_LOG_SEVERITY_LOW][] = array( |
---|
431 | $strError, |
---|
432 | array_merge( |
---|
433 | array(array( |
---|
434 | 'file' => $strFile, |
---|
435 | 'line' => $strLine, |
---|
436 | 'function' => '', |
---|
437 | 'args' => array() |
---|
438 | )), |
---|
439 | (array) debug_backtrace() |
---|
440 | ) |
---|
441 | ); |
---|
442 | break; |
---|
443 | default: |
---|
444 | return false; |
---|
445 | } |
---|
446 | |
---|
447 | return true; |
---|
448 | } |
---|
449 | |
---|
450 | set_error_handler( 'wp_log_error_handler', E_ALL ); |
---|
451 | |
---|
452 | if( function_exists('set_exception_handler') ) : |
---|
453 | function wp_log_exception_handler($exception) { |
---|
454 | wp_log(WP_LOG_EXCEPTIONS, $exception->getMessage(), WP_LOG_SEVERITY_HIGH); |
---|
455 | } |
---|
456 | |
---|
457 | set_exception_handler('wp_log_exception_handler'); |
---|
458 | endif; |
---|
459 | |
---|
460 | ?> |
---|