diff --git a/src/main/java/com/guwan/backend/controller/LogController.java b/src/main/java/com/guwan/backend/controller/LogController.java index 91b06c1..46534b7 100644 --- a/src/main/java/com/guwan/backend/controller/LogController.java +++ b/src/main/java/com/guwan/backend/controller/LogController.java @@ -5,16 +5,13 @@ import com.guwan.backend.elasticsearch.document.LogstashLog; import com.guwan.backend.service.LogstashLogService; import lombok.RequiredArgsConstructor; import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.Date; import java.util.List; @RestController -@RequestMapping("/api/logs") +@RequestMapping("/api/common/logs") @RequiredArgsConstructor public class LogController { @@ -40,4 +37,11 @@ public class LogController { @RequestParam(defaultValue = "10") int pageSize) { return logstashLogService.getRecentErrorLogs(pageNum, pageSize); } + + @GetMapping + public EsPageInfo getLogs( + @RequestParam(defaultValue = "1") int page, + @RequestParam(defaultValue = "20") int size) { + return logstashLogService.getLogsList(page, size); + } } \ No newline at end of file diff --git a/src/main/java/com/guwan/backend/elasticsearch/document/LogstashLog.java b/src/main/java/com/guwan/backend/elasticsearch/document/LogstashLog.java index 37cb7e0..1158ed9 100644 --- a/src/main/java/com/guwan/backend/elasticsearch/document/LogstashLog.java +++ b/src/main/java/com/guwan/backend/elasticsearch/document/LogstashLog.java @@ -30,17 +30,17 @@ public class LogstashLog { @IndexField(fieldType = FieldType.TEXT) private String message; - @IndexField(fieldType = FieldType.TEXT) + @IndexField(fieldType = FieldType.KEYWORD) private String level; @IndexField(fieldType = FieldType.LONG) private Long level_value; - @IndexField(fieldType = FieldType.TEXT) - private String logger_name; + @IndexField(fieldType = FieldType.KEYWORD) + private String loggerName; - @IndexField(fieldType = FieldType.TEXT) - private String thread_name; + @IndexField(fieldType = FieldType.KEYWORD) + private String threadName; @IndexField(fieldType = FieldType.TEXT) private String stack_trace; @@ -48,7 +48,7 @@ public class LogstashLog { @IndexField(fieldType = FieldType.TEXT) private String app_name; - @IndexField(fieldType = FieldType.TEXT) + @IndexField(fieldType = FieldType.KEYWORD) private String host; @IndexField(fieldType = FieldType.LONG) diff --git a/src/main/java/com/guwan/backend/service/LogstashLogService.java b/src/main/java/com/guwan/backend/service/LogstashLogService.java index e19a699..7735456 100644 --- a/src/main/java/com/guwan/backend/service/LogstashLogService.java +++ b/src/main/java/com/guwan/backend/service/LogstashLogService.java @@ -9,6 +9,12 @@ import org.springframework.stereotype.Service; import java.util.Date; import java.util.List; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.lang.StringBuilder; +import java.time.Instant; +import java.time.temporal.ChronoUnit; @Service @RequiredArgsConstructor @@ -51,4 +57,18 @@ public class LogstashLogService { return logstashLogEsMapper.pageQuery(wrapper, pageNum, pageSize); } + + public EsPageInfo getLogsList(int page, int size) { + LambdaEsQueryWrapper wrapper = new LambdaEsQueryWrapper<>(); + + // 设置时间范围为最近7天 + Instant now = Instant.now(); + Instant sevenDaysAgo = now.minus(7, ChronoUnit.DAYS); + + wrapper.index("logstash-logs-*") + .between(LogstashLog::getTimestamp, sevenDaysAgo, now) + .orderByDesc(LogstashLog::getTimestamp); + + return logstashLogEsMapper.pageQuery(wrapper, page, size); + } } \ No newline at end of file diff --git a/src/main/resources/static/log-viewer.html b/src/main/resources/static/log-viewer.html index 80b4b78..68d1138 100644 --- a/src/main/resources/static/log-viewer.html +++ b/src/main/resources/static/log-viewer.html @@ -160,26 +160,26 @@