Make WordPress Core

Ticket #5669: debug.php

File debug.php, 9.9 KB (added by darkdragon, 17 years ago)

Finished wp_log() function in new debug.php file.

Line 
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 */
39define('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 */
50define('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 */
66define('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 */
79define('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 */
95define('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 */
107define('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 */
116define('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 */
125define('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 */
134define('WP_LOG_DATABASE', 'wpdb');
135
136/**
137 * WordPress plugins logging constant
138 *
139 * @since 2.5
140 */
141define('WP_LOG_PLUGIN', 'plugin');
142
143/**
144 * wp_log() - Log a message based on severity of the problem.
145 *
146 * @since 2.5
147 * @global array $wp_log_messages Stores all of the log messages
148 * @uses has_filter() Checks to see if filters exist for preventing logging.
149 * @uses apply_filters() Calls 'wp_log_severity' to see if the severity
150 *              should be logged.
151 * @uses apply_filters() Calls 'wp_log_type' to see if type should be logged.
152 * @uses apply_filters() Calls 'wp_log_$type_type' to see if a specific type
153 *              should be logged.
154 *
155 * @param string $type The area which the message was logged.
156 * @param string $message The message to display to the user about the problem.
157 * @param int $severity Severity level of the log message
158 * @return bool False if failed to log message, true if message was logged.
159 */
160function wp_log($type, $message, $severity) {
161
162        // If WP_DEBUG is not defined, then don't process log message
163        if( !defined('WP_DEBUG') )
164                return false;
165
166        global $wp_log_messages;
167
168        if( !is_array( $wp_log_messages ) )
169                $wp_log_messages = array();
170
171        // Allow for a plugin (if plugin API is loaded) to prevent logging severities
172        if( function_exists('has_filter') ) {
173                if( has_filter('wp_log_severity') )
174                        if ( false === apply_filters('wp_log_severity', true, $severity) )
175                                return false;
176
177                if( has_filter('wp_log_type') || has_filter("wp_log_{$type}_type") ) {
178                        $log_type = apply_filters('wp_log_type', true, $type);
179                        $log_type_with_type = apply_filters("wp_log_{$type}_type", true);
180
181                        if( false === $log_type || false === $log_type_with_type )
182                                return false;
183                }
184        }
185
186        $wp_log_messages[$type][$severity][] = array( $message, debug_backtrace() );
187
188        return true;
189}
190
191/**
192 * wp_plugin_log() - Logs plugin messages by severity and plugin name
193 *
194 * This is supposed to be used by plugin problems and assigns the log
195 * messages to a general type with the plugin name that will be referenced
196 * later.
197 *
198 * @param string $plugin Plugin Name
199 * @param string $message Message to give the user about the plugin problem
200 * @param int|string $severity Plugin log message severity of the problem
201 * @return bool False if log message was not logged and true if message was logged.
202 */
203function wp_plugin_log($plugin, $message, $severity) {
204
205        // If WP_DEBUG is not defined, then don't process log message
206        if( !defined('WP_DEBUG') )
207                return false;
208
209        global $wp_plugin_log_messages;
210
211        if( !is_array( $wp_log_messages ) )
212                $wp_log_messages = array();
213
214        $wp_plugin_log_messages[$plugin][$severity][] = array( $message, debug_backtrace() );
215
216        return true;
217}
218
219/**
220 * wp_log_shutdown() - Display the log messages to the user
221 *
222 * Will display the messages in the theme footer ('wp_footer').
223 *
224 * @since 2.5
225 */
226function wp_log_shutdown() {
227        // If WP_DEBUG is not defined, then don't process log message
228        if( !defined('WP_DEBUG') )
229                return;
230
231        global $wp_log_messages, $wp_plugin_log_messages;
232
233        if( !is_array($wp_log_messages) || empty($wp_log_messages) )
234                return;
235
236        $log_content = '';
237
238        uasort($wp_log_messages, 'strnatcasecmp');
239
240        $log_content .= apply_filters('wp_log_display_wordpress', '<h1>WordPress Log Messages</h1>');
241
242        $log_content .= apply_filters('wp_log_display_start', '');
243        $log_content .= wp_log_display( $wp_log_messages );
244        $log_content .= apply_filters('wp_log_display_end', '');
245
246        $log_content .= apply_filters('wp_log_display_plugins', '<h1>Plugin Log Messages</h1>');
247
248        $log_content .= apply_filters('wp_log_display_start', '');
249        $log_content .= wp_log_display( $wp_plugin_log_messages );
250        $log_content .= apply_filters('wp_log_display_end', '');
251
252        echo $log_content;
253}
254
255/**
256 * wp_log_display() - Converts log message array to display form
257 *
258 * @since 2.5
259 * @param array $log_messages
260 * @return string
261 */
262function wp_log_display($log_messages) {
263        $log_content = '';
264
265        foreach( $wp_log_messages as $type => $log_type ) {
266                $log_type = apply_filters('wp_log_type', true, $type);
267                $log_type_with_type = apply_filters("wp_log_{$type}_type", true);
268
269                // Catch types that shouldn't have been logged.
270                if( false === $log_type || false === $log_type_with_type )
271                        continue;
272
273                $log_content .= apply_filters('wp_log_display_type_start', "<h2>{$type}</h2><ul>", $type);
274
275                uasort($log_type, 'strnatcasecmp');
276
277                foreach( $log_type as $severity => $log_messages ) {
278
279                        // Catch severity items that shouldn't be displayed
280                        if ( false === apply_filters('wp_log_severity', true, $severity) )
281                                continue;
282
283                        $severity_display = wp_log_severity_display_name($severity);
284                        $log_content .= apply_filters('wp_log_display_severity_start', '<li>' . $severity_display, $severity, $severity_display);
285
286                        foreach( $log_message_items as $log_message ) {
287                                // See if we need to add the backtrace.
288                                $log_content .= $log_message[0];
289                                $backtrace = apply_filters('wp_log_backtrace', $log_message[1]);
290                               
291                                // Format the backtrace display
292                                $log_content .= "<table>\n<tr><th>file</th><th>line</th><th>function</th><th>args</th></tr>\n";
293
294                                foreach( (array) $backtrace as $backtrace_message ) {
295                                        if( 'wp_log' == $backtrace_message['function'] )
296                                                continue;
297
298                                        $log_content .= "<tr>\n";
299                                        $log_content .= "<td>". $backtrace_message['file'] ."</td>\n";
300                                        $log_content .= "<td>". $backtrace_message['line'] ."</td>\n";
301                                        $log_content .= "<td>". $backtrace_message['function'] ."</td>\n";
302                                        $log_content .= "<td>". implode("<br />\n", $backtrace_message['args']) ."</td>\n";
303                                        $log_content .= "</tr>";
304                                }
305
306                                $log_content .= "</table>\n";
307                        }
308
309                        $log_content .= apply_filters('wp_log_display_severity_end', '</li>');
310                }
311
312                $log_content .= apply_filters('wp_log_display_type_end', "</ul>\n\n");
313        }
314
315        return $log_content;
316}
317
318/**
319 * wp_log_severity_display_name() - Converts numeric severity to string
320 *
321 * @param int $severity
322 * @return string Name of the severity
323 */
324function wp_log_severity_display_name($severity) {
325        switch($severity) {
326                case WP_LOG_SEVERITY_NOTE: return 'note';
327                case WP_LOG_SEVERITY_LOW: return 'low';
328                case WP_LOG_SEVERITY_MEDIUM: return 'medium';
329                case WP_LOG_SEVERITY_WARNING: return 'warning';
330                case WP_LOG_SEVERITY_HIGH: return 'high';
331                case WP_LOG_SEVERITY_ERROR: return 'error';
332        }
333}
334
335?>