PHP實現日誌處理類庫 - 【微信開發之微電商網站】技術筆記之二

繼上篇文章【微信開發之微電商網站】技術筆記之一,昨日作了日誌處理的功能。

對於如今的應用程序來講,日誌的重要性是不言而喻的。很難想象沒有任何日誌記錄功能的應用程序運行在生產環境中。日誌所能提供的功能是多種多樣的,包括記錄程序運行時產生的錯誤信息、狀態信息、調試信息和執行時間信息等。在生產環境中,日誌是查找問題來源的重要依據。應用程序運行時的產生的各類信息,都應該經過日誌類庫來進行記錄

廢話很少說了,附上日誌類庫的源代碼: html

  1 /**
  2  * 日誌處理類
  3  * 
  4  * @since alpha 0.0.1
  5  * @date 2014.03.04
  6  * @author genialx
  7  * 
  8  */
  9  
 10 class Log{
 11      
 12     //單例模式
 13     private static $instance    = NULL;
 14     //文件句柄
 15     private static $handle      = NULL;
 16     //日誌開關
 17     private $log_switch     = NULL;
 18     //日誌相對目錄
 19     private $log_file_path      = NULL;
 20     //日誌文件最大長度,超出長度從新創建文件
 21     private $log_max_len        = NULL;
 22     //日誌文件前綴,入 log_0
 23     private $log_file_pre       = 'log_';
 24  
 25          
 26     /**
 27      * 構造函數
 28      * 
 29      * @since alpha 0.0.1
 30      * @date 2014.02.04
 31      * @author genialx
 32      */
 33     protected function __construct(){//注意:如下是配置文件中的常量,請讀者自行更改
 34          
 35         $this->log_file_path     = LOG_FILE_PATH;
 36          
 37         $this->log_switch     = LOG_SWITCH;  
 38      
 39         $this->log_max_len    = LOG_MAX_LEN;
 40      
 41     }
 42      
 43     /**
 44      * 單利模式
 45      * 
 46      * @since alpha 0.0.1
 47      * @date 2014.02.04
 48      * @author genialx
 49      */
 50     public static function get_instance(){
 51         if(!self::$instance instanceof self){
 52             self::$instance = new self;
 53         }
 54         return self::$instance;
 55     }
 56      
 57     /**
 58      * 
 59      * 日誌記錄
 60      * 
 61      * @param int $type  0 -> 記錄(THING LOG) / 1 -> 錯誤(ERROR LOG)
 62      * @param string $desc
 63      * @param string $time
 64      * 
 65      * @since alpha 0.0.1
 66      * @date 2014.02.04
 67      * @author genialx
 68      * 
 69      */
 70     public function log($type,$desc,$time){
 71         if($this->log_switch){
 72              
 73             if(self::$handle == NULL){
 74                 $filename = $this->log_file_pre . $this->get_max_log_file_suf();
 75                 self::$handle = fopen($this->log_file_path . $filename, 'a');
 76             }
 77             switch($type){
 78                 case 0:
 79                     fwrite(self::$handle, 'THING LOG:' . ' ' . $desc . ' ' . $time . chr(13));
 80                     break;
 81                 case 1:
 82                     fwrite(self::$handle, 'ERROR LOG:' . ' ' . $desc . ' ' . $time . chr(13));
 83                     break;
 84                 default:
 85                     fwrite(self::$handle, 'THING LOG:' . ' ' . $desc . ' ' . $time . chr(13));
 86                     break;
 87             }
 88              
 89         }
 90     }
 91      
 92     /**
 93      * 獲取當前日誌的最新文檔的後綴
 94      * 
 95      * @since alpha 0.0.1
 96      * @date 2014.02.04
 97      * @author genialx
 98      */
 99     private function get_max_log_file_suf(){
100         $log_file_suf = null;
101         if(is_dir($this->log_file_path)){
102             if($dh = opendir($this->log_file_path)){
103                 while(($file = readdir($dh)) != FALSE){
104                     if($file != '.' && $file != '..'){
105                         if(filetype( $this->log_file_path . $file) == 'file'){
106                             $rs = split('_', $file);
107                             if($log_file_suf < $rs[1]){
108                                 $log_file_suf = $rs[1];
109                             }
110                         }
111                     }
112                 }
113                  
114                 if($log_file_suf == NULL){
115                     $log_file_suf = 0;
116                 }
117                 //截斷文件
118                 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){
119                     $log_file_suf = intval($log_file_suf) + 1;
120                 }
121                  
122                 return $log_file_suf;
123             }   
124         }
125          
126         return 0;
127          
128     }
129      
130     /**
131      * 關閉文件句柄
132      * 
133      * @since alpha 0.0.1
134      * @date 2014.02.04
135      * @author genialx
136      */
137     public function close(){
138         fclose(self::$handle);
139     }
140 }

 

功能說明:
該日誌類利用單例模式,節省資源。自行判斷文件大小,超出指定大小則按序自行建立文件。如:文件log_0大於指定大小,則從新建立log_1文件(注意:建立文件是安裝文件名後綴的數字的,請勿隨意更改日誌文件名)。

有待優化:沒有指定文件的最大個數,因此按期要手動刪除過多的日誌文件。

編程


調用示例:微信

1 //LOG
2 $L = Log::get_instance();
3 //第一個參數 int 0表明事件記錄(THING LOG:),1表明錯誤記錄(ERROR LOG:)
4 //第二個參數 string 描述文字
5 //第三個參數 string 時間
6 $L->log(1,'日誌描述', date('Y-n-j H:m:s'));
7 $L->close();

 

感謝您的查閱!微信開發

文章來源:http://www.ihuxu.com/p/223.html函數

微信公衆號(每日分享有價值的互聯網資訊):胡旭我的博客優化

新浪微博:@身邊的互聯網網站

編程討論羣:235173087this

QQ:2252065614spa

 

微信:genialx調試

相關文章
相關標籤/搜索