记录php运行日记的方法

 1 <?php
 2     /**
 3      * 名称:日志记录
 4      * 添加:wenzl
 5      * 日期:20140327
 6      * desc:
 7      * 当前位置生成日志文件
 8      * 每天会自动进行一次备份
 9      * 日志文件大小2M限制
10      * @param string $log
11      * @param type $def
12      * @return type
13      */
14     public function log($log="",$def="1"){
15   $log_file = dirname(__FILE__) . "/" . date('Y-m-d-',time()).$def.".log";
16    
17   /**
18    * 日志文件归档
19    */
20   $dh = dirname(__FILE__)."/";
21   $da = scandir($dh);
22   $da = array_filter($da, function($da){
23       return preg_match("/\d+\-\d+.*/",$da);
24   });
25  
26   array_walk($da, function($dv,$dk,$dh){
27       $l_file = $dh . $dv;
28       $n_file = $dh . date("Y-m-d") . ".logs/".$dv;
29       $dirbase = dirname($n_file);
30       is_dir($dirbase) || mkdir($dirbase, 0777);
31       $fct = filectime($l_file);
32       $begin = strtotime(date("Y-m-d"));
33       if($fct < $begin){
34    rename($l_file, $n_file);
35       }
36   },$dh);
37  
38  
39  
40  
41   /**
42    * 日志记录
43    */
44   if(filesize($log_file)>2097152){
45       $def++;
46       $this->log($log,$def);
47       exit;
48   }else{
49       $now = "[".date('Y-m-d H:i:s')."] ";
50       $log = $now .$log. "\r\n";
51       error_log($log, 3,$log_file, null);
52   }
53   return $def;
54     }
55 ?>

 

posted @ 2014-04-02 13:35  猿来如些  阅读(427)  评论(0编辑  收藏  举报