记录日志,达到文件大小,自动新建文件

jerry thinkphp 2015年11月19日 收藏
记录日志,达到文件大小,自动新建文件
  1. function write($message,$level='ERROR',$type=3,$destination='',$extra='') {
  2.     $LOG_PATH = './';
  3.     $LOG_FILE_SIZE = 100;
  4.     $now = date('[ c ]');
  5.     $type = $type?$type:3;
  6.     if($type == 3) { // 文件方式记录日志
  7.         if(empty($destination))
  8.             $destination = $LOG_PATH.date('y-m-d').'.log';
  9.         //检测日志文件大小,超过配置大小则备份日志文件重新生成
  10.         if(is_file($destination) && floor($LOG_FILE_SIZE) <= filesize($destination) )
  11.               rename($destination,dirname($destination).'/'.basename($destination).'-'.time().'.log');
  12.     }
  13.     error_log("{$now} {$level}: {$message}\r\n", $type,$destination,$extra );
  14.     //clearstatcache();
  15. }
  16. write('dd');