清除日志的堆栈跟踪

Clean stack trace for logs

我有几个 classes(a、b、c 等)扩展了一个名为 mother 的抽象 class。所有 "sons" 使用方法 "save":

<?php

class mother {
    public function save() {
        echo "Mother saves!\n";
        debug_print_backtrace();
    }
}

class a extends mother {
    public function save() {
        echo "Calling save from A\n";
        parent::save();
    }
}

$test = new a;
$test->save();

?>

如果你运行这段代码,debug_print_backtrace的结果非常干净,这正是我需要的:

#0 mother->save() called at [/home/xfiddlec/public_html/main/code_44364601.php:13] #1 a->save() called at [/home/xfiddlec/public_html/main/code_44364601.php:18]

问题是,如果您使用的是框架(我使用的是 Zend2),堆栈跟踪将超过 1MB,这是一个巨大的字符串。如果有什么方法可以限制跟踪覆盖范围?对于我的具有文件的应用程序,class 的行和扩展母亲的名称就足够了。

如果我理解正确,答案很简单。抛出并捕获异常

try{
    throw new Exception();
}catch( Exception $e ){
    echo $e->getTraceAsString();
}

这是我用来调试的绝妙技巧。您也可以将跟踪作为数组获取,然后在其上使用切片或诸如此类的东西。