2010-07-09 7 views
6

Ok Logging Class에 다른 질문이 HERE 있지만 로그 파일 항목에 호출 스크립트의 줄 번호를 추가 할 수 있기를 원했습니다.PHP 로깅 이벤트에서 줄 번호 가져 오기

나는 이걸 보았습니다 __Line __ 그러나 이것이 제가있는 줄 번호를 알려줍니다.

예 :

a.php에 디버그 내 Logger.php 클래스 이제

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

() 나는 예를 들어 라인 # 300에 매직 상수 __ __Line를 사용합니다. 스크립트를 실행할 때 로그 항목이 '20 행'에서 읽히길 원하지만 '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; 
     }  
    } 

답변

12

아래에 근무 당신은 함수에 항상 (__LINE__와) 라인을 통과 다른이에 대한 debug_backtrace 이상을 사용해야합니다.

+4

+1'debug_backtrace'는 slooow이므로 디버깅 모드에서만 사용해야합니다. –