rev:替换lucene

This commit is contained in:
2024-01-02 17:22:47 +08:00
parent d967103c17
commit 16878950bc
6 changed files with 359 additions and 0 deletions

View File

@@ -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<String, String> mdcPropertyMap = event.getMDCPropertyMap();
if (mdcPropertyMap.getClass().getName().contains("SynchronizedMap")){
mdcPropertyMap.put("traceId",traceId);
}
MDC.clear();
}
super.append(event);
}
}

View File

@@ -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";
}

View File

@@ -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<ILoggingEvent> {
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 {
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<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("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;
}
}

View File

@@ -0,0 +1,23 @@
package org.nl.modules.lucene.config;
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);
}
}

View File

@@ -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();
}
}

View File

@@ -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;
}
}