|
|
@ -12,6 +12,7 @@ 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.lucene.analysis.Analyzer; |
|
|
|
import org.apache.lucene.document.Document; |
|
|
|
import org.apache.lucene.document.Field; |
|
|
@ -22,85 +23,95 @@ 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.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> { |
|
|
|
|
|
|
|
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) { |
|
|
|
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); |
|
|
|
try { |
|
|
|
indexWriter = new IndexWriter(index, config); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
indexWriter = new IndexWriter(index, config); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
protected void append(ILoggingEvent event) { |
|
|
|
String message = event.getFormattedMessage(); |
|
|
|
String[] split = message.split("@"); |
|
|
|
LuceneLogDto luceneLogDto = JSONObject.parseObject(split[0], LuceneLogDto.class); |
|
|
|
Document document = new Document(); |
|
|
|
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对象中添加域。
|
|
|
|
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; |
|
|
|
try { |
|
|
|
indexWriter.addDocument(document); |
|
|
|
indexWriter.commit(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} catch (Exception e){ |
|
|
|
} catch (Exception e) { |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
@ -114,4 +125,9 @@ public class LuceneAppender extends AppenderBase<ILoggingEvent> { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void setProperties(LuceneProperties properties) { |
|
|
|
this.properties = properties; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|