| 5279 | |
| 5280 | /** |
| 5281 | * |
| 5282 | * Send debug messages to default log or die on critical errors |
| 5283 | * |
| 5284 | * Every message sent with priority >= 3 ( LOG_ERR ) will trigger and immediate |
| 5285 | * wp_die and display the error. |
| 5286 | * |
| 5287 | * All levels below this will be sent via error_log. |
| 5288 | * |
| 5289 | * The output level can be controlled by WP_DEBUG_LEVEL constant. |
| 5290 | * |
| 5291 | * @param string $message |
| 5292 | * @param int $level |
| 5293 | * |
| 5294 | * @output log to syslog | wp_die on high level |
| 5295 | * @return false on not taking action, true on log sent |
| 5296 | */ |
| 5297 | function wp_debug( $message, $level = LOG_NOTICE ) { |
| 5298 | if ( empty( $message ) ) |
| 5299 | return false; |
| 5300 | |
| 5301 | if ( @is_array( $message ) || @is_object ( $message ) ) |
| 5302 | $message = json_encode( $message ); |
| 5303 | |
| 5304 | $levels = array ( |
| 5305 | LOG_EMERG => 0, // Emergency system is unusable |
| 5306 | LOG_ALERT => 1, // Alert action must be taken immediately |
| 5307 | LOG_CRIT => 2, // Critical critical conditions |
| 5308 | LOG_ERR => 3, // Error error conditions |
| 5309 | LOG_WARNING => 4, // Warning warning conditions |
| 5310 | LOG_NOTICE => 5, // Notice normal but significant condition |
| 5311 | LOG_INFO => 6, // Informational informational messages |
| 5312 | LOG_DEBUG => 7, // Debug debug-level messages |
| 5313 | ); |
| 5314 | |
| 5315 | // number for number based comparison |
| 5316 | // should work with the defines only, this is just a make-it-sure step |
| 5317 | $level = $levels [ $level ]; |
| 5318 | |
| 5319 | // in case WordPress debug log has a minimum level |
| 5320 | if ( defined ( 'WP_DEBUG_LEVEL' ) ) { |
| 5321 | $wp_level = $levels [ WP_DEBUG_LEVEL ]; |
| 5322 | if ( $level > $wp_level ) { |
| 5323 | return false; |
| 5324 | } |
| 5325 | } |
| 5326 | |
| 5327 | // ERR, CRIT, ALERT and EMERG |
| 5328 | if ( 3 >= $level ) { |
| 5329 | wp_die( '<h1>Error:</h1>' . '<p>' . $message . '</p>' ); |
| 5330 | exit; |
| 5331 | } |
| 5332 | |
| 5333 | $trace = debug_backtrace(); |
| 5334 | $caller = $trace[1]; |
| 5335 | $parent = $caller['function']; |
| 5336 | |
| 5337 | if (isset($caller['class'])) |
| 5338 | $parent = $caller['class'] . '::' . $parent; |
| 5339 | |
| 5340 | return error_log( "{$parent}: {$message}" ); |
| 5341 | } |
| 5342 | No newline at end of file |