Netleştirmek için:
Bana kolayca mesajları oturum sağlayan bir Logger sınıfı inşa ediyorum:
lib.Logger.php:
<?php
class Logger {
private $handle;
public function __construct($log_name, $log_path) {
if ( ! is_dir($log_path))
throw new Exception('Log path does not exist.');
if ( ! in_array(strtolower(substr($log_name, 0, -4)), array('.log', '.txt')))
$log_name = "{$log_name}.log";
$this->handle = fopen("{$log_path}/{$log_name}", 'a');
$this->log('------------- Initializing ------------- '.get_parent_class($this));
}
// --------------------------------------------------------------------
public function __destruct() {
fclose($this->handle);
}
// --------------------------------------------------------------------
public function log($message) {
$time = date(DATE_RFC822);
$log = "[{$time}] {$message}\n";
fwrite($this->handle, $log);
}
}
?>
Ve ben kullanarak bu çağırır:
MyController.php:
<?php
class MyController extends Controller {
$logger = new Logger('testlog','/path/to/logs/');
$logger->log('Logs are fun!');
}
?>
Ben nesnesi başlatılamıyor zaman:
$this->log('------------- Initializing ------------- '.get_parent_class($this));
Bu durumda, ya
MyControllerveya
/path/to/MyController.php- Ben log () çağrılırken olduğunu nesne (veya dosyayı) adı oturum istiyorum.
I) (get_parent_class kullanarak çalıştı, ancak Logger başına bir üst sınıfı yok, çünkü elbette bu işe yaramazsa.
Herhangi bir fikir? Yardımlarınız için çok teşekkür ederim!
Alex B