package com.alibaba.algo.controller.cpwController;
import com.alibaba.algo.model.CpwLogErrorDO;
import com.alibaba.newcpw.FileOperate.FileCopy;
import com.alibaba.newcpw.FileOperate.FileRead;
import com.alibaba.newcpw.service.CpwLogErrorService;
import com.alibaba.newcpw.FileOperate.FileWrite;
import com.alibaba.newcpw.component.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Date;
import static com.alibaba.newcpw.FileOperate.FileWrite.pathAll;
@Controller
public class CpwLogErrorController {
public static String pathError = FileRead.pathError;
@Autowired
CpwLogErrorService cpwLogErrorServer;
@Autowired
CpwLogErrorController cpwLogErrorController;
// 注意这里是RequestMapping 后面的内容为接口路径。此接口为logError,每个参数错误日志断言
@RequestMapping("/logerror")
@ResponseBody
public CpwResultModel runProcessTask() {
return parseData(pathError, true);
}
// 读取path路径的txt文件
public CpwResultModel parseData(String path, Boolean isError) {
// 文件读取必须得用try catch,文件按行读取
// 按行处理数据,一行行插入数据库
int count = 0;
try {
File file = new File(path);
BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
String s;
while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
String result = s.trim();//去空白,末尾或者开头。去空格
if (result.equals("")) {
continue;
}
String[] array = result.split(FileWrite.rex);
count++;
if (isError) {
errorLogInsert(array);
}
}
br.close();
// 拷贝error_log.txt为error_log_copy.txt副本,删除控制台运行出原始日志error_log.txt,防止重复请求接口导致数据重复写入
deleteErrorFileAndCopy();
CpwResultModel cpwResultModel = new CpwResultModel();
cpwResultModel.setCode(Constants.CODE_SUCCESS);
cpwResultModel.setResult("success");
cpwResultModel.setCount(count);
if (count == 0) {
cpwResultModel.setError("暂无错误日志~~");
}
return cpwResultModel;
} catch (Exception e) {
e.printStackTrace();
CpwResultModel cpwResultModel = new CpwResultModel();
cpwResultModel.setCode(Constants.CODE_ERROR);
cpwResultModel.setResult("failed");
cpwResultModel.setCount(count);
cpwResultModel.setError(e.toString());
return cpwResultModel;
}
}
// 原始日志error_log.txt解析后字段入库
private void errorLogInsert(String[] array) {
String nowTime = array[0];
String logTime = array[1];
String logId = array[2];
String errorLog = array[3];
String errorLevel = array[4];
// 把字段解析到cpw_log_param数据表
CpwLogErrorDO cpwLogErrorDO = new CpwLogErrorDO();
// 自增ID不需要插入,无需以下代码,cpwLogErrorDO.setId((long)1);
cpwLogErrorDO.setGmtCreate(new Date());
cpwLogErrorDO.setGmtModified(new Date());
cpwLogErrorDO.setNowTime(nowTime);
cpwLogErrorDO.setLogTime(logTime);
cpwLogErrorDO.setLogId(Integer.parseInt(logId));
cpwLogErrorDO.setErrorLog(errorLog);
cpwLogErrorDO.setErrorLevel(Integer.parseInt(errorLevel));
cpwLogErrorServer.insertData(cpwLogErrorDO);
}
private void deleteErrorFileAndCopy(){
// 请求接口日志入库后,拷贝本地error_log_txt到error_log_copy.txt 每次日志运行做备份
File error_log = new File(pathAll + File.separator + "error_log.txt");
File error_log_copy = new File(pathAll + File.separator + "error_log_copy.txt");
try {
if (error_log_copy.exists()) {
error_log_copy.delete();
}
System.out.println("可能没有这个文件,兼容代码===错误日志副本已删除。delete error_log_copy.txt file ");
} catch (Exception e) {
e.printStackTrace();
}
// 拷贝副本,同步数据
FileCopy.copyFileUsingJava7Files(error_log, error_log_copy);
System.out.println("error_log_copy同步完成");
// 删除正本数据,保留副本数据
FileWrite.deleteErrorLogFirstRunFile();
}
}
待续。。。