支持历史日志的查看,过滤以及下载
This commit is contained in:
@@ -6,15 +6,8 @@ import lombok.Data;
|
||||
public class LogFileInfo {
|
||||
|
||||
private String fileName;
|
||||
private String startTime;
|
||||
private String endTime;
|
||||
|
||||
public static LogFileInfo getInstance(String fileName, String startTime, String endTime) {
|
||||
LogFileInfo logFileInfo = new LogFileInfo();
|
||||
logFileInfo.setFileName(fileName);
|
||||
logFileInfo.setStartTime(startTime);
|
||||
logFileInfo.setEndTime(endTime);
|
||||
return logFileInfo;
|
||||
}
|
||||
private Long fileSize;
|
||||
private Long startTime;
|
||||
private Long endTime;
|
||||
|
||||
}
|
||||
|
||||
@@ -9,17 +9,12 @@ import com.genersoft.iot.vmp.utils.DateUtil;
|
||||
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.input.ReversedLinesFileReader;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@@ -37,33 +32,36 @@ public class LogServiceImpl implements ILogService {
|
||||
if (files == null || files.length == 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 读取文件创建时间作为开始时间,修改时间为结束时间
|
||||
Long startTimestamp = null;
|
||||
if (startTime != null) {
|
||||
startTimestamp = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestampMs(startTime);
|
||||
}
|
||||
Long endTimestamp = null;
|
||||
if (endTime != null) {
|
||||
endTimestamp = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestampMs(endTime);
|
||||
}
|
||||
for (File file : files) {
|
||||
LogFileInfo logFileInfo = new LogFileInfo();
|
||||
logFileInfo.setFileName(file.getName());
|
||||
logFileInfo.setFileSize(file.length());
|
||||
if (query != null && !file.getName().contains(query)) {
|
||||
continue;
|
||||
}
|
||||
// 读取文件创建时间作为开始时间,修改时间为结束时间
|
||||
|
||||
Long startTimestamp = null;
|
||||
if (startTime != null) {
|
||||
startTimestamp = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime);
|
||||
}
|
||||
Long endTimestamp = null;
|
||||
if (startTime != null) {
|
||||
endTimestamp = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime);
|
||||
}
|
||||
try {
|
||||
String[] fileAttributes = getFileAttributes(file);
|
||||
Long[] fileAttributes = getFileAttributes(file);
|
||||
if (fileAttributes == null) {
|
||||
continue;
|
||||
}
|
||||
logFileInfo.setStartTime(fileAttributes[0]);
|
||||
logFileInfo.setEndTime(fileAttributes[1]);
|
||||
if (startTimestamp != null && startTimestamp > DateUtil.yyyy_MM_dd_HH_mm_ssToTimestampMs(fileAttributes[0])) {
|
||||
long startTimestampForFile = fileAttributes[0];
|
||||
long endTimestampForFile = fileAttributes[1];
|
||||
logFileInfo.setStartTime(startTimestampForFile);
|
||||
logFileInfo.setEndTime(endTimestampForFile);
|
||||
if (startTimestamp != null && startTimestamp > startTimestampForFile) {
|
||||
continue;
|
||||
}
|
||||
if (endTimestamp != null && endTimestamp < DateUtil.yyyy_MM_dd_HH_mm_ssToTimestampMs(fileAttributes[1])) {
|
||||
if (endTimestamp != null && endTimestamp < endTimestampForFile) {
|
||||
continue;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
@@ -73,6 +71,7 @@ public class LogServiceImpl implements ILogService {
|
||||
result.add(logFileInfo);
|
||||
|
||||
}
|
||||
result.sort((o1, o2) -> o2.getStartTime().compareTo(o1.getStartTime()));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -83,7 +82,7 @@ public class LogServiceImpl implements ILogService {
|
||||
return rollingFile.getParentFile();
|
||||
}
|
||||
|
||||
String[] getFileAttributes(File file) throws IOException {
|
||||
Long[] getFileAttributes(File file) throws IOException {
|
||||
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
|
||||
String startLine = bufferedReader.readLine();
|
||||
if (startLine== null) {
|
||||
@@ -91,15 +90,15 @@ public class LogServiceImpl implements ILogService {
|
||||
}
|
||||
String startTime = startLine.substring(0, 19);
|
||||
|
||||
|
||||
String lastLine = "";
|
||||
try (ReversedLinesFileReader reversedLinesReader = new ReversedLinesFileReader(file, Charset.defaultCharset())) {
|
||||
lastLine = reversedLinesReader.readLine();
|
||||
} catch (Exception e) {
|
||||
log.error("file read error, msg:{}", e.getMessage(), e);
|
||||
}
|
||||
String endTime = lastLine.substring(0, 19);
|
||||
return new String[]{startTime, endTime};
|
||||
// 最后一行的开头不一定是时间
|
||||
// String lastLine = "";
|
||||
// try (ReversedLinesFileReader reversedLinesReader = new ReversedLinesFileReader(file, Charset.defaultCharset())) {
|
||||
// lastLine = reversedLinesReader.readLine();
|
||||
// } catch (Exception e) {
|
||||
// log.error("file read error, msg:{}", e.getMessage(), e);
|
||||
// }
|
||||
// String endTime = lastLine.substring(0, 19);
|
||||
return new Long[]{DateUtil.yyyy_MM_dd_HH_mm_ssToTimestampMs(startTime), file.lastModified()};
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -62,6 +62,15 @@ public class LogController {
|
||||
public List<LogFileInfo> queryList(@RequestParam(required = false) String query, @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime
|
||||
|
||||
) {
|
||||
if (ObjectUtils.isEmpty(query)) {
|
||||
query = null;
|
||||
}
|
||||
if (ObjectUtils.isEmpty(startTime)) {
|
||||
startTime = null;
|
||||
}
|
||||
if (ObjectUtils.isEmpty(endTime)) {
|
||||
endTime = null;
|
||||
}
|
||||
return logService.queryList(query, startTime, endTime);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user