PHP 로깅 이벤트에서 줄 번호를 가져옵니다
-
13-09-2020 - |
문제
ok 여기 < / a> 내 로깅 클래스의 경우, 통화 스크립트의 행 번호를 로그 파일 항목에 추가 할 수있었습니다.
예 :
a.php
$log = new Logger();
$log->debug('hello'); // Say this is line #20
.
지금 내 logger.php 클래스에서 debug ()에서 __ line __ 매직 일정 예를 들어 LINE # 300. 스크립트를 실행하면 로그 항목을 읽으려면 로그 항목을 읽으려면 '행 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 (__LINE__
포함)을 함수에 전달합니다.
다른 팁
debug_backtrace () 를 보았습니다.나는 그 오버 헤드를 확신하지 못한다.
제휴하지 않습니다 StackOverflow