ठीक है मेरे पास लॉगिंग क्लास के लिए HERE एक और प्रश्न है, लेकिन मैं लॉग फ़ाइल प्रविष्टि में कॉलिंग स्क्रिप्ट की लाइन संख्या जोड़ना चाहता हूं।PHP लॉगिंग ईवेंट से लाइन नंबर प्राप्त करें
मैंने __Line __ देखा है, लेकिन यह मुझे उस लाइन की लाइन संख्या देता है जहां यह है।
उदाहरण:
a.php
$log = new Logger();
$log->debug('hello'); // Say this is line #20
अब डिबग में मेरी Logger.php कक्षा में
() मैं के लिए उदाहरण लाइन # 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;
}
}
+1 नोट करें कि 'debug_backtrace' slooow है, इसलिए इसे वास्तव में केवल डीबगिंग मोड में उपयोग किया जाना चाहिए। –