Make WordPress Core

Ticket #5669: debug.3.php

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

wp-includes debug.php file which contains the WordPress Logging API

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 * PHP Errors logging constant
145 *
146 * @since 2.5
147 */
148define('WP_LOG_PHP_ERROR', 'php_error');
149
150/**
151 * PHP 5 Exceptions logging constant
152 *
153 * @since 2.5
154 */
155define('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 */
174function 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 */
217function 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_severity_display_name() - Converts numeric severity to string
235 *
236 * @param int $severity
237 * @return string Name of the severity
238 */
239function wp_log_severity_display_name($severity) {
240        switch($severity) {
241                case WP_LOG_SEVERITY_NOTE:              return _c('note|severity_note');
242                case WP_LOG_SEVERITY_LOW:               return _c('low|severity_note');
243                case WP_LOG_SEVERITY_MEDIUM:    return _c('medium|severity_note');
244                case WP_LOG_SEVERITY_WARNING:   return _c('warning|severity_note');
245                case WP_LOG_SEVERITY_HIGH:              return _c('high|severity_note');
246                case WP_LOG_SEVERITY_ERROR:             return _c('error|severity_note');
247        }
248}
249
250/**
251 * wp_log_error_handler() - Handle Errors thrown
252 *
253 * @param int $iError
254 * @param string $strError
255 * @param string $strFile
256 * @param string $strLine
257 * @param array $arrContext
258 * @return bool
259 */
260function wp_log_error_handler($iError, $strError, $strFile, $strLine, $arrContext) {
261
262        // If WP_DEBUG is not defined, then don't process error message
263        if( !defined('WP_DEBUG') )
264                return false;
265
266        global $wp_log_messages;
267
268        if( defined('E_RECOVERABLE_ERROR') && E_RECOVERABLE_ERROR === $iError ) {
269                $wp_log_messages[WP_LOG_PHP_ERROR][WP_LOG_SEVERITY_HIGH][] = array(
270                        $strError,
271                        array_merge(
272                                array(array(
273                                'file' => $strFile,
274                                'line' => $strLine,
275                                'function' => '',
276                                'args' => array()
277                                )),
278                                (array) debug_backtrace()
279                        )
280                );
281                return true;
282        }
283
284        switch( $iError ) {
285                case E_ERROR:
286                        $wp_log_messages[WP_LOG_PHP_ERROR][WP_LOG_SEVERITY_ERROR][] = array(
287                                $strError,
288                                array_merge(
289                                        array(array(
290                                        'file' => $strFile,
291                                        'line' => $strLine,
292                                        'function' => '',
293                                        'args' => array()
294                                        )),
295                                        (array) debug_backtrace()
296                                )
297                        );
298                        wp_log_shutdown();
299                        wp_die();
300                        break;
301
302                case E_USER_ERROR:
303                        $wp_log_messages[WP_LOG_PHP_ERROR][WP_LOG_SEVERITY_HIGH][] = array(
304                                $strError,
305                                array_merge(
306                                        array(array(
307                                        'file' => $strFile,
308                                        'line' => $strLine,
309                                        'function' => '',
310                                        'args' => array()
311                                        )),
312                                        (array) debug_backtrace()
313                                )
314                        );
315                        var_dump($wp_log_messages);
316                        break;
317
318                case E_USER_WARNING:
319                case E_WARNING:
320                        $wp_log_messages[WP_LOG_PHP_ERROR][WP_LOG_SEVERITY_WARNING][] = array(
321                                $strError,
322                                array_merge(
323                                        array(array(
324                                        'file' => $strFile,
325                                        'line' => $strLine,
326                                        'function' => '',
327                                        'args' => array()
328                                        )),
329                                        (array) debug_backtrace()
330                                )
331                        );
332                        break;
333
334                case E_USER_NOTICE:
335                case E_NOTICE:
336                        $wp_log_messages[WP_LOG_PHP_ERROR][WP_LOG_SEVERITY_LOW][] = array(
337                                $strError,
338                                array_merge(
339                                        array(array(
340                                        'file' => $strFile,
341                                        'line' => $strLine,
342                                        'function' => '',
343                                        'args' => array()
344                                        )),
345                                        (array) debug_backtrace()
346                                )
347                        );
348                        break;
349                default:
350                        return false;
351        }
352
353        return true;
354}
355
356set_error_handler( 'wp_log_error_handler', E_ALL );
357
358/**
359 * wp_log_exception_handler() - Handle Exceptions thrown
360 *
361 * @param Exception $exception Exception
362 */
363function wp_log_exception_handler($exception) {
364        wp_log(WP_LOG_EXCEPTIONS, $exception->getMessage(), WP_LOG_SEVERITY_HIGH);
365}
366
367if( function_exists('set_exception_handler') )
368        set_exception_handler('wp_log_exception_handler');
369
370?>