From 16878950bc5bbf96b04fcd013170757806ae7daa Mon Sep 17 00:00:00 2001 From: gengby <858962040@qq.com> Date: Tue, 2 Jan 2024 17:22:47 +0800 Subject: [PATCH] =?UTF-8?q?rev:=E6=9B=BF=E6=8D=A2lucene?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lucene/config/AsyncLuceneAppender.java | 43 ++++++ .../lucene/config/LogMessageConstant.java | 80 +++++++++++ .../modules/lucene/config/LuceneAppender.java | 131 ++++++++++++++++++ .../lucene/config/LuceneProperties.java | 23 +++ .../config/LucenePropertyAndEncoder.java | 38 +++++ .../nl/modules/lucene/config/Property.java | 44 ++++++ 6 files changed, 359 insertions(+) create mode 100644 wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/AsyncLuceneAppender.java create mode 100644 wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LogMessageConstant.java create mode 100644 wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LuceneAppender.java create mode 100644 wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LuceneProperties.java create mode 100644 wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LucenePropertyAndEncoder.java create mode 100644 wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/Property.java diff --git a/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/AsyncLuceneAppender.java b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/AsyncLuceneAppender.java new file mode 100644 index 00000000..5f3565a0 --- /dev/null +++ b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/AsyncLuceneAppender.java @@ -0,0 +1,43 @@ +package org.nl.modules.lucene.config; +/** + * @author ldjun + * @version 1.0 + * @date 2023年08月24日 13:00 + * @desc desc + */ + +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.slf4j.MDC; + +import java.util.Map; + +public class AsyncLuceneAppender extends AspectLogbackAsyncAppender { + + + @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+"@"); + } + } + if (StringUtils.isNotEmpty(traceId)){ + MDC.put("traceId",traceId); + Map mdcPropertyMap = event.getMDCPropertyMap(); + if (mdcPropertyMap.getClass().getName().contains("SynchronizedMap")){ + mdcPropertyMap.put("traceId",traceId); + } + MDC.clear(); + } + super.append(event); + } + +} diff --git a/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LogMessageConstant.java b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LogMessageConstant.java new file mode 100644 index 00000000..57ae26fd --- /dev/null +++ b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LogMessageConstant.java @@ -0,0 +1,80 @@ +package org.nl.modules.lucene.config; + +/** + * @Author: lyd + * @Description: 定义lucene相关常量 + * @Date: 2023/8/25 + */ +public class LogMessageConstant { + /** + * + */ + public final static String SORT_NAME = "time"; + /** + * 级别 + */ + 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 = "E:\\lucene\\index"; + +} diff --git a/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LuceneAppender.java b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LuceneAppender.java new file mode 100644 index 00000000..f1176063 --- /dev/null +++ b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LuceneAppender.java @@ -0,0 +1,131 @@ +package org.nl.modules.lucene.config; +/** + * @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 com.alibaba.ttl.TransmittableThreadLocal; +import org.apache.commons.lang3.StringUtils; +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.modules.lucene.service.dto.LuceneLogDto; +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +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 { + + public static final TransmittableThreadLocal traceIdTL = new TransmittableThreadLocal(); + public LuceneProperties properties; + public static Directory index; + private List encoders; + public static IndexWriter indexWriter; + + + @Override + public void start() { + super.start(); + try { + init(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void init() throws IOException { + Resource resource = new ClassPathResource("config/application.yml"); + YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean(); + yamlPropertiesFactoryBean.setResources(resource); + Properties properties = yamlPropertiesFactoryBean.getObject(); + // 获取配置值 + 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); + } + + + @Override + protected void append(ILoggingEvent event) { + String message = event.getFormattedMessage(); + String[] split = message.split("@"); + LuceneLogDto luceneLogDto = JSONObject.parseObject(split[1], LuceneLogDto.class); + Document document = new Document(); + try { + //向document对象中添加域。 + Map 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("logTime",System.currentTimeMillis()));//排序 + + try { + indexWriter.addDocument(document); + indexWriter.commit(); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (Exception e) { + return; + } + } + + @Override + public void stop() { + super.stop(); + try { + indexWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void setProperties(LuceneProperties properties) { + this.properties = properties; + + } +} diff --git a/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LuceneProperties.java b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LuceneProperties.java new file mode 100644 index 00000000..5df7adef --- /dev/null +++ b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LuceneProperties.java @@ -0,0 +1,23 @@ +package org.nl.modules.lucene.config; + + +import java.util.ArrayList; +import java.util.List; + +public class LuceneProperties { + + private List properties; + + public LuceneProperties() { + this.properties = new ArrayList(); + } + + public List getProperties() { + return properties; + } + + public void addProperty(Property property) { + properties.add(property); + } + +} diff --git a/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LucenePropertyAndEncoder.java b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LucenePropertyAndEncoder.java new file mode 100644 index 00000000..3b075043 --- /dev/null +++ b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/LucenePropertyAndEncoder.java @@ -0,0 +1,38 @@ +package org.nl.modules.lucene.config; + +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(); + } +} + diff --git a/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/Property.java b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/Property.java new file mode 100644 index 00000000..643075ca --- /dev/null +++ b/wcs/nladmin-system/src/main/java/org/nl/modules/lucene/config/Property.java @@ -0,0 +1,44 @@ +package org.nl.modules.lucene.config; + +/* + * @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; + } +}