如何在PHP中记录调用当前函数的文件/行的名称?

时间:2009-12-10 12:13:28

标签: php logging error-logging

我正在php中编写一个轻量级的日志记录类。 如何自动记录调用Log类中函数的文件名/函数/行号,并保存行号,时间和内容,而不会让用户麻烦输入$__LINE__$__FILE__每次他们调用这个功能。有什么我可以隐含地称之为。

示例:

class Log {
static private $instance;
static $logfile;
private function __construct(){
    // doesn't need to do anything
    // one logging object to avoid/control future/potential race conditions
}
public function getInstance(){
    if(!Self::$instance) {
        Self::$instance = new Log();
    } else {
        return Self::$instance;
    }
}

public function criticalLog($string){
    // I want this to be logging line number, filename

}
}

现在我想能够像文件api.php第15行一样

logInstance.criticalLog("This log is important");

在我的日志文件中,我想看看

12/10/09-11:15 api.php 15 This log is important

有什么想法吗?我看了一下PEAR和log4php解决方案,但它们似乎非常重要我想要的东西。

2 个答案:

答案 0 :(得分:2)

看看debug_backtrace功能,它会显示它提供您需要的所有信息:

function, line, file, class, object, type, args

答案 1 :(得分:1)

相关问题