PHP读大文件日志(27G)
class LogRewrite
{
protected $logDir = "./";
protected $idxFile = "./idx";
protected $writeIp = "0.0.0.0";
// 要修复的日期
protected $d;
// 日期匹配
protected $Ymd;
public function __construct($d, $dir = null)
{
$this->d = $d;
$this->Ymd = date("Ymd", strtotime($d));
if ($dir) {
$this->logDir = $dir;
}
// 忽略浏览器断开
ignore_user_abort(true);
// 不超时
set_time_limit(0);
}
public function rewriteLog()
{
// 如果有出现异常中段下次可以从此指针的数据开始
// $lastIdx = file_get_contents($idxFile);
$log = new SplFileObject($this->logDir . $this->d . "_error.log");
foreach ($log as $line) {
// 记住指针位置,如果有出现异常中段下次可以从此指针的数据开始
$idx = $log->ftell();
// 测试
/*if ($idx > 500000) {
break;
}*/
/*
// 如果有出现异常中段下次可以从此指针的数据开始
// 从 idx.log 文件中查到的位置
if ($idx < $lastIdx) {
continue;
}
*/
file_put_contents($this->idxFile, $idx);
// 从 AdClient 开始
$str = stristr($line, 'AdClient');
$str1 = str_replace("\"", "", $str);
//
if (!$str1 || empty($str1)) {
continue;
}
if (true) {
$this->write_file($str1);
} else {
$this->error_file($d);
}
}
}
/**
* 匹配成功的日志
* @Author: xiaoliang.cheng
* @Date: 2021/7/29
*
* @param $fieldVal
*
* @Return
*/
public function write_file($v)
{
$logFile = $this->logDir . "data_success.log";
$handle = fopen($logFile, "a+");
$logText = $this->writeIp . "|" . $v . "\r\n";
fwrite($handle, $logText);
fclose($handle);
}
/**
* 不匹配的日志
* @Author: xiaoliang.cheng
* @Date: 2021/7/29
*
* @param $d
*
* @Return
*/
public function error_file($d)
{
$logFile = $this->logDir . "data_error.log";
$handle = fopen($logFile, "a+");
fwrite($handle, $d . "\r\n");
fclose($handle);
}
}
$l = new LogRewrite("2021-07-27", "./logs/");
$l->rewriteLog();
本文来自博客园,作者:Silent-Cxl,转载请注明原文链接:https://www.cnblogs.com/silent-cxl/p/15074747.html

浙公网安备 33010602011771号