質問

OK私は別の質問をしていますこちら< / a>ロギングクラスの場合は、ログファイルエントリに呼び出しスクリプトの行番号を追加できるようになりました。

私は __line __しかしこれは私にこれがある行の行番号を与えます。

例:

a.php

$log = new Logger();
$log->debug('hello'); // Say this is line #20
.

Logger.phpクラスで、debug()で __ line __魔法定数は、例えば行#300です。スクリプトを実行すると、ログエントリは1行目に読み込まれていますが、それは「行300」を読みます。回線番号を関数に渡すことは、これを行うことができる他の方法がありますか?

例デバッグクラス関数

public function debug($message) {
        if(DEBUG) {
            $this->calling_script = $this->getScriptBaseName();
            $this->log_file = LOG_FILE_DIRECTORY."/".$this->calling_script.".log";
            $this->fh = fopen($this->log_file, 'a') or die("Can't open log file: ".$this->log_file);

            if($this->first_run) {
                $this->log_entry = "\n[" . date("Y-m-d H:i:s", mktime()) . "][debug][line:".__LINE__."]:\t".$message."\n";
            } else {
                $this->log_entry = "[" . date("Y-m-d H:i:s", mktime()) . "][debug][line:".__LINE__."]:\t".$message."\n";
            }       
            fwrite($this->fh, $this->log_entry);
            fclose($this->fh);

            $this->first_run = false;
        }       
    }
.

編集:debug_backtrace()は素晴らしい動作!下の作業

public function debug($message) {
        if(DEBUG) {
            $debug_arr = debug_backtrace();
            $this->calling_script = $this->getScriptBaseName();
            $this->log_file = LOG_FILE_DIRECTORY."/".$this->calling_script.".log";
            $this->fh = fopen($this->log_file, 'a') or die("Can't open log file: ".$this->log_file);

            if($this->first_run) {
                $this->log_entry = "\n[" . date("Y-m-d H:i:s", mktime()) . "][debug]:\t".$message." [line:".$debug_arr[0]['line']."]\n";
            } else {
                $this->log_entry = "[" . date("Y-m-d H:i:s", mktime()) . "][debug]:\t".$message." [line:".$debug_arr[0]['line']."]\n";
            }       
            fwrite($this->fh, $this->log_entry);
            fclose($this->fh);

            $this->first_run = false;
        }       
    }
.

役に立ちましたか?

解決

debug_backtrace を使用する必要があります。そうでなければ(__LINE__)は常に関数に渡します。

他のヒント

あなたは debug_backtrace()?私はそのオーバーヘッドがわからない。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top