向WP_DEBUG添加更多信息

时间:2017-03-18 10:47:45

标签: php wordpress debugging logging

当我的Wordpress网站正在制作时,我已将wp_config.php设置如下:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

是否有机会,我可以将任何信息附加到日志中,p.e。用户ID或页面slug?无法在谷歌上找到任何调试过滤器。

我通过创建我自己的错误处理程序来解决这个问题,看起来如下:

set_error_handler('custom_error_log');
function custom_error_log ($errno, $errstr, $errfile, $errline) {
    global $post, $current_user;
    //Message
        /*Is there a function to get rid of this switch statement?*/
    switch ($errno) {
        case 2:
            $type = "WARNING";
            break;
        case 8:
            $type = "NOTICE";
            break;
        case 256:
            $type = "FATAL ERROR";
            break;
        case 512:
            $type = "USER_WARNING";
            break;
        case 1024:
            $type = "USER_NOTICE";
            break;
        default:
            $type = "[$errno]";
    }
    $message = "$errstr";

    //Location
        $location = str_replace(get_template_directory().'/','',$errfile);

    //Cause
    if ($current_user->ID) {
        $user = "User {$current_user->ID}";
    } else {
        $user = "Guest";
    }
    $cause ="$user on '{$post->post_title}'";

    //Log
    $log = "$type\n  $message | $location($errline) | $cause";
    error_log($log);

    //Post Log to wherever else you want to, p.e. Slack.
}

0 个答案:

没有答案