opt:优化日志
This commit is contained in:
@@ -14,11 +14,10 @@ import org.nl.acs.instruction.service.InstructionService;
|
||||
import org.nl.acs.instruction.service.impl.InstructionServiceImpl;
|
||||
import org.nl.acs.log.service.DeviceExecuteLogService;
|
||||
import org.nl.acs.opc.DeviceAppService;
|
||||
import org.nl.config.SpringContextHolder;
|
||||
import org.nl.system.service.dict.ISysDictService;
|
||||
import org.nl.system.service.dict.dao.Dict;
|
||||
import org.nl.system.service.lucene.LuceneExecuteLogService;
|
||||
import org.nl.system.service.param.ISysParamService;
|
||||
import org.nl.config.SpringContextHolder;
|
||||
import org.nl.system.service.param.impl.SysParamServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
@@ -166,8 +165,8 @@ public class OneNDCSocketConnectionAutoRun extends AbstractAutoRunnable {
|
||||
inst.setAgv_jobno(String.valueOf(index));
|
||||
inst.setSend_status("1");
|
||||
instructionService.update(inst);
|
||||
data = NDCAgvService.sendAgvOneModeInst(phase, index, 0, 0, 0, 0, 0);
|
||||
}
|
||||
data = NDCAgvService.sendAgvOneModeInst(phase, index, 0, 0, 0, 0, 0);
|
||||
}
|
||||
//任务完毕
|
||||
//(无车id及状态)
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.nl.acs.device.domain.Device;
|
||||
import org.nl.acs.device.service.DeviceService;
|
||||
import org.nl.acs.device_driver.DeviceDriver;
|
||||
import org.nl.acs.device_driver.basedriver.agv.utils.AgvActionEnum;
|
||||
import org.nl.acs.device_driver.basedriver.standard_ordinary_site.StandardOrdinarySiteDeviceDriver;
|
||||
import org.nl.acs.device_driver.driver.AbstractDeviceDriver;
|
||||
import org.nl.acs.device_driver.yinfei.hj1.Hj1DeviceDriver;
|
||||
import org.nl.acs.device_driver.yinfei.hj2.Hj2DeviceDriver;
|
||||
@@ -236,8 +237,6 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//到达桁架2请求取货
|
||||
if (device.getDeviceDriver() instanceof Hj2DeviceDriver) {
|
||||
hj2DeviceDriver = (Hj2DeviceDriver) device.getDeviceDriver();
|
||||
@@ -281,7 +280,9 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
if (device.getDeviceDriver() instanceof SsxDeviceDriver) {
|
||||
ssxDeviceDriver = (SsxDeviceDriver) device.getDeviceDriver();
|
||||
//有货,待机,运行取货
|
||||
log.info("tofix==================agv到达取货点==================");
|
||||
if (ssxDeviceDriver.getMove() == 1 & ssxDeviceDriver.getMode() == 1 & ssxDeviceDriver.getAction() == 1) {
|
||||
log.info("tofix==================agv到达取货点,有货,待机,运行取货,允许AGV取货==================");
|
||||
//agv进入取货
|
||||
inst.setExecute_status("1");
|
||||
instructionService.update(inst);
|
||||
@@ -290,6 +291,7 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
ssxDeviceDriver.setMessage("");
|
||||
flag = true;
|
||||
} else {
|
||||
log.info("tofix==================agv到达取货点,不允许取货==================");
|
||||
String message = "";
|
||||
if (ssxDeviceDriver.getMode() == 0) {
|
||||
message += "输送线故障,";
|
||||
@@ -306,6 +308,15 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message));
|
||||
}
|
||||
}
|
||||
//到达普通站点
|
||||
if (device.getDeviceDriver() instanceof StandardOrdinarySiteDeviceDriver) {
|
||||
log.info("tofix==================agv到达普通站点取货点===================agvphase:" + phase + "反馈成功");
|
||||
inst.setExecute_status("1");
|
||||
instructionService.update(inst);
|
||||
//让agv进入,phase agv运行阶段,index agv任务号 0 用不上
|
||||
data = ndcAgvService.sendAgvOneModeInst(phase, index, 0);
|
||||
flag = true;
|
||||
}
|
||||
if (flag) {
|
||||
log.info("==================允许AGV取货==================");
|
||||
logServer.deviceExecuteLog(device_code, "", "", "允许AGV取货。");
|
||||
@@ -352,8 +363,6 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
logServer.deviceExecuteLog(this.device_code, "", "", "未找到关联编号对应的指令" + ikey);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//到达桁架1取货完成
|
||||
if (device.getDeviceDriver() instanceof Hj1DeviceDriver) {
|
||||
hj1DeviceDriver = (Hj1DeviceDriver) device.getDeviceDriver();
|
||||
@@ -385,8 +394,6 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//到达桁架2取货完成
|
||||
if (device.getDeviceDriver() instanceof Hj2DeviceDriver) {
|
||||
hj2DeviceDriver = (Hj2DeviceDriver) device.getDeviceDriver();
|
||||
@@ -418,10 +425,10 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message));
|
||||
}
|
||||
}
|
||||
|
||||
//到达输送线取货完成
|
||||
if (device.getDeviceDriver() instanceof SsxDeviceDriver) {
|
||||
ssxDeviceDriver = (SsxDeviceDriver) device.getDeviceDriver();
|
||||
log.info("tofix==================agv取货完成==================");
|
||||
if (ssxDeviceDriver.getMode() != 0 && ssxDeviceDriver.getMove() == 0) {
|
||||
if (ssxDeviceDriver.getTo_command() == 3) {
|
||||
//反馈车辆动作
|
||||
@@ -433,6 +440,7 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
ssxDeviceDriver.setMessage("");
|
||||
flag = true;
|
||||
} else {
|
||||
log.info("tofix==================agv允许取货离开==================");
|
||||
String message = "";
|
||||
if (ssxDeviceDriver.getMode() == 0) {
|
||||
message += "站点未联机,";
|
||||
@@ -449,14 +457,19 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message));
|
||||
}
|
||||
}
|
||||
//到达普通站点
|
||||
if (device.getDeviceDriver() instanceof StandardOrdinarySiteDeviceDriver) {
|
||||
log.info("tofix==================agv到达普通站点取货完成==================agvphase:" + phase + "反馈成功");
|
||||
//让agv进入,phase agv运行阶段,index agv任务号 0 用不上
|
||||
data = ndcAgvService.sendAgvOneModeInst(phase, index, 0);
|
||||
flag = true;
|
||||
}
|
||||
if (flag) {
|
||||
log.info("================允许AGV取货后离开=================");
|
||||
logServer.deviceExecuteLog(device_code, "", "", "允许AGV取货后离开。");
|
||||
log.info("{},{}", device_code, "允许AGV取货后离开。");
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, "允许AGV取货后离开。"));
|
||||
}
|
||||
|
||||
|
||||
//到达放货等待点
|
||||
//(需要WCS反馈)
|
||||
} else if (phase == 0x06) {
|
||||
@@ -485,7 +498,7 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
}
|
||||
}
|
||||
device = deviceAppService.findDeviceByCode(device_code);
|
||||
if (ObjectUtil.isEmpty(device_code)) {
|
||||
if (ObjectUtil.isEmpty(device_code)&&!device_code.equals("0")) {
|
||||
log.info(agvaddr + "对应设备号为空!");
|
||||
return;
|
||||
}
|
||||
@@ -494,8 +507,6 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
log.info("未找到关联编号{}对应的指令", ikey);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//到达桁架1请求放货
|
||||
if (device.getDeviceDriver() instanceof Hj1DeviceDriver) {
|
||||
hj1DeviceDriver = (Hj1DeviceDriver) device.getDeviceDriver();
|
||||
@@ -536,8 +547,6 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//到达桁架2请求放货
|
||||
if (device.getDeviceDriver() instanceof Hj2DeviceDriver) {
|
||||
hj2DeviceDriver = (Hj2DeviceDriver) device.getDeviceDriver();
|
||||
@@ -578,12 +587,13 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message));
|
||||
}
|
||||
}
|
||||
|
||||
//输送线到达放货点
|
||||
if (device.getDeviceDriver() instanceof SsxDeviceDriver) {
|
||||
log.info("tofix==================agv到达放货点==================");
|
||||
ssxDeviceDriver = (SsxDeviceDriver) device.getDeviceDriver();
|
||||
//无货,待机,运行放货
|
||||
if (ssxDeviceDriver.getMove() == 0 & ssxDeviceDriver.getMode() == 1 & ssxDeviceDriver.getAction() == 1) {
|
||||
log.info("tofix==================agv到达放货点,无货,待机,运行放货,允许AGV放货==================");
|
||||
if (inst != null) {
|
||||
inst.setExecute_status("3");
|
||||
instructionService.update(inst);
|
||||
@@ -592,6 +602,7 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
ssxDeviceDriver.setMessage("");
|
||||
flag = true;
|
||||
} else {
|
||||
log.info("tofix==================agv到达放货点,不允许放货==================");
|
||||
String message = "";
|
||||
if (ssxDeviceDriver.getMode() == 0) {
|
||||
message += "站点未联机,";
|
||||
@@ -608,13 +619,19 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message));
|
||||
}
|
||||
}
|
||||
//普通站点
|
||||
if (device.getDeviceDriver() instanceof StandardOrdinarySiteDeviceDriver) {
|
||||
log.info("tofix==================agv到达放货点==================agvphase:" + phase + "反馈成功");
|
||||
//让agv进入,phase agv运行阶段,index agv任务号 0 用不上
|
||||
data = ndcAgvService.sendAgvOneModeInst(phase, index, 0);
|
||||
flag = true;
|
||||
}
|
||||
if (flag) {
|
||||
log.info("==================允许AGV放货==================");
|
||||
logServer.deviceExecuteLog(device_code, "", "", "允许AGV放货。");
|
||||
log.info("{},{}", device_code, "允许AGV放货。");
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, "允许AGV放货。"));
|
||||
}
|
||||
|
||||
//放货完毕
|
||||
//(需要WCS反馈)
|
||||
} else if (phase == 0x09) {
|
||||
@@ -638,7 +655,6 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
device_code = old_device_code;
|
||||
}
|
||||
}
|
||||
|
||||
device = deviceAppService.findDeviceByCode(device_code);
|
||||
if (ObjectUtil.isEmpty(device_code)) {
|
||||
log.info(agvaddr + "对应设备号为空!");
|
||||
@@ -651,8 +667,6 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
return;
|
||||
}
|
||||
transportOrder = "";
|
||||
|
||||
|
||||
//到达桁架1放货完成
|
||||
if (device.getDeviceDriver() instanceof Hj1DeviceDriver) {
|
||||
hj1DeviceDriver = (Hj1DeviceDriver) device.getDeviceDriver();
|
||||
@@ -684,7 +698,6 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message));
|
||||
}
|
||||
}
|
||||
|
||||
//到达桁架2放货完成
|
||||
if (device.getDeviceDriver() instanceof Hj2DeviceDriver) {
|
||||
hj2DeviceDriver = (Hj2DeviceDriver) device.getDeviceDriver();
|
||||
@@ -716,9 +729,9 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message));
|
||||
}
|
||||
}
|
||||
|
||||
//到达输送线放货完成
|
||||
if (device.getDeviceDriver() instanceof SsxDeviceDriver) {
|
||||
log.info("tofix==================agv放货完成==================");
|
||||
ssxDeviceDriver = (SsxDeviceDriver) device.getDeviceDriver();
|
||||
ssxDeviceDriver.writing(4);
|
||||
if (ssxDeviceDriver.getMode() != 0 && ssxDeviceDriver.getMove() != 0) {
|
||||
@@ -740,6 +753,7 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
message += "站点无货,";
|
||||
}
|
||||
}
|
||||
log.info("tofix==================agv放货完成,不允许离开==================");
|
||||
message += "不允许AGV放货后离开。";
|
||||
ssxDeviceDriver.setMessage(message);
|
||||
agv_message = ssxDeviceDriver.getDevice_code() + message;
|
||||
@@ -748,7 +762,13 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message));
|
||||
}
|
||||
}
|
||||
|
||||
//agv普通站点放货完成
|
||||
if (device.getDeviceDriver() instanceof StandardOrdinarySiteDeviceDriver) {
|
||||
log.info("tofix==================agv普通站点放货完成==================agvphase:" + phase + "反馈成功");
|
||||
//让agv进入,phase agv运行阶段,index agv任务号 0 用不上
|
||||
data = ndcAgvService.sendAgvOneModeInst(phase, index, 0);
|
||||
flag = true;
|
||||
}
|
||||
if (flag) {
|
||||
log.info("================允许AGV放货后离开=================");
|
||||
logServer.deviceExecuteLog(device_code, "", "", "允许AGV放货后离开。");
|
||||
@@ -795,8 +815,6 @@ public class AgvNdcOneDeviceDriver extends AbstractDeviceDriver implements Devic
|
||||
} else if (phase == 0x75) {
|
||||
is_have = ikey;
|
||||
}
|
||||
|
||||
|
||||
if (!ObjectUtil.isEmpty(data)) {
|
||||
logServer.deviceExecuteLog(this.device_code, "", "", "agvphase:" + phase + "反馈:" + data);
|
||||
OneNDCSocketConnectionAutoRun.write(data);
|
||||
|
||||
@@ -126,6 +126,7 @@ public class StandardOrdinarySiteDeviceDriver extends AbstractDeviceDriver imple
|
||||
if (ObjectUtil.isNotEmpty(inst)) {
|
||||
inst.setExecute_status("1");
|
||||
instructionService.update(inst);
|
||||
log.info("agvphase:" + agvphase + "反馈成功,到达普通站点。");
|
||||
byte[] data = agvService.sendAgvOneModeInst(agvphase, index, 0,0,0,0,0);
|
||||
agvphase = 0;
|
||||
index = 0;
|
||||
@@ -141,6 +142,7 @@ public class StandardOrdinarySiteDeviceDriver extends AbstractDeviceDriver imple
|
||||
if (ObjectUtil.isNotEmpty(inst)) {
|
||||
inst.setExecute_status("2");
|
||||
instructionService.update(inst);
|
||||
log.info("agvphase:" + agvphase + "反馈成功,取货完成离开普通站点。");
|
||||
byte[] data = agvService.sendAgvOneModeInst(agvphase, index, 0,0,0,0,0);
|
||||
// OneNDCSocketConnectionAutoRun.write(data);
|
||||
agvphase = 0;
|
||||
@@ -157,6 +159,7 @@ public class StandardOrdinarySiteDeviceDriver extends AbstractDeviceDriver imple
|
||||
if (ObjectUtil.isNotEmpty(inst)) {
|
||||
inst.setExecute_status("5");
|
||||
instructionService.update(inst);
|
||||
log.info("agvphase:" + agvphase + "反馈成功,放货完成。");
|
||||
byte[] data = agvService.sendAgvOneModeInst(agvphase, index, 0,0,0,0,0);
|
||||
// OneNDCSocketConnectionAutoRun.write(data);
|
||||
agvphase = 0;
|
||||
@@ -172,6 +175,7 @@ public class StandardOrdinarySiteDeviceDriver extends AbstractDeviceDriver imple
|
||||
if (ObjectUtil.isNotEmpty(inst)) {
|
||||
inst.setExecute_status("6");
|
||||
instructionService.update(inst);
|
||||
log.info("agvphase:" + agvphase + "反馈成功,取货完成请求离开普通站点。");
|
||||
byte[] data = agvService.sendAgvOneModeInst(agvphase, index, 0,0,0,0,0);
|
||||
// OneNDCSocketConnectionAutoRun.write(data);
|
||||
agvphase = 0;
|
||||
|
||||
@@ -1,71 +1,71 @@
|
||||
//package org.nl.acs.log.rest;
|
||||
//
|
||||
//import org.nl.common.logging.annotation.Log;
|
||||
//import org.nl.acs.log.service.LogService;
|
||||
//import org.nl.acs.log.service.dto.LogDto;
|
||||
//import org.nl.acs.log.service.dto.LogQueryParam;
|
||||
//import org.springframework.data.domain.Pageable;
|
||||
//import lombok.RequiredArgsConstructor;
|
||||
//import org.springframework.http.HttpStatus;
|
||||
//import org.springframework.http.ResponseEntity;
|
||||
//import org.springframework.validation.annotation.Validated;
|
||||
//import org.springframework.web.bind.annotation.*;
|
||||
//import io.swagger.annotations.*;
|
||||
//import java.util.Set;
|
||||
//
|
||||
///**
|
||||
//* @author jiaolm
|
||||
//* @date 2023-05-09
|
||||
//**/
|
||||
//@RestController
|
||||
//@RequiredArgsConstructor
|
||||
//@Api(tags = "acs日志管理")
|
||||
//@RequestMapping("/api/log")
|
||||
//public class LogController {
|
||||
//
|
||||
// private final LogService logService;
|
||||
//
|
||||
// @GetMapping
|
||||
// @Log("查询acs日志")
|
||||
// @ApiOperation("查询acs日志")
|
||||
// //@PreAuthorize("@el.check('log:list')")
|
||||
// public ResponseEntity query(LogQueryParam query, Pageable pageable){
|
||||
// return new ResponseEntity<>(logService.queryAll(query,pageable),HttpStatus.OK);
|
||||
// }
|
||||
//
|
||||
// @PostMapping
|
||||
// @Log("新增acs日志")
|
||||
// @ApiOperation("新增acs日志")
|
||||
// //@PreAuthorize("@el.check('log:add')")
|
||||
// public ResponseEntity create(@Validated @RequestBody LogDto resources){
|
||||
// return new ResponseEntity<>(logService.insert(resources),HttpStatus.CREATED);
|
||||
// }
|
||||
//
|
||||
// @PutMapping
|
||||
// @Log("修改acs日志")
|
||||
// @ApiOperation("修改acs日志")
|
||||
// //@PreAuthorize("@el.check('log:edit')")
|
||||
// public ResponseEntity update(@Validated @RequestBody LogDto resources){
|
||||
// logService.updateById(resources);
|
||||
// return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
// }
|
||||
//
|
||||
// @DeleteMapping
|
||||
// @Log("删除acs日志")
|
||||
// @ApiOperation("删除acs日志")
|
||||
// //@PreAuthorize("@el.check('log:del')")
|
||||
// public ResponseEntity delete(@RequestBody Set<String> ids) {
|
||||
// logService.removeByIds(ids);
|
||||
// return new ResponseEntity<>(HttpStatus.OK);
|
||||
// }
|
||||
//
|
||||
// /*
|
||||
// @Log("导出acs日志")
|
||||
// @ApiOperation("导出acs日志")
|
||||
// @GetMapping(value = "/download")
|
||||
// //@PreAuthorize("@el.check('log:list')")
|
||||
// public void download(HttpServletResponse response, LogQueryParam query) throws IOException {
|
||||
// logService.download(logService.queryAll(query), response);
|
||||
// }*/
|
||||
//
|
||||
//}
|
||||
package org.nl.acs.log.rest;
|
||||
|
||||
import org.nl.common.logging.annotation.Log;
|
||||
import org.nl.acs.log.service.LogService;
|
||||
import org.nl.acs.log.service.dto.LogDto;
|
||||
import org.nl.acs.log.service.dto.LogQueryParam;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import io.swagger.annotations.*;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author jiaolm
|
||||
* @date 2023-05-09
|
||||
**/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Api(tags = "acs日志管理")
|
||||
@RequestMapping("/api/log")
|
||||
public class LogController {
|
||||
|
||||
private final LogService logService;
|
||||
|
||||
@GetMapping
|
||||
@Log("查询acs日志")
|
||||
@ApiOperation("查询acs日志")
|
||||
//@PreAuthorize("@el.check('log:list')")
|
||||
public ResponseEntity query(LogQueryParam query, Pageable pageable){
|
||||
return new ResponseEntity<>(logService.queryAll(query,pageable),HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@Log("新增acs日志")
|
||||
@ApiOperation("新增acs日志")
|
||||
//@PreAuthorize("@el.check('log:add')")
|
||||
public ResponseEntity create(@Validated @RequestBody LogDto resources){
|
||||
return new ResponseEntity<>(logService.insert(resources),HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@Log("修改acs日志")
|
||||
@ApiOperation("修改acs日志")
|
||||
//@PreAuthorize("@el.check('log:edit')")
|
||||
public ResponseEntity update(@Validated @RequestBody LogDto resources){
|
||||
logService.updateById(resources);
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
@Log("删除acs日志")
|
||||
@ApiOperation("删除acs日志")
|
||||
//@PreAuthorize("@el.check('log:del')")
|
||||
public ResponseEntity delete(@RequestBody Set<String> ids) {
|
||||
logService.removeByIds(ids);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
/*
|
||||
@Log("导出acs日志")
|
||||
@ApiOperation("导出acs日志")
|
||||
@GetMapping(value = "/download")
|
||||
//@PreAuthorize("@el.check('log:list')")
|
||||
public void download(HttpServletResponse response, LogQueryParam query) throws IOException {
|
||||
logService.download(logService.queryAll(query), response);
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
@@ -1,53 +1,53 @@
|
||||
//package org.nl.acs.log.service;
|
||||
//
|
||||
//import org.nl.acs.common.base.PageInfo;
|
||||
//import org.nl.acs.common.base.CommonService;
|
||||
//import org.nl.acs.log.domain.Log;
|
||||
//import org.nl.acs.log.service.dto.LogDto;
|
||||
//import org.nl.acs.log.service.dto.LogQueryParam;
|
||||
//import org.springframework.data.domain.Pageable;
|
||||
//import java.util.List;
|
||||
//import java.util.Set;
|
||||
//
|
||||
///**
|
||||
//* @author jiaolm
|
||||
//* @date 2023-05-09
|
||||
//*/
|
||||
//public interface LogService extends CommonService<Log> {
|
||||
//
|
||||
// static final String CACHE_KEY = "log";
|
||||
//
|
||||
// /**
|
||||
// * 查询数据分页
|
||||
// * @param query 条件
|
||||
// * @param pageable 分页参数
|
||||
// * @return PageInfo<LogDto>
|
||||
// */
|
||||
// PageInfo<LogDto> queryAll(LogQueryParam query, Pageable pageable);
|
||||
//
|
||||
// /**
|
||||
// * 查询所有数据不分页
|
||||
// * @param query 条件参数
|
||||
// * @return List<LogDto>
|
||||
// */
|
||||
// List<LogDto> queryAll(LogQueryParam query);
|
||||
//
|
||||
// Log getById(String id);
|
||||
// LogDto findById(String id);
|
||||
//
|
||||
// /**
|
||||
// * 插入一条新数据。
|
||||
// */
|
||||
// int insert(LogDto resources);
|
||||
// int updateById(LogDto resources);
|
||||
// int removeById(String id);
|
||||
// int removeByIds(Set<String> ids);
|
||||
//
|
||||
// /**
|
||||
// * 导出数据
|
||||
// * @param all 待导出的数据
|
||||
// * @param response /
|
||||
// * @throws IOException /
|
||||
// */
|
||||
// // void download(List<LogDto> all, HttpServletResponse response) throws IOException;
|
||||
//}
|
||||
package org.nl.acs.log.service;
|
||||
|
||||
import org.nl.acs.common.base.PageInfo;
|
||||
import org.nl.acs.common.base.CommonService;
|
||||
import org.nl.acs.log.domain.Log;
|
||||
import org.nl.acs.log.service.dto.LogDto;
|
||||
import org.nl.acs.log.service.dto.LogQueryParam;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author jiaolm
|
||||
* @date 2023-05-09
|
||||
*/
|
||||
public interface LogService extends CommonService<Log> {
|
||||
|
||||
static final String CACHE_KEY = "log";
|
||||
|
||||
/**
|
||||
* 查询数据分页
|
||||
* @param query 条件
|
||||
* @param pageable 分页参数
|
||||
* @return PageInfo<LogDto>
|
||||
*/
|
||||
PageInfo<LogDto> queryAll(LogQueryParam query, Pageable pageable);
|
||||
|
||||
/**
|
||||
* 查询所有数据不分页
|
||||
* @param query 条件参数
|
||||
* @return List<LogDto>
|
||||
*/
|
||||
List<LogDto> queryAll(LogQueryParam query);
|
||||
|
||||
Log getById(String id);
|
||||
LogDto findById(String id);
|
||||
|
||||
/**
|
||||
* 插入一条新数据。
|
||||
*/
|
||||
int insert(LogDto resources);
|
||||
int updateById(LogDto resources);
|
||||
int removeById(String id);
|
||||
int removeByIds(Set<String> ids);
|
||||
|
||||
/**
|
||||
* 导出数据
|
||||
* @param all 待导出的数据
|
||||
* @param response /
|
||||
* @throws IOException /
|
||||
*/
|
||||
// void download(List<LogDto> all, HttpServletResponse response) throws IOException;
|
||||
}
|
||||
|
||||
@@ -1,128 +1,128 @@
|
||||
//package org.nl.acs.log.service.impl;
|
||||
//
|
||||
//import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
//import lombok.AllArgsConstructor;
|
||||
//import org.nl.acs.common.base.PageInfo;
|
||||
//import org.nl.acs.common.base.QueryHelpMybatisPlus;
|
||||
//import org.nl.acs.common.base.impl.CommonServiceImpl;
|
||||
//import org.nl.acs.utils.ConvertUtil;
|
||||
//import org.nl.acs.utils.PageUtil;
|
||||
//import org.nl.acs.log.domain.Log;
|
||||
//import org.nl.acs.log.service.LogService;
|
||||
//import org.nl.acs.log.service.dto.LogDto;
|
||||
//import org.nl.acs.log.service.dto.LogQueryParam;
|
||||
//import org.nl.acs.log.service.mapper.LogMapper;
|
||||
//import org.springframework.stereotype.Service;
|
||||
//import org.springframework.transaction.annotation.Propagation;
|
||||
//import org.springframework.transaction.annotation.Transactional;
|
||||
//// 默认不使用缓存
|
||||
////import org.springframework.cache.annotation.CacheConfig;
|
||||
////import org.springframework.cache.annotation.CacheEvict;
|
||||
////import org.springframework.cache.annotation.Cacheable;
|
||||
//import org.springframework.data.domain.Pageable;
|
||||
//import java.util.*;
|
||||
//
|
||||
///**
|
||||
//* @author jiaolm
|
||||
//* @date 2023-05-09
|
||||
//*/
|
||||
//@Service
|
||||
//@AllArgsConstructor
|
||||
//// @CacheConfig(cacheNames = LogService.CACHE_KEY)
|
||||
//@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
|
||||
//public class LogServiceImpl extends CommonServiceImpl<LogMapper, Log> implements LogService {
|
||||
//
|
||||
// // private final RedisUtils redisUtils;
|
||||
// private final LogMapper logMapper;
|
||||
//
|
||||
// @Override
|
||||
// public PageInfo<LogDto> queryAll(LogQueryParam query, Pageable pageable) {
|
||||
// IPage<Log> queryPage = PageUtil.toMybatisPage(pageable);
|
||||
// IPage<Log> page = logMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
|
||||
// return ConvertUtil.convertPage(page, LogDto.class);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public List<LogDto> queryAll(LogQueryParam query){
|
||||
// return ConvertUtil.convertList(logMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), LogDto.class);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public Log getById(String id) {
|
||||
// return logMapper.selectById(id);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// // @Cacheable(key = "'id:' + #p0")
|
||||
// public LogDto findById(String id) {
|
||||
// return ConvertUtil.convert(getById(id), LogDto.class);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// @Transactional(rollbackFor = Exception.class)
|
||||
// public int insert(LogDto resources) {
|
||||
// Log entity = ConvertUtil.convert(resources, Log.class);
|
||||
// return logMapper.insert(entity);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// @Transactional(rollbackFor = Exception.class)
|
||||
// public int updateById(LogDto resources){
|
||||
// Log entity = ConvertUtil.convert(resources, Log.class);
|
||||
// int ret = logMapper.updateById(entity);
|
||||
// // delCaches(resources.id);
|
||||
// return ret;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// @Transactional(rollbackFor = Exception.class)
|
||||
// public int removeByIds(Set<String> ids){
|
||||
// // delCaches(ids);
|
||||
// return logMapper.deleteBatchIds(ids);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// @Transactional(rollbackFor = Exception.class)
|
||||
// public int removeById(String id){
|
||||
// Set<String> set = new HashSet<>(1);
|
||||
// set.add(id);
|
||||
// return this.removeByIds(set);
|
||||
// }
|
||||
//
|
||||
// /*
|
||||
// private void delCaches(String id) {
|
||||
// redisUtils.delByKey(CACHE_KEY + "::id:", id);
|
||||
// }
|
||||
//
|
||||
// private void delCaches(Set<String> ids) {
|
||||
// for (String id: ids) {
|
||||
// delCaches(id);
|
||||
// }
|
||||
// }*/
|
||||
//
|
||||
// /*
|
||||
// @Override
|
||||
// public void download(List<LogDto> all, HttpServletResponse response) throws IOException {
|
||||
// List<Map<String, Object>> list = new ArrayList<>();
|
||||
// for (LogDto log : all) {
|
||||
// Map<String,Object> map = new LinkedHashMap<>();
|
||||
// map.put("任务编码", log.getTaskCode());
|
||||
// map.put("任务标识", log.getTaskId());
|
||||
// map.put(" vehicleCode", log.getVehicleCode());
|
||||
// map.put("备注", log.getRemark());
|
||||
// map.put("日志类型", log.getLogType());
|
||||
// map.put("方法", log.getMethod());
|
||||
// map.put("请求参数", log.getRequestparam());
|
||||
// map.put("返回参数", log.getResponseparam());
|
||||
// map.put("请求地址", log.getRequesturl());
|
||||
// map.put("状态码", log.getStatusCode());
|
||||
// map.put("是否删除", log.getIsDelete());
|
||||
// map.put("创建者", log.getCreateBy());
|
||||
// map.put("创建时间", log.getCreateTime());
|
||||
// map.put("修改者", log.getUpdateBy());
|
||||
// map.put("修改时间", log.getUpdateTime());
|
||||
// list.add(map);
|
||||
// }
|
||||
// FileUtil.downloadExcel(list, response);
|
||||
// }*/
|
||||
//}
|
||||
package org.nl.acs.log.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.nl.acs.common.base.PageInfo;
|
||||
import org.nl.acs.common.base.QueryHelpMybatisPlus;
|
||||
import org.nl.acs.common.base.impl.CommonServiceImpl;
|
||||
import org.nl.acs.utils.ConvertUtil;
|
||||
import org.nl.acs.utils.PageUtil;
|
||||
import org.nl.acs.log.domain.Log;
|
||||
import org.nl.acs.log.service.LogService;
|
||||
import org.nl.acs.log.service.dto.LogDto;
|
||||
import org.nl.acs.log.service.dto.LogQueryParam;
|
||||
import org.nl.acs.log.service.mapper.LogMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
// 默认不使用缓存
|
||||
//import org.springframework.cache.annotation.CacheConfig;
|
||||
//import org.springframework.cache.annotation.CacheEvict;
|
||||
//import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author jiaolm
|
||||
* @date 2023-05-09
|
||||
*/
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
// @CacheConfig(cacheNames = LogService.CACHE_KEY)
|
||||
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
|
||||
public class LogServiceImpl extends CommonServiceImpl<LogMapper, Log> implements LogService {
|
||||
|
||||
// private final RedisUtils redisUtils;
|
||||
private final LogMapper logMapper;
|
||||
|
||||
@Override
|
||||
public PageInfo<LogDto> queryAll(LogQueryParam query, Pageable pageable) {
|
||||
IPage<Log> queryPage = PageUtil.toMybatisPage(pageable);
|
||||
IPage<Log> page = logMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
|
||||
return ConvertUtil.convertPage(page, LogDto.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LogDto> queryAll(LogQueryParam query){
|
||||
return ConvertUtil.convertList(logMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), LogDto.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Log getById(String id) {
|
||||
return logMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
// @Cacheable(key = "'id:' + #p0")
|
||||
public LogDto findById(String id) {
|
||||
return ConvertUtil.convert(getById(id), LogDto.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int insert(LogDto resources) {
|
||||
Log entity = ConvertUtil.convert(resources, Log.class);
|
||||
return logMapper.insert(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int updateById(LogDto resources){
|
||||
Log entity = ConvertUtil.convert(resources, Log.class);
|
||||
int ret = logMapper.updateById(entity);
|
||||
// delCaches(resources.id);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int removeByIds(Set<String> ids){
|
||||
// delCaches(ids);
|
||||
return logMapper.deleteBatchIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int removeById(String id){
|
||||
Set<String> set = new HashSet<>(1);
|
||||
set.add(id);
|
||||
return this.removeByIds(set);
|
||||
}
|
||||
|
||||
/*
|
||||
private void delCaches(String id) {
|
||||
redisUtils.delByKey(CACHE_KEY + "::id:", id);
|
||||
}
|
||||
|
||||
private void delCaches(Set<String> ids) {
|
||||
for (String id: ids) {
|
||||
delCaches(id);
|
||||
}
|
||||
}*/
|
||||
|
||||
/*
|
||||
@Override
|
||||
public void download(List<LogDto> all, HttpServletResponse response) throws IOException {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
for (LogDto log : all) {
|
||||
Map<String,Object> map = new LinkedHashMap<>();
|
||||
map.put("任务编码", log.getTaskCode());
|
||||
map.put("任务标识", log.getTaskId());
|
||||
map.put(" vehicleCode", log.getVehicleCode());
|
||||
map.put("备注", log.getRemark());
|
||||
map.put("日志类型", log.getLogType());
|
||||
map.put("方法", log.getMethod());
|
||||
map.put("请求参数", log.getRequestparam());
|
||||
map.put("返回参数", log.getResponseparam());
|
||||
map.put("请求地址", log.getRequesturl());
|
||||
map.put("状态码", log.getStatusCode());
|
||||
map.put("是否删除", log.getIsDelete());
|
||||
map.put("创建者", log.getCreateBy());
|
||||
map.put("创建时间", log.getCreateTime());
|
||||
map.put("修改者", log.getUpdateBy());
|
||||
map.put("修改时间", log.getUpdateTime());
|
||||
list.add(map);
|
||||
}
|
||||
FileUtil.downloadExcel(list, response);
|
||||
}*/
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.nl.acs.utils;
|
||||
|
||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @Author: lyd
|
||||
* @Description: 配置文件获取方法
|
||||
* @Date: 2023/12/6
|
||||
*/
|
||||
public class YmlConfigFileUtil {
|
||||
public static Properties readConfig(String configFile) {
|
||||
// 创建 Resource 对象
|
||||
Resource resource = new ClassPathResource(configFile);
|
||||
|
||||
// 创建 YamlPropertiesFactoryBean
|
||||
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
|
||||
yamlPropertiesFactoryBean.setResources(resource);
|
||||
|
||||
// 获取 Properties 对象
|
||||
Properties properties = yamlPropertiesFactoryBean.getObject();
|
||||
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,10 @@ package org.nl.config.lucene;
|
||||
*/
|
||||
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.yomahub.tlog.core.context.AspectLogContext;
|
||||
import com.yomahub.tlog.core.enhance.logback.async.AspectLogbackAsyncAppender;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import org.nl.config.lucene.remote.RemoteLogServer;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -18,25 +18,24 @@ import java.util.Map;
|
||||
public class AsyncLuceneAppender extends AspectLogbackAsyncAppender {
|
||||
|
||||
|
||||
public AsyncLuceneAppender() {
|
||||
RemoteLogServer.asyncLuceneAppender = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent event) {
|
||||
String traceId = AspectLogContext.getLogValue();
|
||||
if (StringUtils.isEmpty(traceId)){
|
||||
traceId = IdUtil.nanoId()+"@";
|
||||
AspectLogContext.putLogValue(traceId);
|
||||
}else {
|
||||
if (!traceId.contains("@")){
|
||||
AspectLogContext.putLogValue(traceId+"@");
|
||||
}
|
||||
}
|
||||
String traceId = LuceneAppender.traceIdTL.get();
|
||||
if (StringUtils.isNotEmpty(traceId)){
|
||||
MDC.put("traceId",traceId);
|
||||
MDC.put("traceId",traceId);
|
||||
Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();
|
||||
if (mdcPropertyMap.getClass().getName().contains("SynchronizedMap")){
|
||||
mdcPropertyMap.put("traceId",traceId);
|
||||
}
|
||||
MDC.clear();
|
||||
}
|
||||
RemoteLogServer.writeLog(event);
|
||||
}
|
||||
public void appendSync(ILoggingEvent event){
|
||||
super.append(event);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ package org.nl.config.lucene;
|
||||
* @Date: 2023/8/25
|
||||
*/
|
||||
public class LogMessageConstant {
|
||||
/** */
|
||||
public final static String SORT_NAME = "time";
|
||||
/** 级别 */
|
||||
public final static String FIELD_LEVEL = "level";
|
||||
/** 时间 */
|
||||
@@ -38,7 +40,7 @@ public class LogMessageConstant {
|
||||
public final static String COLOR_RESET = "\u001B[0m";
|
||||
/** 背景颜色:黄色 */
|
||||
public final static String BACKGROUND_YELLOW = "\u001B[43m";
|
||||
|
||||
/** 索引路径 */
|
||||
public final static String INDEX_DIR = "D:\\lucene\\index";
|
||||
public final static String INDEX_DIR = "E:\\lucene\\index";
|
||||
|
||||
}
|
||||
|
||||
@@ -8,47 +8,48 @@ package org.nl.config.lucene;
|
||||
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.document.NumericDocValuesField;
|
||||
import org.apache.lucene.document.StringField;
|
||||
import org.apache.lucene.document.*;
|
||||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.index.IndexWriterConfig;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.FSDirectory;
|
||||
import org.nl.system.service.lucene.dto.LuceneLogDto;
|
||||
import org.wltea.analyzer.lucene.IKAnalyzer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
public class LuceneAppender extends AppenderBase<ILoggingEvent> {
|
||||
|
||||
private Directory index;
|
||||
private IndexWriter indexWriter;
|
||||
public static final TransmittableThreadLocal<String> traceIdTL = new TransmittableThreadLocal();
|
||||
public LuceneProperties properties;
|
||||
public static Directory index;
|
||||
private List<LucenePropertyAndEncoder> encoders;
|
||||
public static IndexWriter indexWriter;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
super.start();
|
||||
try {
|
||||
index = FSDirectory.open(Paths.get(LogMessageConstant.INDEX_DIR));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 读取配置文件
|
||||
Properties properties = org.nl.acs.utils.YmlConfigFileUtil.readConfig("config/application.yml");
|
||||
|
||||
// 初始化 Lucene 索引
|
||||
Analyzer analyzer = new IKAnalyzer();
|
||||
IndexWriterConfig config = new IndexWriterConfig(analyzer);
|
||||
try {
|
||||
// 获取配置值
|
||||
String luceneDir = properties.getProperty("lucene.index.path");
|
||||
System.out.println("---index地址----"+luceneDir);
|
||||
index = FSDirectory.open(Paths.get(luceneDir));
|
||||
// 初始化 Lucene 索引
|
||||
Analyzer analyzer = new IKAnalyzer();
|
||||
IndexWriterConfig config = new IndexWriterConfig(analyzer);
|
||||
indexWriter = new IndexWriter(index, config);
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -56,52 +57,27 @@ public class LuceneAppender extends AppenderBase<ILoggingEvent> {
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent event) {
|
||||
String message = event.getFormattedMessage();
|
||||
try {
|
||||
// String[] split = message.split("@");
|
||||
LuceneLogDto luceneLogDto = JSONObject.parseObject(message, LuceneLogDto.class);
|
||||
Document document = new Document();
|
||||
try {
|
||||
//向document对象中添加域。
|
||||
Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();
|
||||
String traceId = mdcPropertyMap.get("traceId");
|
||||
System.out.println("---追踪号---"+traceId);
|
||||
if (ObjectUtil.isNotEmpty(traceId)) {
|
||||
document.add(new StringField("trace_id", traceId, Field.Store.YES));
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getDevice_code())) {
|
||||
document.add(new StringField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES));
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getContent())) {
|
||||
document.add(new StringField("fieldContent", luceneLogDto.getContent(), Field.Store.YES));
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getMethod())) {
|
||||
document.add(new StringField("method", luceneLogDto.getMethod(), Field.Store.YES));
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getStatus_code())) {
|
||||
document.add(new StringField("status_code", luceneLogDto.getStatus_code(), Field.Store.YES));
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getRequestparam())) {
|
||||
document.add(new StringField("requestparam", luceneLogDto.getRequestparam(), Field.Store.YES));
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getResponseparam())) {
|
||||
document.add(new StringField("responseparam", luceneLogDto.getResponseparam(), Field.Store.YES));
|
||||
}
|
||||
document.add(new StringField("logType", luceneLogDto.getLogType(), Field.Store.YES));
|
||||
document.add(new StringField("logTime", DateUtil.format(new DateTime(), "yyyy-MM-dd HH:mm:ss.SSS"), Field.Store.YES));
|
||||
document.add(new NumericDocValuesField("time",System.currentTimeMillis()));//排序
|
||||
Document doc = new Document();
|
||||
for (Property property : this.properties.getProperties()) {
|
||||
LucenePropertyAndEncoder encoder = new LucenePropertyAndEncoder(property, this.context);
|
||||
String encode = encoder.encode(event);
|
||||
doc.add(new StringField(property.getName(), encode, Field.Store.YES));
|
||||
}
|
||||
Map<String, String> map = event.getMDCPropertyMap();
|
||||
if (!map.isEmpty() && StringUtils.isNotEmpty(map.get("traceId"))){
|
||||
doc.add(new StringField("traceId",map.get("traceId"), Field.Store.YES));
|
||||
}else {
|
||||
doc.add(new StringField("traceId"," ", Field.Store.YES));
|
||||
}
|
||||
|
||||
try {
|
||||
indexWriter.addDocument(document);
|
||||
indexWriter.commit();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
} catch (Exception e){
|
||||
return;
|
||||
doc.add(new TextField(LogMessageConstant.FIELD_MESSAGE, event.getFormattedMessage(), Field.Store.YES));
|
||||
doc.add(new StringField(LogMessageConstant.FIELD_TIMESTAMP, String.valueOf(event.getTimeStamp()),Field.Store.YES));
|
||||
doc.add(new NumericDocValuesField(LogMessageConstant.SORT_NAME, event.getTimeStamp()));
|
||||
try {
|
||||
indexWriter.addDocument(doc);
|
||||
indexWriter.commit();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,4 +90,9 @@ public class LuceneAppender extends AppenderBase<ILoggingEvent> {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void setProperties(LuceneProperties properties) {
|
||||
this.properties = properties;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
package org.nl.config.lucene;//package org.nl.config.lucene;
|
||||
//
|
||||
//import cn.hutool.core.date.DateUtil;
|
||||
//import org.apache.lucene.index.CorruptIndexException;
|
||||
//import org.apache.lucene.index.IndexWriter;
|
||||
//import org.apache.lucene.index.IndexWriterConfig;
|
||||
//import org.apache.lucene.store.Directory;
|
||||
//import org.apache.lucene.store.FSDirectory;
|
||||
//import org.wltea.analyzer.lucene.IKAnalyzer;
|
||||
//
|
||||
//import java.io.File;
|
||||
//import java.io.IOException;
|
||||
//import java.text.ParseException;
|
||||
//import java.text.SimpleDateFormat;
|
||||
//import java.util.Date;
|
||||
//
|
||||
//public class LuceneIndexWriter {
|
||||
// private static IndexWriter indexWriter;
|
||||
//
|
||||
// static {
|
||||
// try {
|
||||
// Directory directory = FSDirectory.open(new File(UrlConfig.luceneUrl).toPath());
|
||||
// IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());
|
||||
// indexWriter = new IndexWriter(directory, config);
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// /**当当前线程结束时,自动关闭IndexWriter,使用Runtime对象*/
|
||||
// Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
// try {
|
||||
// closeIndexWriter();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }));
|
||||
// }
|
||||
// /**在线程结束时,自动关闭IndexWriter*/
|
||||
// public static IndexWriter getIndexWriter() {
|
||||
// return indexWriter;
|
||||
// }
|
||||
// /**关闭IndexWriter
|
||||
// * @throws IOException
|
||||
// * @throws CorruptIndexException */
|
||||
// public static void closeIndexWriter() throws Exception {
|
||||
// if(indexWriter != null) {
|
||||
// indexWriter.close();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) throws IOException {
|
||||
// indexWriter.deleteAll();
|
||||
// }
|
||||
//
|
||||
// public static String getDate(String timeString) throws ParseException {
|
||||
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");//时间格式
|
||||
// Date date = sdf.parse(timeString);
|
||||
// timeString = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss.SSS");//格式化后的时间
|
||||
// return timeString;
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.nl.config.lucene;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LuceneProperties {
|
||||
|
||||
private List<Property> properties;
|
||||
|
||||
public LuceneProperties() {
|
||||
this.properties = new ArrayList<Property>();
|
||||
}
|
||||
|
||||
public List<Property> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public void addProperty(Property property) {
|
||||
properties.add(property);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.nl.config.lucene;
|
||||
|
||||
import ch.qos.logback.classic.PatternLayout;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.Context;
|
||||
import ch.qos.logback.core.pattern.PatternLayoutBase;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2023/12/22 18:11
|
||||
*/
|
||||
public class LucenePropertyAndEncoder {
|
||||
|
||||
private Property property;
|
||||
|
||||
private PatternLayoutBase layout = new PatternLayout();
|
||||
|
||||
public LucenePropertyAndEncoder(Property property, Context context) {
|
||||
this.property = property;
|
||||
this.layout.setContext(context);
|
||||
this.layout.setPattern(String.valueOf(property.getValue()));
|
||||
this.layout.setPostCompileProcessor(null);
|
||||
this.layout.start();
|
||||
}
|
||||
|
||||
public String encode(ILoggingEvent event) {
|
||||
return layout.doLayout(event);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return property.getName();
|
||||
}
|
||||
|
||||
public boolean allowEmpty() {
|
||||
return property.isAllowEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.nl.config.lucene;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2023/12/26 15:30
|
||||
*/
|
||||
public class Property {
|
||||
private String name;
|
||||
private String value;
|
||||
private boolean allowEmpty;
|
||||
|
||||
public Property() {
|
||||
}
|
||||
|
||||
public Property(String name, String value, boolean allowEmpty) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.allowEmpty = allowEmpty;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean isAllowEmpty() {
|
||||
return allowEmpty;
|
||||
}
|
||||
|
||||
public void setAllowEmpty(boolean allowEmpty) {
|
||||
this.allowEmpty = allowEmpty;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package org.nl.config.lucene;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @deprecated 设置静态参数初始化
|
||||
*/
|
||||
@Configuration
|
||||
public class StaticConfig {
|
||||
//日志索引目录
|
||||
@Value("${lucene.index.path}")
|
||||
private String luceneDir;
|
||||
|
||||
@Bean
|
||||
public int initStatic() {
|
||||
UrlConfig.setLuceneUrl(luceneDir);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.nl.config.lucene.remote;
|
||||
|
||||
import io.netty.bootstrap.AbstractBootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2024/1/22 10:01
|
||||
*/
|
||||
public abstract class AbstraceServer {
|
||||
|
||||
|
||||
public AbstraceServer(SocketAddress address) {
|
||||
this.address = address;
|
||||
if (channel!=null){
|
||||
doDestroy();
|
||||
}
|
||||
doOpen();
|
||||
doConnect();
|
||||
}
|
||||
|
||||
public AbstractBootstrap server;
|
||||
public SocketAddress address;
|
||||
public Channel channel;
|
||||
|
||||
public abstract void doOpen();
|
||||
|
||||
public void doDestroy(){
|
||||
if (channel!=null){
|
||||
channel.close();
|
||||
}
|
||||
};
|
||||
|
||||
public abstract void doConnect() ;
|
||||
|
||||
public void doDisConnect(){
|
||||
if (channel!=null){
|
||||
channel.close();
|
||||
channel.flush();
|
||||
doConnect();
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package org.nl.config.lucene.remote;
|
||||
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.nl.config.MapOf;
|
||||
import org.nl.config.lucene.AsyncLuceneAppender;
|
||||
import org.nl.config.lucene.remote.impl.ClientServer;
|
||||
import org.nl.config.lucene.remote.impl.RemoteServer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2024/1/22 09:06
|
||||
*/
|
||||
//@Component
|
||||
public class RemoteLogServer implements SmartLifecycle {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
public static volatile Boolean LOCAL_LOG = Boolean.TRUE;
|
||||
|
||||
private Integer port = 20888;
|
||||
|
||||
public static AbstraceServer server;
|
||||
|
||||
public static AsyncLuceneAppender asyncLuceneAppender;
|
||||
|
||||
public static void writeLog(ILoggingEvent event){
|
||||
if (LOCAL_LOG){
|
||||
asyncLuceneAppender.appendSync(event);
|
||||
}else {
|
||||
ByteBuf log = Unpooled.copiedBuffer(JSON.toJSONString(event), CharsetUtil.UTF_8);
|
||||
server.channel.writeAndFlush(log);
|
||||
}
|
||||
};
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void start() {
|
||||
try {
|
||||
String provider = redisTemplate.opsForValue().get("providers");
|
||||
if (StringUtils.isEmpty(provider)){
|
||||
String ip = Inet4Address.getLocalHost().getHostAddress();
|
||||
Map<String,Object> config = MapOf.of("ip", ip, "port", port);
|
||||
redisTemplate.opsForValue().set("provider", JSON.toJSONString(config));
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() ->{
|
||||
System.out.println("------服务关闭-升级从变主-------");
|
||||
server.doDestroy();
|
||||
redisTemplate.delete("providers");
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
}catch (Exception ex){}
|
||||
}));
|
||||
server = new RemoteServer(new InetSocketAddress(ip, port));
|
||||
LOCAL_LOG =Boolean.TRUE;
|
||||
}else {
|
||||
Map<String,String> map = JSONObject.parseObject(provider, HashMap.class);
|
||||
String ip = map.get("ip");
|
||||
Integer port = Integer.valueOf(map.get("port"));
|
||||
server = new ClientServer(new InetSocketAddress(ip, port));
|
||||
LOCAL_LOG = Boolean.FALSE;
|
||||
}
|
||||
}catch (Exception ex){
|
||||
ex.printStackTrace();
|
||||
throw ex;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.nl.config.lucene.remote.coder;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
import org.nl.config.lucene.remote.RemoteLogServer;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2024/1/22 10:24
|
||||
*/
|
||||
public class LogConsumerHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
System.out.println("断开连接---");
|
||||
RemoteLogServer.LOCAL_LOG = Boolean.TRUE;
|
||||
RemoteLogServer.server.doDestroy();
|
||||
//重新建立
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
System.out.println("连接");
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
System.out.println("接收到消息");
|
||||
super.channelRead(ctx, msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
||||
if (evt instanceof IdleStateEvent){
|
||||
IdleStateEvent stateEvent = (IdleStateEvent) evt;
|
||||
System.out.println(stateEvent.state());
|
||||
}
|
||||
super.userEventTriggered(ctx, evt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.nl.config.lucene.remote.coder;
|
||||
|
||||
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import org.nl.config.lucene.remote.RemoteLogServer;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2024/1/22 10:24
|
||||
*/
|
||||
public class LogProviderHandler extends SimpleChannelInboundHandler<String> {
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
System.out.println("断开连接---");
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, String c){
|
||||
LoggingEvent event = JSONObject.parseObject(c, LoggingEvent.class);
|
||||
RemoteLogServer.asyncLuceneAppender.appendSync(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
System.out.println("创建了连接----");
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.nl.config.lucene.remote.impl;
|
||||
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.buffer.PooledByteBufAllocator;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.codec.LengthFieldPrepender;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import io.netty.util.concurrent.Future;
|
||||
import org.nl.config.lucene.remote.AbstraceServer;
|
||||
import org.nl.config.lucene.remote.coder.LogConsumerHandler;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2024/1/22 10:01
|
||||
*/
|
||||
public class ClientServer extends AbstraceServer {
|
||||
|
||||
private static EventLoopGroup group = new NioEventLoopGroup();
|
||||
|
||||
public ClientServer(SocketAddress address) {
|
||||
super(address);
|
||||
}
|
||||
|
||||
// 非阻塞IO线程组
|
||||
@Override
|
||||
public void doOpen() {
|
||||
server = new Bootstrap();
|
||||
server.group(group)
|
||||
.option(ChannelOption.SO_KEEPALIVE, true)
|
||||
.option(ChannelOption.TCP_NODELAY, true)
|
||||
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
|
||||
.channel(NioSocketChannel.class);
|
||||
|
||||
server.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) throws Exception {
|
||||
ch.pipeline()
|
||||
.addLast("client-idle-handler", new IdleStateHandler(500, 0,0 , MILLISECONDS))
|
||||
.addLast(new LengthFieldPrepender(2))
|
||||
.addLast( new LogConsumerHandler());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doDestroy() {
|
||||
super.doDestroy();
|
||||
Future<?> bossGroupShutdownFuture = group.shutdownGracefully();
|
||||
bossGroupShutdownFuture.syncUninterruptibly();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doConnect() {
|
||||
try {
|
||||
ChannelFuture connect = ((Bootstrap) server).connect(address);
|
||||
connect.syncUninterruptibly();
|
||||
channel = connect.channel();
|
||||
} catch (Throwable t) {
|
||||
this.doDestroy();
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package org.nl.config.lucene.remote.impl;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.buffer.PooledByteBufAllocator;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
|
||||
import io.netty.handler.codec.LengthFieldPrepender;
|
||||
import io.netty.handler.codec.string.StringDecoder;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import io.netty.util.concurrent.Future;
|
||||
import org.nl.config.lucene.remote.AbstraceServer;
|
||||
import org.nl.config.lucene.remote.coder.LogProviderHandler;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2024/1/22 10:01
|
||||
*/
|
||||
public class RemoteServer extends AbstraceServer {
|
||||
|
||||
private static EventLoopGroup boss = new NioEventLoopGroup();
|
||||
private static EventLoopGroup worker = new NioEventLoopGroup();
|
||||
|
||||
public RemoteServer(SocketAddress address) {
|
||||
super(address);
|
||||
}
|
||||
|
||||
// 非阻塞IO线程组
|
||||
@Override
|
||||
public void doOpen() {
|
||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||
bootstrap
|
||||
.group(boss, worker)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.option(ChannelOption.SO_REUSEADDR, Boolean.TRUE)
|
||||
.childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE)
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)
|
||||
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
|
||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) throws Exception {
|
||||
ch.pipeline()
|
||||
.addLast("client-idle-handler", new IdleStateHandler(500, 0, 0, MILLISECONDS))
|
||||
.addLast(new LengthFieldBasedFrameDecoder(8089, 0, 2, 0, 2))
|
||||
.addLast(new LengthFieldPrepender(2))
|
||||
.addLast("encode",new StringDecoder())
|
||||
.addLast(new LogProviderHandler());
|
||||
}
|
||||
});
|
||||
server = bootstrap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doDestroy(){
|
||||
Future<?> bossGroupShutdownFuture = boss.shutdownGracefully();
|
||||
Future<?> workerGroupShutdownFuture = worker.shutdownGracefully();
|
||||
bossGroupShutdownFuture.syncUninterruptibly();
|
||||
workerGroupShutdownFuture.syncUninterruptibly();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doConnect() {
|
||||
ChannelFuture future = server.bind(address);
|
||||
boolean ret = future.awaitUninterruptibly(3000, MILLISECONDS);
|
||||
if (ret && future.isSuccess()) {
|
||||
Channel newChannel = future.channel();
|
||||
if (channel != null) {
|
||||
channel.close();
|
||||
channel = newChannel;
|
||||
}
|
||||
} else if (future.cause() != null) {
|
||||
Throwable cause = future.cause();
|
||||
cause.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +1,24 @@
|
||||
package org.nl.system.controller.lucence;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.common.logging.annotation.Log;
|
||||
import org.nl.system.service.lucene.LuceneService;
|
||||
import org.nl.system.service.lucene.dto.LogQuery;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Api(tags = "日志检索")
|
||||
@RequestMapping("/api/lucene")
|
||||
@RequestMapping("/api/esLog")
|
||||
@Slf4j
|
||||
public class LuceneController {
|
||||
|
||||
@@ -32,4 +31,26 @@ public class LuceneController {
|
||||
public ResponseEntity<Object> get(@RequestParam Map whereJson, Pageable page) {
|
||||
return new ResponseEntity<>(luceneService.getAll(whereJson, page), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/query")
|
||||
//("日志查询")
|
||||
public ResponseEntity<Object> query(@RequestBody LogQuery query) {
|
||||
return new ResponseEntity<>(luceneService.query(query), HttpStatus.OK);
|
||||
}
|
||||
@GetMapping("/labels/{type}")
|
||||
//("获取标签")
|
||||
public ResponseEntity<Object> labelsValues(@PathVariable String type) {
|
||||
return new ResponseEntity<>(luceneService.getLabelsValues(type), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@DeleteMapping("/clearLogs")
|
||||
//("清空日志")
|
||||
public ResponseEntity<Object> clearLogs(@RequestBody LogQuery query) {
|
||||
luceneService.clearLogs(query);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.nl.system.service.lucene;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.nl.system.service.lucene.dto.LogQuery;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -21,4 +23,30 @@ public interface LuceneService {
|
||||
* @return Map<String, Object>
|
||||
*/
|
||||
Map<String, Object> getAll(Map whereJson, Pageable page);
|
||||
|
||||
|
||||
/**
|
||||
* 日志查询
|
||||
*
|
||||
* @param logQuery
|
||||
* @return
|
||||
*/
|
||||
JSONObject query(LogQuery logQuery);
|
||||
|
||||
|
||||
/**
|
||||
* 清空日志
|
||||
*/
|
||||
void clearLogs(LogQuery query);
|
||||
|
||||
/**
|
||||
* 获取labels和values树
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
JSONArray getLabelsValues(String type);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.nl.system.service.lucene.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 traceId;
|
||||
private String requestMethod;
|
||||
/**
|
||||
* 日志内容模糊匹配
|
||||
*/
|
||||
private String message;
|
||||
/**
|
||||
* 日志级别
|
||||
*/
|
||||
private String logLevel;
|
||||
/**
|
||||
* 系统标签
|
||||
*/
|
||||
private String system;
|
||||
/**
|
||||
* 是否只查询Http相关请求
|
||||
*/
|
||||
private Boolean isRequest = Boolean.TRUE;
|
||||
/**
|
||||
* 是否过滤wql日志
|
||||
*/
|
||||
private Boolean filterSql = Boolean.TRUE;
|
||||
|
||||
private Integer size = 20;
|
||||
|
||||
private Integer page = 1;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.nl.system.service.lucene.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2023/2/8 4:06 下午
|
||||
*/
|
||||
//@Document(indexName = "#{@ESConfig.index}", type = "mes_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 traceId;
|
||||
private String requestMethod;
|
||||
private String thread;
|
||||
private String system;
|
||||
|
||||
}
|
||||
@@ -1,18 +1,44 @@
|
||||
package org.nl.system.service.lucene.impl;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import cn.hutool.core.date.DateField;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.db.PageResult;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.index.DirectoryReader;
|
||||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queryparser.classic.QueryParser;
|
||||
import org.apache.lucene.search.*;
|
||||
import org.apache.lucene.store.FSDirectory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.nl.common.exception.BadRequestException;
|
||||
import org.nl.config.lucene.LuceneAppender;
|
||||
import org.nl.config.lucene.Searcher;
|
||||
import org.nl.system.service.lucene.LuceneService;
|
||||
import org.nl.system.service.lucene.dto.LogQuery;
|
||||
import org.nl.system.service.lucene.dto.LogRepositoryDTO;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.wltea.analyzer.lucene.IKAnalyzer;
|
||||
|
||||
import java.util.Map;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
@Service
|
||||
@@ -20,56 +46,147 @@ import java.util.Map;
|
||||
@Slf4j
|
||||
public class LuceneServiceImpl implements LuceneService {
|
||||
|
||||
// @Value("${loki.url}")
|
||||
// private String lokiUrl;
|
||||
|
||||
// @Value("${loki.systemName}")
|
||||
// private String systemName;
|
||||
|
||||
//日志索引目录
|
||||
static String[] INFO_LEVEL = new String[]{"DEBUG","INFO","WARN","ERROR"};
|
||||
|
||||
@Value("${lucene.index.path}")
|
||||
private String luceneUrl;
|
||||
private String indexUrl;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public JSONObject query(LogQuery logQuery) {
|
||||
JSONObject res = new JSONObject();
|
||||
PageResult page = new PageResult();
|
||||
page.setPage(logQuery.getPage());
|
||||
page.setPageSize(logQuery.getSize());
|
||||
try {
|
||||
FSDirectory directory = FSDirectory.open(Paths.get(indexUrl));
|
||||
DirectoryReader open = DirectoryReader.open(directory);
|
||||
IndexSearcher searcher = new IndexSearcher(open);
|
||||
// 创建排序对象,需要排序字段SortField,参数:字段的名称、字段的类型、是否反转如果是false,升序。true降序
|
||||
BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
|
||||
//时间范围查询
|
||||
Date startDate = logQuery.getStartTime();
|
||||
Date endDate = logQuery.getEndTime();
|
||||
|
||||
if (startDate == null){
|
||||
Calendar calendar=Calendar.getInstance();
|
||||
calendar.set(1970, 0, 1);
|
||||
startDate = calendar.getTime(); }
|
||||
if (endDate == null){ endDate = new DateTime(); }
|
||||
TermRangeQuery termRangeQuery = new TermRangeQuery(
|
||||
"requestTime",
|
||||
new BytesRef(DateUtil.format(startDate, "yyyy-MM-dd HH:mm:ss.SSS")),
|
||||
new BytesRef(DateUtil.format(endDate, "yyyy-MM-dd HH:mm:ss.SSS")), true, true);
|
||||
booleanQueryBuilder.add(termRangeQuery, BooleanClause.Occur.MUST);
|
||||
|
||||
// 字段之间的与或非关系,MUST表示and,MUST_NOT表示not,SHOULD表示or,有几个fields就必须有几个clauses
|
||||
if (ObjectUtil.isNotEmpty(logQuery.getTraceId())){
|
||||
TermQuery termQuery = new TermQuery(new Term("traceId", logQuery.getTraceId()));
|
||||
booleanQueryBuilder.add(termQuery, BooleanClause.Occur.MUST);
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(logQuery.getLogLevel())){
|
||||
TermQuery termQuery = new TermQuery(new Term("logLevel", logQuery.getLogLevel()));
|
||||
booleanQueryBuilder.add(termQuery, BooleanClause.Occur.MUST);
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(logQuery.getSystem())) {
|
||||
TermQuery termQuery = new TermQuery(new Term("system", logQuery.getSystem()));
|
||||
booleanQueryBuilder.add(termQuery, BooleanClause.Occur.MUST);
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(logQuery.getRequestMethod())) {
|
||||
TermQuery termQuery = new TermQuery(new Term("requestMethod", logQuery.getRequestMethod()));
|
||||
booleanQueryBuilder.add(termQuery, BooleanClause.Occur.MUST);
|
||||
}
|
||||
if (Boolean.TRUE.equals(logQuery.getIsRequest())) {
|
||||
Term traceid = new Term("traceId");
|
||||
TermQuery termQuery = new TermQuery(traceid);
|
||||
booleanQueryBuilder.add(termQuery, BooleanClause.Occur.MUST_NOT);
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(logQuery.getMessage())){
|
||||
//查询解析器
|
||||
QueryParser queryParser = new QueryParser("message", new IKAnalyzer(true));
|
||||
Query query = queryParser.parse(logQuery.getMessage());
|
||||
booleanQueryBuilder.add(query, BooleanClause.Occur.MUST);
|
||||
}
|
||||
|
||||
TopFieldCollector collector = TopFieldCollector.create(new Sort(new SortField("time", SortField.Type.LONG,true)), 20000, 0);
|
||||
searcher.search(booleanQueryBuilder.build(), collector);
|
||||
TopDocs topDocs = collector.topDocs((logQuery.getPage()-1)*logQuery.getSize(), logQuery.getSize());
|
||||
int totalSize = collector.getTotalHits();
|
||||
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
|
||||
|
||||
List<LogRepositoryDTO> list = new ArrayList<>();
|
||||
for (ScoreDoc scoreDoc : scoreDocs) {
|
||||
Document doc = open.document(scoreDoc.doc);
|
||||
LogRepositoryDTO dto = new LogRepositoryDTO();
|
||||
dto.setSystem(doc.get("system"));
|
||||
dto.setTraceId(doc.get("traceId"));
|
||||
dto.setLogger(doc.get("logger"));
|
||||
dto.setLogLevel(doc.get("logLevel"));
|
||||
dto.setMessage(doc.get("message"));
|
||||
dto.setRequestIp(doc.get("requestIp"));
|
||||
dto.setRequestMethod(doc.get("requestMethod"));
|
||||
dto.setRequestTime(doc.get("requestTime"));
|
||||
dto.setThread(doc.get("thread"));
|
||||
list.add(dto);
|
||||
}
|
||||
open.close();
|
||||
directory.close();
|
||||
page.addAll(list);
|
||||
page.setTotal(scoreDocs.length);
|
||||
res.put("total", totalSize);
|
||||
}catch (Exception ex){
|
||||
ex.printStackTrace();
|
||||
}
|
||||
res.put("page",page);
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void clearLogs(LogQuery query) {
|
||||
try {
|
||||
IndexWriter indexWriter = LuceneAppender.indexWriter;
|
||||
if (indexWriter != null){
|
||||
BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
|
||||
DateTime offset = DateUtil.offset(new Date(), DateField.HOUR_OF_DAY, -10);
|
||||
TermRangeQuery termRangeQuery = new TermRangeQuery(
|
||||
"requestTime",null,
|
||||
new BytesRef(DateUtil.format(offset, "yyyy-MM-dd HH:mm:ss.SSS")), true, true);
|
||||
booleanQueryBuilder.add(termRangeQuery,BooleanClause.Occur.MUST);
|
||||
indexWriter.deleteDocuments(termRangeQuery);
|
||||
indexWriter.commit();
|
||||
}
|
||||
}catch (Exception ex){
|
||||
throw new BadRequestException("删除失败:"+ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONArray getLabelsValues(String type) {
|
||||
JSONArray result = new JSONArray();
|
||||
for (String v : INFO_LEVEL) {
|
||||
JSONObject item = new JSONObject();
|
||||
item.put("label", v);
|
||||
item.put("value", v);
|
||||
result.add(item);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取labels和values树
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
// @Override
|
||||
// public JSONArray getLabelsValues() {
|
||||
// JSONArray result = new JSONArray();
|
||||
// // 获取所有标签
|
||||
// String labelString = HttpUtil.get(lokiUrl + "/labels", CharsetUtil.CHARSET_UTF_8);
|
||||
// JSONObject parse = (JSONObject) JSONObject.parse(labelString);
|
||||
// JSONArray labels = parse.getJSONArray("data");
|
||||
// for (int i=0; i<labels.size(); i++) {
|
||||
// // 获取标签下的所有值
|
||||
// String valueString = HttpUtil.get(lokiUrl + "/label/" + labels.getString(i) + "/values", CharsetUtil.CHARSET_UTF_8);
|
||||
// JSONObject parse2 = (JSONObject) JSONObject.parse(valueString);
|
||||
// JSONArray values = parse2.getJSONArray("data");
|
||||
// JSONArray children = new JSONArray();
|
||||
// // 组成树形状态 两级
|
||||
// for (int j=0; j<values.size(); j++) {
|
||||
// JSONObject leaf = new JSONObject();
|
||||
// leaf.put("label", values.getString(j));
|
||||
// leaf.put("value", values.getString(j));
|
||||
// children.add(leaf);
|
||||
// }
|
||||
//
|
||||
// JSONObject node = new JSONObject();
|
||||
// node.put("label", labels.getString(i));
|
||||
// node.put("value", labels.getString(i));
|
||||
// node.put("children", children);
|
||||
// result.add(node);
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getAll(Map whereJson, Pageable page) {
|
||||
JSONObject jo = new JSONObject();
|
||||
try {
|
||||
JSONObject jsonObject = (JSONObject) Searcher.search(luceneUrl, "", whereJson);
|
||||
JSONObject jsonObject = (JSONObject) Searcher.search(indexUrl, "", whereJson);
|
||||
JSONArray array = jsonObject.getJSONArray("content");
|
||||
int totalElements = Integer.parseInt(jsonObject.get("totalElements").toString());
|
||||
jo.put("content", array);
|
||||
@@ -78,7 +195,6 @@ public class LuceneServiceImpl implements LuceneService {
|
||||
log.error("索引查询为空", e);
|
||||
throw new NullPointerException("索引查询为空");
|
||||
}
|
||||
|
||||
return jo;
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,6 @@ https://juejin.cn/post/6844903775631572999
|
||||
<root level="debug">
|
||||
<appender-ref ref="asyncLuceneAppender"/>
|
||||
<appender-ref ref="asyncFileAppender"/>
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</root>
|
||||
<logger name="jdbc" level="ERROR" additivity="true">
|
||||
<appender-ref ref="asyncFileAppender"/>
|
||||
|
||||
Reference in New Issue
Block a user