源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
查看: 410|回复: 15

[PHP编程] PHP滚动日志的代码实现

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2018-12-25 17:38:47 | 显示全部楼层 |阅读模式
所谓滚动日志,顾名思义,就是记录一个模块的日志用一系列的日志文件,同一模块文件个数有限制,最多maxNum个,大小也有限制,最大maxSize字节,文件名有一定的命名方式,比如:testlog.log、testlog_1.log,testlog_2.log、、、、、、

PHP滚动日志类库

PHP记录日志,我之前接触过的有按照年月分文件夹,然后按照日分文件的日志记录方式,这种方式有利有弊,有他的使用场景,我今天要说的是另一种日志记录方式——文件滚动方式记录日志,当然了,这种滚动机制也可以加在前面那种日志记录方式中。

如何让日志滚动起来

滚动日志,顾名思义,记录一个模块的日志用一系列的日志文件,同一模块文件个数有限制,最多maxNum个,大小也有限制,最大maxSize字节,文件名有一定的命名方式,比如:testlog.log、testlog_1.log,testlog_2.log、、、、、、其中testlog.log是正在使用的日志文件,当testlog.log文件大小到达限制maxSize的时候就会向后滚动日志文件,就像下面这样:

复制代码 代码如下:
testlog_2.log  -> testlog_3.log
testlog_1.log  -> testlog_2.log
testlog.log  -> testlog_1.log
testlog.log #0kb

当日志文件个数到达限制maxNum的时候就会启动淘汰机制,删除最老的日志,比如说maxNum设置为10,这个时候算上testlog.log一共最多有10个文件,当滚动的时候如果存在testlog_9.log就会从testlog_8.log开始滚动,覆盖掉testlog_9.log,这样就可以保证日志正常记录,而且不会出现很大很大的日志文件,保证日志系统的正常运行。

代码实现

<?php
final class LOGS {
 private $level;
 private $maxFileNum;
 private $maxFileSize;
 private $logPath;
 private $file;

 //日志的级别DEBUG,MSG,ERR
 const LOGS_DEBUG = 0;
 const LOGS_MSG = 1;
 const LOGS_ERR = 2;

 private static $instance = null;

 private function __construct(){}

 public static function getInstance()
 {
 if(self::$instance == null)
 {
  self::$instance = new self();
 }
 return self::$instance;
 }

 /**
 * @Desc 初始化
 * @Param $level int 记录级别
 * @Param $maxNum int 最大日志文件数目
 * @Param $maxSize int 最大日志文件大小
 * @Param $logPath string 日志文件保存路径
 * @Param $file string 日志文件名称前缀
 * @Return boolean
 */
 public function init($level, $maxNum, $maxSize, $logPath, $file)
 {
 $level = intval($level);
 $maxNum = intval($maxNum);
 $maxSize = intval($maxSize);
 !is_dir($logPath) && mkdir($logPath, 0777, true);
 if(!in_array($level, array(self::LOGS_DEBUG, self::LOGS_MSG, self::LOGS_ERR)) || $maxNum <= 0 || $maxSize <= 0 || !is_dir($logPath))
 {
  return false;
 }
 $this->level = $level;
 $this->maxFileNum = $maxNum;
 $this->maxFileSize = $maxSize;
 $this->logPath = $logPath;
 $this->file = $file;
 return true;
 }

 /**
 * @Desc 获取格式化时间串
 */
 public function formatTime()
 {
    $ustime = explode ( " ", microtime () );
    return "[" . date('Y-m-d H:i:s', time()) .".". ($ustime[0] * 1000) . "]";
 }

 /** 
 * @Desc 滚动方式记录日志文件
 */
 public function log($str)
 {
 $path = $this->logPath.DIRECTORY_SEPARATOR.$this->file.".log";
 clearstatcache();
 if(file_exists($path))
 {
  if(filesize($path) >= $this->maxFileSize)
  {
  $index = 1;
  //获取最大的滚动日志数目
  for(;$index < $this->maxFileNum; $index++)
  {
   if(!file_exists($this->logPath.DIRECTORY_SEPARATOR.$this->file."_".$index.".log"))
   {
   break;
   }
  }
  //已经存在maxFileNum个日志文件了
  if($index == $this->maxFileNum)
  {
   $index--;
  }
  //滚动日志
  for(;$index > 1; $index--)
  {
   $new = $this->logPath.DIRECTORY_SEPARATOR.$this->file."_".$index.".log";
   $old = $this->logPath.DIRECTORY_SEPARATOR.$this->file."_".($index - 1).".log";
   rename($old, $new);
  }

  $newFile = $this->logPath.DIRECTORY_SEPARATOR.$this->file."_1.log";
  rename($path, $newFile);
  }
 }
 $fp = fopen($path, "a+b");
 fwrite($fp, $str, strlen($str));
 fclose($fp);
 return true;
 }

 /**
 * @Desc 记录调试信息
 * @Param string 日志信息
 * @Param string 日志所在文件
 * @Param string 日志所在行
 */
 public function debug($msg, $file, $line)
 {
 if($this->level <= self::LOGS_DEBUG)
 {
  $this->log($this->formatTime()."[{$file}:{$line}]DEBUG: ${msg}\n");
 }
 }

 /**
 * @Desc 记录信息
 * @Param string 日志信息
 * @Param string 日志所在文件
 * @Param string 日志所在行
 */
 public function msg($msg, $file, $line)
 {
 if($this->level <= self::LOGS_MSG)
 {
  $this->log($this->formatTime()."[{$file}:{$line}]MSG: ${msg}\n");
 }
 }

 /**
 * @Desc 记录错误信息
 * @Param string 日志信息
 * @Param string 日志所在文件
 * @Param string 日志所在行
 */
 public function err($msg, $file, $line)
 {
 if($this->level <= self::LOGS_ERR)
 {
  $this->log($this->formatTime()."[{$file}:{$line}]ERR: ${msg}\n");
 }
 }
}

看个例子

#例子中设置记录级别为msg(此时debug信息是不会纪录的),日志文件个数为5,大小为200个字节(测试方便),文件名称为testlog

$logs = LOGS::getInstance();
$logs->init(1, 5, 200, "./", 'testlog');

$logs->msg("YRT", __FILE__, __LINE__);
$logs->debug("YRT", __FILE__, __LINE__);

当我们不停的运行这个例子的时候,会在代码所在文件夹下生成5个文件就像下面这样:

testlog_4.log
testlog_3.log
testlog_2.log
testlog_1.log
testlog.log  #最新的日志在这个文件中

以上所述就是本文的全部内容了,希望大家能够喜欢。

回复

使用道具 举报

0

主题

1万

回帖

87

积分

注册会员

Rank: 2

积分
87
发表于 2022-9-13 23:58:42 | 显示全部楼层
来看看怎么样
回复 支持 反对

使用道具 举报

1

主题

1万

回帖

321

积分

中级会员

Rank: 3Rank: 3

积分
321
发表于 2022-12-4 14:39:41 | 显示全部楼层
笑纳了老板
回复 支持 反对

使用道具 举报

4

主题

1万

回帖

60

积分

注册会员

Rank: 2

积分
60
发表于 2023-1-6 21:51:46 | 显示全部楼层
了乐趣了去了去了去了去了
回复 支持 反对

使用道具 举报

11

主题

1万

回帖

300

积分

中级会员

Rank: 3Rank: 3

积分
300
发表于 2023-7-8 14:38:13 | 显示全部楼层
刷屏刷屏刷屏
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-9-5 22:21:11 | 显示全部楼层
数据库了多久撒快乐的健身卡啦
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

68

积分

注册会员

Rank: 2

积分
68
发表于 2023-10-17 13:16:48 | 显示全部楼层
源码源码源码源码源码源码源码源码源码源码源码源码源码
回复 支持 反对

使用道具 举报

9

主题

1万

回帖

420

积分

中级会员

Rank: 3Rank: 3

积分
420
发表于 2024-3-2 06:20:38 | 显示全部楼层
天天源码社区www.tiantianym.com
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

186

积分

注册会员

Rank: 2

积分
186
发表于 2024-3-13 05:54:41 | 显示全部楼层
大家都不容易!
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-4-3 05:01:49 | 显示全部楼层
抽根烟,下来看看再说
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-9-21 05:37 , Processed in 0.117892 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表