php利用單例模式實現Tlog類庫_PHP教程

來源:互聯網
上載者:User
對於現在的應用程式來說,日誌的重要性是不言而喻的。很難想象沒有任何日誌記錄功能的應用程式運行在生產環境中。日誌所能提供的功能是多種多樣的,包括記錄程式運行時產生的錯誤資訊、狀態資訊、調試資訊和執行時間資訊等。在生產環境中,日誌是尋找問題來源的重要依據。應用程式運行時的產生的各種資訊,都應該通過日誌類庫來進行記錄。

複製代碼 代碼如下:
/**
* Tlog類
*
* @since alpha 0.0.1
* @date 2014.03.04
* @author genialx
*
*/

class Log{

//單例模式
private static $instance = NULL;
//檔案控制代碼
private static $handle = NULL;
//日誌開關
private $log_switch = NULL;
//日誌相對目錄
private $log_file_path = NULL;
//記錄檔最大長度,超出長度重建立立檔案
private $log_max_len = NULL;
//記錄檔首碼,入 log_0
private $log_file_pre = 'log_';


/**
* 建構函式
*
* @since alpha 0.0.1
* @date 2014.02.04
* @author genialx
*/
protected function __construct(){//注意:以下是設定檔中的常量,請讀者自行更改

$this->log_file_path = LOG_FILE_PATH;

$this->log_switch = LOG_SWITCH;

$this->log_max_len = LOG_MAX_LEN;

}

/**
* 單利模式
*
* @since alpha 0.0.1
* @date 2014.02.04
* @author genialx
*/
public static function get_instance(){
if(!self::$instance instanceof self){
self::$instance = new self;
}
return self::$instance;
}

/**
*
* 日誌記錄
*
* @param int $type 0 -> 記錄(THING LOG) / 1 -> 錯誤(ERROR LOG)
* @param string $desc
* @param string $time
*
* @since alpha 0.0.1
* @date 2014.02.04
* @author genialx
*
*/
public function log($type,$desc,$time){
if($this->log_switch){

if(self::$handle == NULL){
$filename = $this->log_file_pre . $this->get_max_log_file_suf();
self::$handle = fopen($this->log_file_path . $filename, 'a');
}
switch($type){
case 0:
fwrite(self::$handle, 'THING LOG:' . ' ' . $desc . ' ' . $time . chr(13));
break;
case 1:
fwrite(self::$handle, 'ERROR LOG:' . ' ' . $desc . ' ' . $time . chr(13));
break;
default:
fwrite(self::$handle, 'THING LOG:' . ' ' . $desc . ' ' . $time . chr(13));
break;
}

}
}

/**
* 擷取當前日誌的最新文檔的尾碼
*
* @since alpha 0.0.1
* @date 2014.02.04
* @author genialx
*/
private function get_max_log_file_suf(){
$log_file_suf = null;
if(is_dir($this->log_file_path)){
if($dh = opendir($this->log_file_path)){
while(($file = readdir($dh)) != FALSE){
if($file != '.' && $file != '..'){
if(filetype( $this->log_file_path . $file) == 'file'){
$rs = split('_', $file);
if($log_file_suf < $rs[1]){
$log_file_suf = $rs[1];
}
}
}
}

if($log_file_suf == NULL){
$log_file_suf = 0;
}
//截斷檔案
if( file_exists($this->log_file_path . $this->log_file_pre . $log_file_suf) && filesize($this->log_file_path . $this->log_file_pre . $log_file_suf) >= $this->log_max_len){
$log_file_suf = intval($log_file_suf) + 1;
}

return $log_file_suf;
}
}

return 0;

}

/**
* 關閉檔案控制代碼
*
* @since alpha 0.0.1
* @date 2014.02.04
* @author genialx
*/
public function close(){
fclose(self::$handle);
}
}

功能說明:
該日誌類利用單例模式,節省資源。自行判斷檔案大小,超出指定大小則按序自行建立檔案。如:檔案log_0大於指定大小,則重新建立log_1檔案(注意:建立檔案是安裝檔案名稱尾碼的數位,請勿隨意變更記錄檔檔案名稱)。

有待最佳化:沒有指定檔案的最大個數,所以定期要手動刪除過多的記錄檔。

調用樣本:

複製代碼 代碼如下:
//LOG
$L = Log::get_instance();
//第一個參數 int 0代表事件記錄(THING LOG:),1代表錯誤記錄(ERROR LOG:)
//第二個參數 string 標題文字
//第三個參數 string 時間
$L->log(1,'日誌描述', date('Y-n-j H:m:s'));
$L->close();

http://www.bkjia.com/PHPjc/732389.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/732389.htmlTechArticle對於現在的應用程式來說,日誌的重要性是不言而喻的。很難想象沒有任何日誌記錄功能的應用程式運行在生產環境中。日誌所能提供的功...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.