zoukankan      html  css  js  c++  java
  • PHP实现日志处理类库

    继上篇文章【微信开发之微电商网站】技术笔记之一,昨日做了日志处理的功能。

    对于现在的应用程序来说,日志的重要性是不言而喻的。很难想象没有任何日志记录功能的应用程序运行在生产环境中。日志所能提供的功能是多种多样的,包括记录程序运行时产生的错误信息、状态信息、调试信息和执行时间信息等。在生产环境中,日志是查找问题来源的重要依据。应用程序运行时的产生的各种信息,都应该通过日志类库来进行记录

    废话不多说了,附上日志类库的源代码: 

      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

    微信公众号(每日分享有价值的互联网资讯):胡旭个人博客

    新浪微博:@身边的互联网

    编程讨论群:235173087

    QQ:2252065614

     

    微信:genialx

     

  • 相关阅读:
    Sliding Window
    方程的解数
    [JLOI2011]不重复数字
    A−B数对
    2007年分区联赛提高组之一 统计数字
    Magic Squares 魔板 (BFS+HASH)
    集合(normal)
    Place the Robots
    LoadRunner监控Linux
    CentOS6.3(64位)下安装Oracle11gR2(64)服务器
  • 原文地址:https://www.cnblogs.com/wuniaoheart/p/3542119.html
Copyright © 2011-2022 走看看