add:es
This commit is contained in:
@@ -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.server.ServletWebServerFactory;
|
||||
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.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
@@ -36,6 +37,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@EnableMethodCache(basePackages = "org.nl")
|
||||
@EnableCreateCacheAnnotation
|
||||
@MapperScan("org.nl.**.mapper")
|
||||
//@EnableElasticsearchRepositories(basePackages = {"org.nl.modules.logging.repository.*"})
|
||||
public class AppRun {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -38,7 +38,7 @@ public class ElasticSearchTest {
|
||||
|
||||
public static void searchById() throws IOException {
|
||||
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);
|
||||
System.out.println(getResponse.getSourceAsString());
|
||||
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.service.LogService;
|
||||
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.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
@@ -53,52 +55,39 @@ import java.util.*;
|
||||
@Slf4j
|
||||
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()上注册的切入点
|
||||
*
|
||||
* @param joinPoint join point for advice
|
||||
*/
|
||||
@Around("logPointcut()")
|
||||
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
@Around("@annotation(logInfo)")
|
||||
public Object logAround(ProceedingJoinPoint joinPoint,org.nl.modules.logging.annotation.Log logInfo) throws Throwable {
|
||||
String trackId = UUID.randomUUID().toString();
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
// 方法路径
|
||||
String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
|
||||
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();
|
||||
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 {
|
||||
log.info("[--request--][请求方法:{}][请求参数:{}]",methodName,params);
|
||||
result = joinPoint.proceed();
|
||||
//是否把日志存到日志表
|
||||
if (logInfo.isAddLogTable()) {
|
||||
Log log = new Log("INFO", System.currentTimeMillis() - currentTime.get());
|
||||
currentTime.remove();
|
||||
Log log = new Log("INFO", System.currentTimeMillis() - comming);
|
||||
logService.save(getUsername(), StringUtils.getBrowser(request), requestIp, joinPoint, log);
|
||||
}
|
||||
if (logInfo.isInterfaceLog()) {
|
||||
@@ -112,25 +101,29 @@ public class LogAspect {
|
||||
json.put("method", methodName);
|
||||
json.put("params", getParameter(method, joinPoint.getArgs()));
|
||||
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("address", StringUtils.getCityInfo(requestIp));
|
||||
json.put("browser", StringUtils.getBrowser(request));
|
||||
json.put("exception_detail", IdUtil.getStringId());
|
||||
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);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}catch (Exception ex){
|
||||
log.error("track_id:{},error:{}",trackId,ex.getMessage());
|
||||
Log log = new Log("ERROR", System.currentTimeMillis() - currentTime.get());
|
||||
currentTime.remove();
|
||||
log.info("[--requestError--][请求方法:{}]【异常信息 :{}】", methodName, ex);
|
||||
Log log = new Log("ERROR", System.currentTimeMillis() - comming);
|
||||
log.setExceptionDetail(ThrowableUtil.getStackTrace(ex).getBytes());
|
||||
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint) joinPoint, log);
|
||||
throw ex;
|
||||
}finally {
|
||||
log.info("[--response--][请求方法:{}][请求参数:{}]【返回结果 :{}】",methodName,params,result);
|
||||
MDC.clear();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -165,21 +158,6 @@ public class LogAspect {
|
||||
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() {
|
||||
try {
|
||||
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 lombok.RequiredArgsConstructor;
|
||||
import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.modules.logging.service.EsLogService;
|
||||
import org.nl.modules.logging.service.dto.LogQuery;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @author ldjun
|
||||
@@ -22,10 +22,17 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
public class EsLogController {
|
||||
private final EsLogService esLogService;
|
||||
|
||||
|
||||
@GetMapping("/labels")
|
||||
@ApiOperation("获取标签")
|
||||
public ResponseEntity<Object> labelsValues() {
|
||||
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;
|
||||
|
||||
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
|
||||
@@ -14,4 +17,11 @@ public interface EsLogService {
|
||||
* @return
|
||||
*/
|
||||
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.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
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.dto.LogQuery;
|
||||
import org.nl.modules.logging.service.dto.LogRepositoryDTO;
|
||||
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 java.util.Map;
|
||||
@@ -20,57 +34,62 @@ import java.util.Map;
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
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
|
||||
public JSONArray getLabelsValues() {
|
||||
/**
|
||||
* [{
|
||||
* label:
|
||||
* value:
|
||||
* children:[{
|
||||
* label
|
||||
* 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);
|
||||
JSONArray result = new JSONArray();
|
||||
for (LevelEnum value : LevelEnum.values()) {
|
||||
JSONObject level = new JSONObject();
|
||||
level.put("label", value.name());
|
||||
level.put("Value", value.name());
|
||||
result.add(level);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user