add:es
This commit is contained in:
@@ -32,7 +32,12 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- elasticsearch-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
||||||
|
<version>2.7.5</version>
|
||||||
|
</dependency>
|
||||||
<!-- 获取系统信息 -->
|
<!-- 获取系统信息 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.oshi</groupId>
|
<groupId>com.github.oshi</groupId>
|
||||||
@@ -40,21 +45,22 @@
|
|||||||
<version>6.1.4</version>
|
<version>6.1.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.elasticsearch.client</groupId>
|
<!-- <dependency>-->
|
||||||
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
<!-- <groupId>org.elasticsearch.client</groupId>-->
|
||||||
<version>7.6.1</version>
|
<!-- <artifactId>elasticsearch-rest-high-level-client</artifactId>-->
|
||||||
</dependency>
|
<!-- <version>7.6.1</version>-->
|
||||||
<dependency>
|
<!-- </dependency>-->
|
||||||
<groupId>org.elasticsearch.client</groupId>
|
<!-- <dependency>-->
|
||||||
<artifactId>elasticsearch-rest-client</artifactId>
|
<!-- <groupId>org.elasticsearch.client</groupId>-->
|
||||||
<version>7.6.1</version>
|
<!-- <artifactId>elasticsearch-rest-client</artifactId>-->
|
||||||
</dependency>
|
<!-- <version>7.6.1</version>-->
|
||||||
<dependency>
|
<!-- </dependency>-->
|
||||||
<groupId>org.elasticsearch</groupId>
|
<!-- <dependency>-->
|
||||||
<artifactId>elasticsearch</artifactId>
|
<!-- <groupId>org.elasticsearch</groupId>-->
|
||||||
<version>7.6.1</version>
|
<!-- <artifactId>elasticsearch</artifactId>-->
|
||||||
</dependency>
|
<!-- <version>7.6.1</version>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
<!-- logback appender日志-->
|
<!-- logback appender日志-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.internetitem</groupId>
|
<groupId>com.internetitem</groupId>
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactor
|
|||||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||||
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
|
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||||
import org.springframework.scheduling.annotation.EnableAsync;
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
@@ -36,6 +37,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@EnableMethodCache(basePackages = "org.nl")
|
@EnableMethodCache(basePackages = "org.nl")
|
||||||
@EnableCreateCacheAnnotation
|
@EnableCreateCacheAnnotation
|
||||||
@MapperScan("org.nl.**.mapper")
|
@MapperScan("org.nl.**.mapper")
|
||||||
|
//@EnableElasticsearchRepositories(basePackages = {"org.nl.modules.logging.repository.*"})
|
||||||
public class AppRun {
|
public class AppRun {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ public class ElasticSearchTest {
|
|||||||
|
|
||||||
public static void searchById() throws IOException {
|
public static void searchById() throws IOException {
|
||||||
RestHighLevelClient client = getClientConnection();
|
RestHighLevelClient client = getClientConnection();
|
||||||
GetRequest getRequest = new GetRequest("gateway_log", "DceJqGwBqlIig5BB05Z-");
|
GetRequest getRequest = null;//new GetRequest("gateway_log", "DceJqGwBqlIig5BB05Z-");
|
||||||
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
|
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
|
||||||
System.out.println(getResponse.getSourceAsString());
|
System.out.println(getResponse.getSourceAsString());
|
||||||
client.close();
|
client.close();
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.nl.common.enums;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
public enum LevelEnum{
|
||||||
|
DEBUG,
|
||||||
|
INFO,
|
||||||
|
WARN,
|
||||||
|
ERROR;
|
||||||
|
public static LevelEnum checkLevel(String level){
|
||||||
|
if (!StringUtils.isEmpty(level)){
|
||||||
|
for (LevelEnum value : LevelEnum.values()) {
|
||||||
|
if (value.name().equals(level)){
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return LevelEnum.DEBUG;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,6 +35,8 @@ import org.nl.modules.common.utils.ThrowableUtil;
|
|||||||
import org.nl.modules.logging.domain.Log;
|
import org.nl.modules.logging.domain.Log;
|
||||||
import org.nl.modules.logging.service.LogService;
|
import org.nl.modules.logging.service.LogService;
|
||||||
import org.nl.modules.wql.core.bean.WQLObject;
|
import org.nl.modules.wql.core.bean.WQLObject;
|
||||||
|
import org.slf4j.MDC;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
@@ -53,52 +55,39 @@ import java.util.*;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class LogAspect {
|
public class LogAspect {
|
||||||
|
|
||||||
private final LogService logService;
|
@Autowired
|
||||||
|
private LogService logService;
|
||||||
|
|
||||||
ThreadLocal<Long> currentTime = new ThreadLocal<>();
|
|
||||||
|
|
||||||
public LogAspect(LogService logService) {
|
|
||||||
this.logService = logService;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 配置切入点
|
|
||||||
*/
|
|
||||||
@Pointcut("@annotation(org.nl.modules.logging.annotation.Log)")
|
|
||||||
public void logPointcut() {
|
|
||||||
// 该方法无方法体,主要为了让同类中其他方法使用此切入点
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置环绕通知,使用在方法logPointcut()上注册的切入点
|
* 配置环绕通知,使用在方法logPointcut()上注册的切入点
|
||||||
*
|
*
|
||||||
* @param joinPoint join point for advice
|
* @param joinPoint join point for advice
|
||||||
*/
|
*/
|
||||||
@Around("logPointcut()")
|
@Around("@annotation(logInfo)")
|
||||||
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
public Object logAround(ProceedingJoinPoint joinPoint,org.nl.modules.logging.annotation.Log logInfo) throws Throwable {
|
||||||
String trackId = UUID.randomUUID().toString();
|
String trackId = UUID.randomUUID().toString();
|
||||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||||
Method method = signature.getMethod();
|
Method method = signature.getMethod();
|
||||||
// 方法路径
|
|
||||||
String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
|
String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
|
||||||
String params = getParameter(method, joinPoint.getArgs());
|
String params = getParameter(method, joinPoint.getArgs());
|
||||||
|
|
||||||
org.nl.modules.logging.annotation.Log logInfo = method.getAnnotation(org.nl.modules.logging.annotation.Log.class);
|
|
||||||
|
|
||||||
//是否输出到日志文件
|
|
||||||
if (logInfo.isPrintToLogFile()) {
|
|
||||||
log.info("track_id:{},请求方法:{},请求方法参数:{}",trackId,methodName,params);
|
|
||||||
}
|
|
||||||
HttpServletRequest request = RequestHolder.getHttpServletRequest();
|
HttpServletRequest request = RequestHolder.getHttpServletRequest();
|
||||||
String requestIp = StringUtils.getIp(request);
|
String requestIp = StringUtils.getIp(request);
|
||||||
Object result;
|
|
||||||
currentTime.set(System.currentTimeMillis());
|
MDC.put("requestMethod",methodName);
|
||||||
|
MDC.put("requestIp", StringUtils.getIp(request));
|
||||||
|
MDC.put("trackId", UUID.randomUUID().toString());
|
||||||
|
MDC.put("requestTime", DateUtil.now());
|
||||||
|
|
||||||
|
Object result = null;
|
||||||
|
long comming = System.currentTimeMillis();
|
||||||
try {
|
try {
|
||||||
|
log.info("[--request--][请求方法:{}][请求参数:{}]",methodName,params);
|
||||||
result = joinPoint.proceed();
|
result = joinPoint.proceed();
|
||||||
//是否把日志存到日志表
|
//是否把日志存到日志表
|
||||||
if (logInfo.isAddLogTable()) {
|
if (logInfo.isAddLogTable()) {
|
||||||
Log log = new Log("INFO", System.currentTimeMillis() - currentTime.get());
|
Log log = new Log("INFO", System.currentTimeMillis() - comming);
|
||||||
currentTime.remove();
|
|
||||||
logService.save(getUsername(), StringUtils.getBrowser(request), requestIp, joinPoint, log);
|
logService.save(getUsername(), StringUtils.getBrowser(request), requestIp, joinPoint, log);
|
||||||
}
|
}
|
||||||
if (logInfo.isInterfaceLog()) {
|
if (logInfo.isInterfaceLog()) {
|
||||||
@@ -112,25 +101,29 @@ public class LogAspect {
|
|||||||
json.put("method", methodName);
|
json.put("method", methodName);
|
||||||
json.put("params", getParameter(method, joinPoint.getArgs()));
|
json.put("params", getParameter(method, joinPoint.getArgs()));
|
||||||
json.put("request_ip", StringUtils.getIp(request));
|
json.put("request_ip", StringUtils.getIp(request));
|
||||||
json.put("time", System.currentTimeMillis() - currentTime.get());
|
json.put("time", System.currentTimeMillis() - comming);
|
||||||
json.put("username", getUsername());
|
json.put("username", getUsername());
|
||||||
json.put("address", StringUtils.getCityInfo(requestIp));
|
json.put("address", StringUtils.getCityInfo(requestIp));
|
||||||
json.put("browser", StringUtils.getBrowser(request));
|
json.put("browser", StringUtils.getBrowser(request));
|
||||||
json.put("exception_detail", IdUtil.getStringId());
|
json.put("exception_detail", IdUtil.getStringId());
|
||||||
json.put("create_time", DateUtil.now());
|
json.put("create_time", DateUtil.now());
|
||||||
json.put("return_result", JSONObject.parse(result.toString()));
|
Object parse = JSONObject.parse(result.toString());
|
||||||
|
json.put("return_result", parse);
|
||||||
|
log.info("请求结果:{}",parse);
|
||||||
interfaceLog.insert(json);
|
interfaceLog.insert(json);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (Exception ex){
|
}catch (Exception ex){
|
||||||
log.error("track_id:{},error:{}",trackId,ex.getMessage());
|
log.info("[--requestError--][请求方法:{}]【异常信息 :{}】", methodName, ex);
|
||||||
Log log = new Log("ERROR", System.currentTimeMillis() - currentTime.get());
|
Log log = new Log("ERROR", System.currentTimeMillis() - comming);
|
||||||
currentTime.remove();
|
|
||||||
log.setExceptionDetail(ThrowableUtil.getStackTrace(ex).getBytes());
|
log.setExceptionDetail(ThrowableUtil.getStackTrace(ex).getBytes());
|
||||||
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint) joinPoint, log);
|
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint) joinPoint, log);
|
||||||
throw ex;
|
throw ex;
|
||||||
|
}finally {
|
||||||
|
log.info("[--response--][请求方法:{}][请求参数:{}]【返回结果 :{}】",methodName,params,result);
|
||||||
|
MDC.clear();
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -165,21 +158,6 @@ public class LogAspect {
|
|||||||
return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList);
|
return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 配置异常通知
|
|
||||||
*
|
|
||||||
* @param joinPoint join point for advice
|
|
||||||
* @param e exception
|
|
||||||
*/
|
|
||||||
// @AfterThrowing(pointcut = "logPointcut()", throwing = "e")
|
|
||||||
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
|
|
||||||
Log log = new Log("ERROR", System.currentTimeMillis() - currentTime.get());
|
|
||||||
currentTime.remove();
|
|
||||||
log.setExceptionDetail(ThrowableUtil.getStackTrace(e).getBytes());
|
|
||||||
HttpServletRequest request = RequestHolder.getHttpServletRequest();
|
|
||||||
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint) joinPoint, log);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
try {
|
try {
|
||||||
return SecurityUtils.getCurrentUsername();
|
return SecurityUtils.getCurrentUsername();
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package org.nl.modules.logging.repository;
|
||||||
|
|
||||||
|
import org.nl.modules.logging.service.dto.LogRepositoryDTO;
|
||||||
|
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @author ZZQ
|
||||||
|
* @Date 2023/2/8 4:11 下午
|
||||||
|
*/
|
||||||
|
@Repository
|
||||||
|
public interface EsLogRepository extends ElasticsearchRepository<LogRepositoryDTO, String> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,12 +2,12 @@ package org.nl.modules.logging.rest;
|
|||||||
|
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.nl.common.domain.query.PageQuery;
|
||||||
import org.nl.modules.logging.service.EsLogService;
|
import org.nl.modules.logging.service.EsLogService;
|
||||||
|
import org.nl.modules.logging.service.dto.LogQuery;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ldjun
|
* @author ldjun
|
||||||
@@ -22,10 +22,17 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
public class EsLogController {
|
public class EsLogController {
|
||||||
private final EsLogService esLogService;
|
private final EsLogService esLogService;
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/labels")
|
@GetMapping("/labels")
|
||||||
@ApiOperation("获取标签")
|
@ApiOperation("获取标签")
|
||||||
public ResponseEntity<Object> labelsValues() {
|
public ResponseEntity<Object> labelsValues() {
|
||||||
return new ResponseEntity<>(esLogService.getLabelsValues(), HttpStatus.OK);
|
return new ResponseEntity<>(esLogService.getLabelsValues(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/query")
|
||||||
|
@ApiOperation("日志查询")
|
||||||
|
public ResponseEntity<Object> queryAll(@RequestBody LogQuery query) {
|
||||||
|
return new ResponseEntity<>(esLogService.query(query), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package org.nl.modules.logging.service;
|
package org.nl.modules.logging.service;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import org.nl.common.domain.query.PageQuery;
|
||||||
|
import org.nl.modules.logging.service.dto.LogQuery;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ldjun
|
* @author ldjun
|
||||||
@@ -14,4 +17,11 @@ public interface EsLogService {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
JSONArray getLabelsValues();
|
JSONArray getLabelsValues();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志查询
|
||||||
|
* @param logQuery
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Page query(LogQuery logQuery);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package org.nl.modules.logging.service.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @author ZZQ
|
||||||
|
* @Date 2023/2/8 5:18 下午
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class LogQuery {
|
||||||
|
/**
|
||||||
|
* 创建时间范围查询
|
||||||
|
*/
|
||||||
|
private Date startTime;
|
||||||
|
private Date endTime;
|
||||||
|
/**
|
||||||
|
* 追踪id
|
||||||
|
*/
|
||||||
|
private String trackId;
|
||||||
|
/**
|
||||||
|
* 日志内容模糊匹配
|
||||||
|
*/
|
||||||
|
private String message;
|
||||||
|
/**
|
||||||
|
* 日志级别
|
||||||
|
*/
|
||||||
|
private String logLevel;
|
||||||
|
/**
|
||||||
|
* 是否只查询Http相关请求
|
||||||
|
*/
|
||||||
|
private Boolean isRequest = Boolean.TRUE;
|
||||||
|
/**
|
||||||
|
* 是否过滤wql日志
|
||||||
|
*/
|
||||||
|
private Boolean filterSql = Boolean.TRUE;
|
||||||
|
|
||||||
|
private Integer size = 20;
|
||||||
|
|
||||||
|
private Integer page = 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.nl.modules.logging.service.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
import org.springframework.data.elasticsearch.annotations.Document;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @author ZZQ
|
||||||
|
* @Date 2023/2/8 4:06 下午
|
||||||
|
*/
|
||||||
|
@Document(indexName = "lms_log", type = "lms_log")
|
||||||
|
@Data
|
||||||
|
public class LogRepositoryDTO {
|
||||||
|
|
||||||
|
private String message;
|
||||||
|
private String host;
|
||||||
|
private String logLevel;
|
||||||
|
private String logger;
|
||||||
|
private String requestTime;
|
||||||
|
private String requestIp;
|
||||||
|
@Id
|
||||||
|
private String id;
|
||||||
|
private String trackId;
|
||||||
|
private String requestMethod;
|
||||||
|
private String thread;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,9 +4,23 @@ import cn.hutool.http.HttpRequest;
|
|||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.poi.ss.formula.functions.T;
|
||||||
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||||
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
|
import org.nl.common.domain.query.PageQuery;
|
||||||
|
import org.nl.common.enums.LevelEnum;
|
||||||
|
import org.nl.modules.logging.repository.EsLogRepository;
|
||||||
import org.nl.modules.logging.service.EsLogService;
|
import org.nl.modules.logging.service.EsLogService;
|
||||||
|
import org.nl.modules.logging.service.dto.LogQuery;
|
||||||
|
import org.nl.modules.logging.service.dto.LogRepositoryDTO;
|
||||||
import org.nl.wms.ext.acs.service.impl.AcsToWmsServiceImpl;
|
import org.nl.wms.ext.acs.service.impl.AcsToWmsServiceImpl;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -20,57 +34,62 @@ import java.util.Map;
|
|||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class EsLogServiceImpl implements EsLogService {
|
public class EsLogServiceImpl implements EsLogService {
|
||||||
|
|
||||||
|
private final EsLogRepository esLogRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page query(LogQuery logQuery){
|
||||||
|
Page<T> page = new Page<>();
|
||||||
|
if (logQuery != null){
|
||||||
|
BoolQueryBuilder query = QueryBuilders.boolQuery(); //requestMethod
|
||||||
|
extractedParam(logQuery, query);
|
||||||
|
Iterable<LogRepositoryDTO> all = esLogRepository.search(query, PageRequest.of(logQuery.getPage(),logQuery.getSize(), Sort.by("@timestamp").descending()));
|
||||||
|
page.setRecords(((AggregatedPageImpl) all).getContent());
|
||||||
|
page.setTotal(((AggregatedPageImpl) all).getTotalElements());
|
||||||
|
page.setPages(logQuery.getPage());
|
||||||
|
page.setSize(logQuery.getSize());
|
||||||
|
}
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void extractedParam(LogQuery logQuery, BoolQueryBuilder query) {
|
||||||
|
if (StringUtils.isNotEmpty(logQuery.getLogLevel())){
|
||||||
|
query.must().add(QueryBuilders.matchQuery("logLevel", LevelEnum.checkLevel(logQuery.getLogLevel())));
|
||||||
|
}
|
||||||
|
if (logQuery.getIsRequest()){
|
||||||
|
query.must().add(QueryBuilders.existsQuery("requestMethod"));
|
||||||
|
}
|
||||||
|
if (logQuery.getFilterSql()){
|
||||||
|
query.mustNot().add(QueryBuilders.wildcardQuery("logger","org.nl.modules.wql.core.engine.*"));
|
||||||
|
}
|
||||||
|
query.mustNot().add(QueryBuilders.wildcardQuery("logger","org.nl.modules.wql.core.engine.*"));
|
||||||
|
if (StringUtils.isNotEmpty(logQuery.getTrackId())){
|
||||||
|
query.must().add(QueryBuilders.matchQuery("trackId", logQuery.getTrackId()));
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotEmpty(logQuery.getMessage())){
|
||||||
|
query.must().add(QueryBuilders.matchQuery("message", logQuery.getMessage()).minimumShouldMatch("50%"));
|
||||||
|
}
|
||||||
|
if (logQuery.getStartTime()!=null){
|
||||||
|
query.must().add(QueryBuilders.rangeQuery("requestTime").gte(logQuery.getStartTime()));
|
||||||
|
}
|
||||||
|
if (logQuery.getEndTime()!=null){
|
||||||
|
query.must().add(QueryBuilders.rangeQuery("requestTime").lte(logQuery.getStartTime()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONArray getLabelsValues() {
|
public JSONArray getLabelsValues() {
|
||||||
/**
|
JSONArray result = new JSONArray();
|
||||||
* [{
|
for (LevelEnum value : LevelEnum.values()) {
|
||||||
* label:
|
JSONObject level = new JSONObject();
|
||||||
* value:
|
level.put("label", value.name());
|
||||||
* children:[{
|
level.put("Value", value.name());
|
||||||
* label
|
result.add(level);
|
||||||
* value
|
|
||||||
* }]
|
|
||||||
* }]
|
|
||||||
*/
|
|
||||||
//获取所有索引
|
|
||||||
// String url = "http://47.111.78.178:27017/_cat/indices";
|
|
||||||
|
|
||||||
String url = "http://47.111.78.178:27017/_aliases";
|
|
||||||
String resultMsg = HttpRequest.get(url)
|
|
||||||
.execute().body();
|
|
||||||
|
|
||||||
JSONObject jsonObject = JSON.parseObject(resultMsg);
|
|
||||||
|
|
||||||
JSONArray arr = new JSONArray();
|
|
||||||
|
|
||||||
for (Map.Entry entry : jsonObject.entrySet()) {
|
|
||||||
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
|
|
||||||
JSONObject json = new JSONObject();
|
|
||||||
json.put("label", entry.getKey());
|
|
||||||
json.put("Value", entry.getValue());
|
|
||||||
arr.add(json);
|
|
||||||
}
|
}
|
||||||
return arr;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// String url = "http://47.111.78.178:27017/_cat/indices";
|
|
||||||
String url = "http://47.111.78.178:27017/_aliases";
|
|
||||||
String resultMsg = HttpRequest.get(url)
|
|
||||||
.execute().body();
|
|
||||||
|
|
||||||
JSONObject jsonObject = JSON.parseObject(resultMsg);
|
|
||||||
|
|
||||||
JSONArray arr = new JSONArray();
|
|
||||||
|
|
||||||
for (Map.Entry entry : jsonObject.entrySet()) {
|
|
||||||
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
|
|
||||||
JSONObject json = new JSONObject();
|
|
||||||
json.put("label", entry.getKey());
|
|
||||||
json.put("Value", entry.getValue());
|
|
||||||
arr.add(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,16 +2,30 @@ server:
|
|||||||
port: 8010
|
port: 8010
|
||||||
#配置数据源
|
#配置数据源
|
||||||
spring:
|
spring:
|
||||||
|
data:
|
||||||
|
elasticsearch:
|
||||||
|
repositories:
|
||||||
|
enabled: true
|
||||||
|
client:
|
||||||
|
reactive:
|
||||||
|
#endpoints: 172.31.185.110:9200,172.31.154.9:9200 #内网
|
||||||
|
endpoints: 47.96.133.178:8200 #外网
|
||||||
|
elasticsearch:
|
||||||
|
rest:
|
||||||
|
#uris: 172.31.185.110:9200,172.31.154.9:9200 #内网
|
||||||
|
uris: 47.96.133.178:8200 #外网
|
||||||
|
# username: elastic
|
||||||
|
# password: 123456
|
||||||
datasource:
|
datasource:
|
||||||
druid:
|
druid:
|
||||||
db-type: com.alibaba.druid.pool.DruidDataSource
|
db-type: com.alibaba.druid.pool.DruidDataSource
|
||||||
driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
|
driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
|
||||||
# url: jdbc:log4jdbc:mysql://${DB_HOST:10.1.3.91}:${DB_PORT:3306}/${DB_NAME:lms_test2}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true
|
# url: jdbc:log4jdbc:mysql://${DB_HOST:10.1.3.91}:${DB_PORT:3306}/${DB_NAME:lms_test2}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true
|
||||||
url: jdbc:log4jdbc:mysql://${DB_HOST:127.0.0.1}:${DB_PORT:3306}/${DB_NAME:hl_tb_lms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true
|
url: jdbc:log4jdbc:mysql://${DB_HOST:127.0.0.1}:${DB_PORT:3306}/${DB_NAME:lms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true
|
||||||
# username: ${DB_USER:root}
|
# username: ${DB_USER:root}
|
||||||
username: ${DB_USER:root}
|
username: ${DB_USER:root}
|
||||||
# password: ${DB_PWD:123456}
|
# password: ${DB_PWD:123456}
|
||||||
password: ${DB_PWD:root}
|
password: ${DB_PWD:942464Yy}
|
||||||
# 初始连接数
|
# 初始连接数
|
||||||
initial-size: 5
|
initial-size: 5
|
||||||
# 最小连接数
|
# 最小连接数
|
||||||
@@ -93,7 +107,7 @@ login:
|
|||||||
# 字体大小
|
# 字体大小
|
||||||
font-size: 25
|
font-size: 25
|
||||||
|
|
||||||
#jwt
|
#jwt/api/esLog
|
||||||
jwt:
|
jwt:
|
||||||
header: Authorization
|
header: Authorization
|
||||||
# 令牌前缀
|
# 令牌前缀
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ security:
|
|||||||
# 参数
|
# 参数
|
||||||
- /api/param/getValueByCode
|
- /api/param/getValueByCode
|
||||||
- /plumelog/**
|
- /plumelog/**
|
||||||
|
- /api/esLog/**
|
||||||
# Sa-Token配置
|
# Sa-Token配置
|
||||||
sa-token:
|
sa-token:
|
||||||
# token 名称 (同时也是cookie名称)
|
# token 名称 (同时也是cookie名称)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ https://juejin.cn/post/6844903775631572999
|
|||||||
<contextName>nlAdmin</contextName>
|
<contextName>nlAdmin</contextName>
|
||||||
<property name="log.charset" value="utf-8"/>
|
<property name="log.charset" value="utf-8"/>
|
||||||
<property name="log.pattern"
|
<property name="log.pattern"
|
||||||
value="%black(%contextName-) %red(%d{yyyy-MM-dd HH:mm:ss.SSS}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{36}) - %gray(%msg%n)"/>
|
value="%black(%contextName-) %X{traceId} %red(%d{yyyy-MM-dd HH:mm:ss.SSS}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{36}) - %gray(%msg%n)"/>
|
||||||
<springProperty scope="context" name="logPath" source="logging.file.path" defaultValue="logs"/>
|
<springProperty scope="context" name="logPath" source="logging.file.path" defaultValue="logs"/>
|
||||||
<springProperty scope="context" name="lokiUrl" source="loki.url"/>
|
<springProperty scope="context" name="lokiUrl" source="loki.url"/>
|
||||||
<springProperty scope="context" name="systemName" source="loki.systemName"/>
|
<springProperty scope="context" name="systemName" source="loki.systemName"/>
|
||||||
@@ -49,9 +49,9 @@ https://juejin.cn/post/6844903775631572999
|
|||||||
|
|
||||||
|
|
||||||
<appender name="esLogAppender" class="com.internetitem.logback.elasticsearch.ElasticsearchAppender">
|
<appender name="esLogAppender" class="com.internetitem.logback.elasticsearch.ElasticsearchAppender">
|
||||||
<url>http://10.1.3.90:9200/_bulk</url>
|
<url>http://47.96.133.178:8200/_bulk</url>
|
||||||
<index>lms_root</index>
|
<index>lms_log</index>
|
||||||
<type>lms_root</type>
|
<type>lms_log</type>
|
||||||
<loggerName>es-logger</loggerName> <!-- optional -->
|
<loggerName>es-logger</loggerName> <!-- optional -->
|
||||||
<errorLoggerName>es-error-logger</errorLoggerName> <!-- optional -->
|
<errorLoggerName>es-error-logger</errorLoggerName> <!-- optional -->
|
||||||
<connectTimeout>30000</connectTimeout> <!-- optional (in ms, default 30000) -->
|
<connectTimeout>30000</connectTimeout> <!-- optional (in ms, default 30000) -->
|
||||||
@@ -68,30 +68,33 @@ https://juejin.cn/post/6844903775631572999
|
|||||||
<!-- <authentication class="com.internetitem.logback.elasticsearch.config.BasicAuthentication" /> <!– optional –>-->
|
<!-- <authentication class="com.internetitem.logback.elasticsearch.config.BasicAuthentication" /> <!– optional –>-->
|
||||||
<properties>
|
<properties>
|
||||||
<property>
|
<property>
|
||||||
<name>host</name>
|
<name>trackId</name>
|
||||||
<value>${HOSTNAME}</value>
|
<value>%X{trackId}</value>
|
||||||
<allowEmpty>false</allowEmpty>
|
|
||||||
</property>
|
</property>
|
||||||
<property>
|
<property>
|
||||||
<name>severity</name>
|
<name>logLevel</name>
|
||||||
<value>%level</value>
|
<value>%level</value>
|
||||||
</property>
|
</property>
|
||||||
|
<property>
|
||||||
|
<name>requestMethod</name>
|
||||||
|
<value>%X{requestMethod}</value>
|
||||||
|
</property>
|
||||||
|
<property>
|
||||||
|
<name>requestTime</name>
|
||||||
|
<value>%X{requestTime}</value>
|
||||||
|
</property>
|
||||||
|
<property>
|
||||||
|
<name>requestIp</name>
|
||||||
|
<value>%X{requestIp}</value>
|
||||||
|
</property>
|
||||||
<property>
|
<property>
|
||||||
<name>thread</name>
|
<name>thread</name>
|
||||||
<value>%thread</value>
|
<value>%thread</value>
|
||||||
</property>
|
</property>
|
||||||
<property>
|
|
||||||
<name>stacktrace</name>
|
|
||||||
<value>%ex</value>
|
|
||||||
</property>
|
|
||||||
<property>
|
<property>
|
||||||
<name>logger</name>
|
<name>logger</name>
|
||||||
<value>%logger</value>
|
<value>%logger</value>
|
||||||
</property>
|
</property>
|
||||||
<property>
|
|
||||||
<name>traceId</name>
|
|
||||||
<value>%X{traceId}</value>
|
|
||||||
</property>
|
|
||||||
</properties>
|
</properties>
|
||||||
<headers>
|
<headers>
|
||||||
<header>
|
<header>
|
||||||
@@ -133,7 +136,6 @@ https://juejin.cn/post/6844903775631572999
|
|||||||
<root level="debug">
|
<root level="debug">
|
||||||
<appender-ref ref="CONSOLE"/>
|
<appender-ref ref="CONSOLE"/>
|
||||||
<appender-ref ref="asyncFileAppender"/>
|
<appender-ref ref="asyncFileAppender"/>
|
||||||
<appender-ref ref="ERROR" />
|
|
||||||
<appender-ref ref="esLogAppender" />
|
<appender-ref ref="esLogAppender" />
|
||||||
</root>
|
</root>
|
||||||
<logger name="org.springframework" level="ERROR" additivity="false">
|
<logger name="org.springframework" level="ERROR" additivity="false">
|
||||||
|
|||||||
Reference in New Issue
Block a user