清理日志的堆栈跟踪


Clean stack trace for logs

我有几个类(a,b,c等)扩展了一个名为mother的抽象类。所有"儿子"都使用"保存"方法:

<?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,一个巨大的字符串。如果有办法限制跟踪覆盖范围?对于我的应用程序,具有扩展母的类的行和名称就足够了。

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

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

这是我用于调试的一个巧妙技巧。您也可以将跟踪作为数组获取,并在其上使用切片或其他内容。