Browse Source

rev 更新lucene打印

master
汪菘 1 year ago
parent
commit
add93b3693
  1. 2
      acs/nladmin-system/src/main/java/org/nl/acs/device_driver/driver/OpcDeviceDriver.java
  2. 11
      acs/nladmin-system/src/main/java/org/nl/acs/device_driver/lnsh/lnsh_palletizing_manipulator_site/LnshPalletizingManipulatorSiteDeviceDriver.java
  3. 6
      acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/LmsUtil.java
  4. 49
      acs/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java
  5. 1
      acs/nladmin-system/src/main/java/org/nl/acs/opc/OpcItemDto.java
  6. 44
      acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LogMessageConstant.java
  7. 123
      acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneAppender.java
  8. 79
      acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneIndexWriter.java
  9. 3
      acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/Searcher.java
  10. 81
      acs/nladmin-system/src/main/java/org/nl/modules/lucene/service/impl/LuceneExecuteLogServiceImpl.java
  11. 9
      acs/nladmin-system/src/main/resources/log/Lucene.xml
  12. 13
      acs/nladmin-system/src/main/resources/logback-spring.xml

2
acs/nladmin-system/src/main/java/org/nl/acs/device_driver/driver/OpcDeviceDriver.java

@ -24,7 +24,7 @@ public interface OpcDeviceDriver extends DeviceDriver {
//设备扩展表【acs_device_extra】
WQLObject extraTab = WQLObject.getWQLObject("acs_device_extra");
JSONArray arr = extraTab.query("filed_type='02' and device_id = '" + this.getDevice().getDevice_id() + "'").getResultJSONArray(0);
JSONArray arr = extraTab.query(" (filed_type='02' or filed_type='03' )and device_id = '" + this.getDevice().getDevice_id() + "'").getResultJSONArray(0);
for (int i = 0; i < arr.size(); i++) {
JSONObject json = arr.getJSONObject(i);
OpcItemDto dto = new OpcItemDto();

11
acs/nladmin-system/src/main/java/org/nl/acs/device_driver/lnsh/lnsh_palletizing_manipulator_site/LnshPalletizingManipulatorSiteDeviceDriver.java

@ -37,6 +37,7 @@ import org.nl.acs.opc.DeviceAppService;
import org.nl.acs.order.service.ProduceshiftorderService;
import org.nl.acs.route.service.RouteLineService;
import org.nl.acs.task.service.TaskService;
import org.nl.modules.lucene.enums.LogTypeEnum;
import org.nl.modules.lucene.service.LuceneExecuteLogService;
import org.nl.modules.lucene.service.dto.LuceneLogDto;
import org.nl.modules.wql.util.SpringContextHolder;
@ -898,8 +899,15 @@ public class LnshPalletizingManipulatorSiteDeviceDriver extends AbstractOpcDevic
ApplyTaskResponse resp = JSON.toJavaObject(JSONObject.parseObject(acsToWmsService.applyTask(request)),ApplyTaskResponse.class);
message = RequestMethodEnum.getName("apply_take_full_vehicle") + "apply_take_full_vehicle 接口请求LMS...";
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message + "参数:" + JSON.toJSONString(request)));
LuceneLogDto dto = new LuceneLogDto();
dto.setDevice_code(this.device_code);
dto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc());
dto.setRemark(JSON.toJSONString(request));
lucene.deviceExecuteLog(dto);
// lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message + "参数:" + JSON.toJSONString(request)));
// log.info("{}",JSON.toJSONString(dto));
// log.info("{}",JSON.toJSONString(new LuceneLogDto(this.device_code, message + "参数:" + JSON.toJSONString(request))));
if (resp.getCode() == 200) {
this.writing(200);
this.setRequireSucess(true);
@ -1200,7 +1208,6 @@ public class LnshPalletizingManipulatorSiteDeviceDriver extends AbstractOpcDevic
ReadUtil.write(itemMap, server);
ReadUtil.write(itemMap, server);
server.disconnect();
logServer.deviceExecuteLog(this.device_code, "", "", "to_command 写入 " + command);
lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, "to_command 写入 " + command));
}

6
acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/LmsUtil.java

@ -35,19 +35,19 @@ public class LmsUtil {
}
String liKu_wcs_url = paramService.findByCode("wmsurl").getValue();
try {
log.info("请求LMS参数:{}", JSON.toJSONString(requestParam));
// log.info("请求LMS参数:{}", JSON.toJSONString(requestParam));
String body = HttpRequest
.post(liKu_wcs_url + api).setConnectionTimeout(3000)
.body(JSON.toJSONString(requestParam))
.execute()
.body();
log.info("请求LMS参数返回参数:{}", body);
// log.info("请求LMS参数返回参数:{}", body);
return body;
} catch (Exception e) {
JSONObject result = new JSONObject();
result.put("code", "500");
result.put("message", e.getMessage());
log.info("请求LMS异常返回参数:{}", String.valueOf(result));
// log.info("请求LMS异常返回参数:{}", String.valueOf(result));
return String.valueOf(result);
}
}

49
acs/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java

@ -3,10 +3,12 @@ package org.nl.acs.opc;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.udw.UnifiedDataAccessor;
import org.nl.acs.udw.UnifiedDataAccessorFactory;
import org.nl.acs.udw.UnifiedDataAppService;
import org.nl.modules.lucene.enums.LogTypeEnum;
import org.nl.modules.lucene.service.LuceneExecuteLogService;
import org.nl.modules.lucene.service.dto.LuceneLogDto;
import org.nl.modules.wql.util.SpringContextHolder;
@ -118,7 +120,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
try {
Item item = group.addItem(string);
itemsMap.put(string, item);
log.trace("添加成功 {}", string);
// log.trace("添加成功 {}", string);
} catch (Exception var26) {
err_message.append(string + ":" + var26.getMessage());
if (!is_error) {
@ -130,7 +132,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
String tag;
if (is_error) {
tag = err_message.toString();
log.warn("{}:{}", OpcConfig.resource_code, tag);
// log.warn("{}:{}", OpcConfig.resource_code, tag);
}
if (!OpcStartTag.is_run) {
@ -152,18 +154,18 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
while (DeviceOpcSynchronizeAutoRun.isRun) {
long begin = System.currentTimeMillis();
if (log.isTraceEnabled()) {
log.trace("{} 开始记时{}", tag, DateUtil.now());
// log.trace("{} 开始记时{}", tag, DateUtil.now());
}
Map<Item, ItemState> itemStatus = group.read(true, (Item[]) itemsMap.values().toArray(new Item[0]));
long end = System.currentTimeMillis();
long duration = end - begin;
if (log.isTraceEnabled()) {
log.trace("{} 读取耗时:{}", tag, duration);
// log.trace("{} 读取耗时:{}", tag, duration);
}
if (duration > 1000L) {
if (!time_out) {
log.warn("{} 读取超时 : {}", tag, duration);
// log.warn("{} 读取超时 : {}", tag, duration);
}
time_out = true;
@ -186,7 +188,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
String itemId = item.getId();
Object his = accessor_value.getValue(itemId);
if (!ObjectUtl.isEquals(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && his != null) {
log.warn("opc 值不健康 item: {}, 状态: {}", itemId, itemState.getQuality());
// log.warn("opc 值不健康 item: {}, 状态: {}", itemId, itemState.getQuality());
}
if (!UnifiedDataAppService.isEquals(value, his)) {
@ -225,18 +227,17 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
log.warn(tag + "重新创建server");
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}s", tag, all_null, 30000 + random);
}
// ThreadUtl.sleep((long) (30000 + random));
ThreadUtl.sleep((long) ((new Random()).nextInt(3) + 1) * 1000);
break start;
} else if (this.all_null < 12) {
if (log.isWarnEnabled()) {
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, '\uea60' + random);
// log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, '\uea60' + random);
}
ThreadUtl.sleep((long) ('\uea60' + random));
} else {
if (log.isWarnEnabled()) {
log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, 120000 + random);
// log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, 120000 + random);
}
ThreadUtl.sleep((long) (120000 + random));
@ -351,7 +352,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
this.server = null;
}
log.warn("opc server {} {}", this.getOpcGroupID(), connected ? "connected" : "disconnected");
// log.warn("opc server {} {}", this.getOpcGroupID(), connected ? "connected" : "disconnected");
}
private String getOpcGroupID() {
@ -381,7 +382,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
// } else if (log.isInfoEnabled()) {
// log.info("Item {} new value: {}, Timestamp: {}, Quality: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime(), itemState.getQuality()});
// }
log.trace("Item {} new value: {}, Timestamp: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime()});
// log.trace("Item {} new value: {}, Timestamp: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime()});
OpcItemDto itemDto = this.getItem(itemId);
// if (Boolean.TRUE.equals(itemDto.getNeed_log())) {
@ -397,6 +398,8 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
private void logItemChanged(String itemId, UnifiedDataAccessor accessor_value, Object value, OpcItemDto itemDto) {
Object his = accessor_value.getValue(itemId);
itemDto.setHis_item_value(his);
itemDto.setItem_value(value);
List<String> relate_items = itemDto.getRelate_items();
if (relate_items != null && !relate_items.isEmpty()) {
StringBuilder sb = new StringBuilder();
@ -407,24 +410,22 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC
Object obj = accessor_value.getValue(relate);
sb.append("key:" + relate + "value:" + obj + ";");
}
log.info("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value, sb});
// 存在上次点位值为null情况 则不记录日志
if (!itemDto.getItem_code().endsWith("heartbeat") && !itemDto.getItem_code().endsWith("time")) {
if((ObjectUtil.equal(his,"null") || ObjectUtil.isEmpty(his)) || (ObjectUtil.equal(value,"null") || ObjectUtil.isEmpty(value)) ){
//lucene.deviceExecuteLog(new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), String.valueOf(his), String.valueOf(value)));
} else {
lucene.deviceExecuteLog(new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), String.valueOf(his), String.valueOf(value)));
}
// 存在上次点位值为null情况 则不记录日志
LuceneLogDto luceneLogDto = new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1),
String.valueOf(itemDto.getHis_item_value()), String.valueOf(itemDto.getItem_value()));
luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc());
log.info("{}", JSON.toJSONString(luceneLogDto));
}
} else {
log.info("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value});
// log.info("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value});
// 存在上次点位值为null情况 则不记录日志
if (!itemDto.getItem_code().endsWith("heartbeat") && !itemDto.getItem_code().endsWith("time")) {
if((ObjectUtil.equal(his,"null") || ObjectUtil.isEmpty(his)) || (ObjectUtil.equal(value,"null") || ObjectUtil.isEmpty(value)) ){
//lucene.deviceExecuteLog(new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), String.valueOf(his), String.valueOf(value)));
} else {
lucene.deviceExecuteLog(new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), String.valueOf(his), String.valueOf(value)));
}
LuceneLogDto luceneLogDto = new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1),
String.valueOf(itemDto.getHis_item_value()), String.valueOf(itemDto.getItem_value()));
luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc());
log.info("{}", JSON.toJSONString(luceneLogDto));
}
}
}

1
acs/nladmin-system/src/main/java/org/nl/acs/opc/OpcItemDto.java

@ -14,6 +14,7 @@ public class OpcItemDto {
private String opc_plc_code;
private String item_code;
private Object item_value;
private Object his_item_value;
private Boolean need_log = Boolean.valueOf(false);
private List<String> relate_items = new ArrayList();

44
acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LogMessageConstant.java

@ -0,0 +1,44 @@
package org.nl.modules.lucene.common;
/**
* @Author: lyd
* @Description: 定义lucene相关常量
* @Date: 2023/8/25
*/
public class LogMessageConstant {
/** 级别 */
public final static String FIELD_LEVEL = "level";
/** 时间 */
public final static String FIELD_TIMESTAMP = "timestamp";
/** 类的限定名 */
public final static String FIELD_CLASS_NAME = "logger";
/** 线程名 */
public final static String FIELD_THREAD = "thread";
/** 日志内容 */
public final static String FIELD_MESSAGE = "message";
public final static String FIELD_TRACEID = "tlogTraceId";
// 定义颜色值
/** 文本颜色:黑色 */
public final static String COLOR_BLACK = "\u001B[30m";
/** 文本颜色:红色 */
public final static String COLOR_RED = "\u001B[31m";
/** 文本颜色:绿色 */
public final static String COLOR_GREEN = "\u001B[32m";
/** 文本颜色:黄色 */
public final static String COLOR_YELLOW = "\u001B[33m";
/** 文本颜色:蓝色 */
public final static String COLOR_BLUE = "\u001B[34m";
/** 文本颜色:品红色 */
public final static String COLOR_MAGENTA = "\u001B[35m";
/** 文本颜色:青色 */
public final static String COLOR_CYAN = "\u001B[36m";
/** 文本颜色:白色 */
public final static String COLOR_WHITE = "\u001B[37m";
/** 文本颜色重置 */
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";
}

123
acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneAppender.java

@ -0,0 +1,123 @@
package org.nl.modules.lucene.common;
/**
* @author ldjun
* @version 1.0
* @date 2023年08月24日 13:00
* @desc desc
*/
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 org.apache.lucene.analysis.Analyzer;
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.acs.opc.OpcItemDto;
import org.nl.modules.lucene.enums.LogTypeEnum;
import org.nl.modules.lucene.service.LuceneExecuteLogService;
import org.nl.modules.lucene.service.dto.LuceneLogDto;
import org.nl.modules.wql.util.SpringContextHolder;
import org.wltea.analyzer.lucene.IKAnalyzer;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Map;
public class LuceneAppender extends AppenderBase<ILoggingEvent> {
private Directory index;
private IndexWriter indexWriter;
@Override
public void start() {
super.start();
try {
index = FSDirectory.open(Paths.get(LogMessageConstant.INDEX_DIR));
} catch (IOException e) {
e.printStackTrace();
}
// 初始化 Lucene 索引
Analyzer analyzer = new IKAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
try {
indexWriter = new IndexWriter(index, config);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void append(ILoggingEvent event) {
String message = event.getFormattedMessage();
try {
LuceneLogDto luceneLogDto = JSONObject.parseObject(message, LuceneLogDto.class);
// LuceneLogDto luceneLogDto = new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1),
// String.valueOf(itemDto.getHis_item_value()), String.valueOf(itemDto.getItem_value()));
// luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc());
//IndexWriter indexWriter = LuceneIndexWriter.getIndexWriter();
//创建一个Document对象
Document document = new Document();
try {
//记录索引开始时间
long startTime = System.currentTimeMillis();
//向document对象中添加域。
if (ObjectUtil.isNotEmpty(luceneLogDto.getDevice_code())) {
document.add(new StringField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES));
// document.add(new TextField("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("logTime",System.currentTimeMillis()));//排序
//记录索引结束时间
long endTime = System.currentTimeMillis();
// log.info("建立索引共耗时{}毫秒", endTime - startTime);
try {
indexWriter.addDocument(document);
indexWriter.commit();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
return;
}
} catch (Exception e){
return;
}
}
@Override
public void stop() {
super.stop();
try {
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

79
acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneIndexWriter.java

@ -16,45 +16,45 @@ 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(){
@Override
public void run() {
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();
}
// 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(){
// @Override
// public void run() {
// 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);
@ -62,5 +62,4 @@ public class LuceneIndexWriter {
return timeString;
}
}

3
acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/Searcher.java

@ -36,6 +36,7 @@ public class Searcher {
//构建IndexSearcher
IndexSearcher searcher = new IndexSearcher(reader);
//标准分词器,会自动去掉空格啊,is a the等单词
Analyzer analyzer = new IKAnalyzer(true);
// Analyzer analyzer = new StandardAnalyzer();
// Analyzer analyzer = new IKAnalyzer(false);
//查询解析器
@ -49,7 +50,7 @@ public class Searcher {
int start = pageNum * pageSize;// 当前页的起始条数
int end = start + pageSize;// 当前页的结束条数(不能包含)
// 创建排序对象,需要排序字段SortField,参数:字段的名称、字段的类型、是否反转如果是false,升序。true降序
Sort sort = new Sort(new SortField("logTime", SortField.Type.DOC,true));
Sort sort = new Sort(new SortField("logTime", SortField.Type.LONG,true));
TopDocs docs = null;
BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();

81
acs/nladmin-system/src/main/java/org/nl/modules/lucene/service/impl/LuceneExecuteLogServiceImpl.java

@ -3,6 +3,7 @@ package org.nl.modules.lucene.service.impl;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@ -35,57 +36,57 @@ public class LuceneExecuteLogServiceImpl implements LuceneExecuteLogService {
String now = DateUtil.now();
}
@SneakyThrows
@Override
public void deviceExecuteLog(LuceneLogDto luceneLogDto) {
luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc());
addIndex(luceneLogDto);
log.info("{}", JSON.toJSONString(luceneLogDto));
}
@Override
public void interfaceExecuteLog(LuceneLogDto luceneLogDto) {
luceneLogDto.setLogType(LogTypeEnum.INTERFACE_LOG.getDesc());
addIndex(luceneLogDto);
// addIndex(luceneLogDto);
log.info("{}", JSON.toJSONString(luceneLogDto));
}
private void addIndex(LuceneLogDto luceneLogDto) {
IndexWriter indexWriter = LuceneIndexWriter.getIndexWriter();
//创建一个Document对象
Document document = new Document();
try {
//记录索引开始时间
long startTime = System.currentTimeMillis();
//向document对象中添加域。
if (ObjectUtil.isNotEmpty(luceneLogDto.getDevice_code())) {
document.add(new StringField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES));
// document.add(new TextField("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));
indexWriter.addDocument(document);
//记录索引结束时间
long endTime = System.currentTimeMillis();
log.info("建立索引共耗时{}毫秒", endTime - startTime);
indexWriter.commit();
MDC.put("DEVICECODE", luceneLogDto.getDevice_code());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
// IndexWriter indexWriter = LuceneIndexWriter.getIndexWriter();
// //创建一个Document对象
// Document document = new Document();
// try {
// //记录索引开始时间
// long startTime = System.currentTimeMillis();
// //向document对象中添加域。
// if (ObjectUtil.isNotEmpty(luceneLogDto.getDevice_code())) {
// document.add(new StringField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES));
//// document.add(new TextField("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));
// indexWriter.addDocument(document);
// //记录索引结束时间
// long endTime = System.currentTimeMillis();
// log.info("建立索引共耗时{}毫秒", endTime - startTime);
// indexWriter.commit();
// MDC.put("DEVICECODE", luceneLogDto.getDevice_code());
// } catch (Exception e) {
// log.error(e.getMessage(), e);
// }
}
@Override

9
acs/nladmin-system/src/main/resources/log/Lucene.xml

@ -23,12 +23,19 @@
</appender>
<appender name="luceneAppender" class="org.nl.modules.lucene.common.LuceneAppender" />
<appender name="asyncLuceneAppender" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="luceneAppender" />
<!-- 设置队列大小,根据您的需求调整 -->
<queueSize>512</queueSize>
</appender>
<!-- <logger name="org.nl.start.Init" level="info" additivity="false">
<appender-ref ref="FILE3"/>
</logger>-->
<!-- 打印sql -->
<logger name="org.nl.modules.lucene.service.impl.LuceneExecuteLogServiceImpl" level="info" additivity="false">
<logger name="org.nl.modules.lucene.service.impl.LuceneExecuteLogServiceImpl" level="info" additivity="true">
<appender-ref ref="FILE_LUCENE"/>
<appender-ref ref="asyncLuceneAppender"/>
</logger>
</included>

13
acs/nladmin-system/src/main/resources/logback-spring.xml

@ -57,10 +57,17 @@ https://juejin.cn/post/6844903775631572999
<!--异步到文件-->
<appender name="asyncFileAppender" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>500</queueSize>
<queueSize>200</queueSize>
<appender-ref ref="FILE"/>
</appender>
<appender name="luceneAppender" class="org.nl.modules.lucene.common.LuceneAppender" />
<appender name="asyncLuceneAppender" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="luceneAppender" />
<!-- 设置队列大小,根据您的需求调整 -->
<queueSize>512</queueSize>
</appender>
<!--添加loki-->
<appender name="lokiAppender" class="com.github.loki4j.logback.Loki4jAppender">
<batchTimeoutMs>1000</batchTimeoutMs>
@ -85,10 +92,12 @@ https://juejin.cn/post/6844903775631572999
<appender-ref ref="CONSOLE"/>
<appender-ref ref="lokiAppender"/>
<appender-ref ref="asyncFileAppender"/>
<appender-ref ref="asyncLuceneAppender"/>
</root>
<!--logmanage -->
<logger name="org.nl.acs.log.service.impl.DeviceExecuteLogServiceImpl" level="info" additivity="false">
<appender-ref ref="lokiAppender" />
<appender-ref ref="asyncLuceneAppender"/>
</logger>
<logger name="jdbc.resultsettable" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE"/>
@ -137,10 +146,12 @@ https://juejin.cn/post/6844903775631572999
<root level="info">
<appender-ref ref="asyncFileAppender"/>
<appender-ref ref="lokiAppender" />
<appender-ref ref="asyncLuceneAppender"/>
</root>
<logger name="org.nl.acs.log.service.impl.DeviceExecuteLogServiceImpl" level="info" additivity="false">
<appender-ref ref="asyncFileAppender"/>
<appender-ref ref="lokiAppender" />
<appender-ref ref="asyncLuceneAppender"/>
</logger>
<logger name="jdbc.resultsettable" level="ERROR" additivity="false">
<appender-ref ref="asyncFileAppender"/>

Loading…
Cancel
Save