1. 程式人生 > 其它 >PHP讀大檔案日誌(27G)

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();