Ok Mam inne pytanie HERE dla mojej klasy logowania, ale chciałem móc dodać numer linii wywołującego skryptu do wpisu pliku dziennika.PHP pobiera numer linii z rejestru zdarzeń
Widziałem __Line __, ale to daje mi numer wiersza linii, w którym to jest.
Przykład:
a.php
$log = new Logger();
$log->debug('hello'); // Say this is line #20
Teraz w mojej klasie Logger.php w debug() Używam __Line __ Magii stałą na na przykład linii # 300. Po uruchomieniu skryptu chciałbym, aby wpis w dzienniku był czytany "on line 20", ale czytał "on line 300". Poza przekazaniem numeru linii do funkcji jest jakiś inny sposób, w jaki mógłbym to zrobić?
Przykład funkcja klasy debugowania
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;
}
}
EDIT: debug_backtrace() działa świetnie !!! Praca poniżej
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;
}
}
+1 Zwróć uwagę, że 'debug_backtrace' jest slooow, więc powinno być naprawdę używane tylko w trybie debugowania. –