汪菘
1 year ago
12 changed files with 308 additions and 113 deletions
@ -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"; |
||||
|
} |
@ -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(); |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue