PHPロギングイベントからの行番号を取得します
-
13-09-2020 - |
質問
OK私は別の質問をしていますこちら< / a>ロギングクラスの場合は、ログファイルエントリに呼び出しスクリプトの行番号を追加できるようになりました。
私は __line __しかしこれは私にこれがある行の行番号を与えます。
例:
a.php
$log = new Logger();
$log->debug('hello'); // Say this is line #20
.
Logger.phpクラスで、debug()で
例デバッグクラス関数
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()?私はそのオーバーヘッドがわからない。
所属していません StackOverflow