opt:日志优化
This commit is contained in:
@@ -8,6 +8,8 @@ package org.nl.common.lucene;
|
|||||||
|
|
||||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||||
import ch.qos.logback.core.AppenderBase;
|
import ch.qos.logback.core.AppenderBase;
|
||||||
|
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.lucene.analysis.Analyzer;
|
import org.apache.lucene.analysis.Analyzer;
|
||||||
import org.apache.lucene.document.*;
|
import org.apache.lucene.document.*;
|
||||||
import org.apache.lucene.index.IndexWriter;
|
import org.apache.lucene.index.IndexWriter;
|
||||||
@@ -15,6 +17,7 @@ import org.apache.lucene.index.IndexWriterConfig;
|
|||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.store.FSDirectory;
|
import org.apache.lucene.store.FSDirectory;
|
||||||
import org.nl.common.domain.Property;
|
import org.nl.common.domain.Property;
|
||||||
|
import org.nl.common.utils.IdUtil;
|
||||||
import org.nl.common.utils.YmlConfigFileUtil;
|
import org.nl.common.utils.YmlConfigFileUtil;
|
||||||
import org.wltea.analyzer.lucene.IKAnalyzer;
|
import org.wltea.analyzer.lucene.IKAnalyzer;
|
||||||
|
|
||||||
@@ -24,6 +27,8 @@ import java.util.List;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
public class LuceneAppender extends AppenderBase<ILoggingEvent> {
|
public class LuceneAppender extends AppenderBase<ILoggingEvent> {
|
||||||
|
|
||||||
|
public static final TransmittableThreadLocal<String> traceIdTL = new TransmittableThreadLocal();
|
||||||
public LuceneProperties properties;
|
public LuceneProperties properties;
|
||||||
public static Directory index;
|
public static Directory index;
|
||||||
private List<LucenePropertyAndEncoder> encoders;
|
private List<LucenePropertyAndEncoder> encoders;
|
||||||
@@ -57,8 +62,18 @@ public class LuceneAppender extends AppenderBase<ILoggingEvent> {
|
|||||||
Document doc = new Document();
|
Document doc = new Document();
|
||||||
for (Property property : this.properties.getProperties()) {
|
for (Property property : this.properties.getProperties()) {
|
||||||
LucenePropertyAndEncoder encoder = new LucenePropertyAndEncoder(property, this.context);
|
LucenePropertyAndEncoder encoder = new LucenePropertyAndEncoder(property, this.context);
|
||||||
doc.add(new StringField(property.getName(), encoder.encode(event), Field.Store.YES));
|
String encode = encoder.encode(event);
|
||||||
|
if (property.getName().equals("traceId")){
|
||||||
|
if (StringUtils.isNotEmpty(encode)){
|
||||||
|
LuceneAppender.traceIdTL.set(encode);
|
||||||
|
}else {
|
||||||
|
encode = LuceneAppender.traceIdTL.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
doc.add(new StringField(property.getName(), encode, Field.Store.YES));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc.add(new StringField("traceId",traceIdTL.get(), Field.Store.YES));
|
||||||
doc.add(new TextField(LogMessageConstant.FIELD_MESSAGE, event.getFormattedMessage(), Field.Store.YES));
|
doc.add(new TextField(LogMessageConstant.FIELD_MESSAGE, event.getFormattedMessage(), Field.Store.YES));
|
||||||
doc.add(new StringField(LogMessageConstant.FIELD_TIMESTAMP, String.valueOf(event.getTimeStamp()),Field.Store.YES));
|
doc.add(new StringField(LogMessageConstant.FIELD_TIMESTAMP, String.valueOf(event.getTimeStamp()),Field.Store.YES));
|
||||||
doc.add(new NumericDocValuesField(LogMessageConstant.SORT_NAME, event.getTimeStamp()));
|
doc.add(new NumericDocValuesField(LogMessageConstant.SORT_NAME, event.getTimeStamp()));
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import org.aspectj.lang.annotation.Around;
|
|||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Pointcut;
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
import org.aspectj.lang.reflect.MethodSignature;
|
import org.aspectj.lang.reflect.MethodSignature;
|
||||||
|
import org.nl.common.lucene.LuceneAppender;
|
||||||
import org.nl.common.utils.BaseCode;
|
import org.nl.common.utils.BaseCode;
|
||||||
import org.nl.common.utils.IdUtil;
|
import org.nl.common.utils.IdUtil;
|
||||||
import org.nl.common.utils.SecurityUtils;
|
import org.nl.common.utils.SecurityUtils;
|
||||||
@@ -75,10 +76,9 @@ public class LogAspect {
|
|||||||
HttpServletRequest request = RequestHolder.getHttpServletRequest();
|
HttpServletRequest request = RequestHolder.getHttpServletRequest();
|
||||||
String url = request.getRequestURI();
|
String url = request.getRequestURI();
|
||||||
String requestIp = StringUtils.getIp(request);
|
String requestIp = StringUtils.getIp(request);
|
||||||
|
|
||||||
MDC.put("requestMethod",url);
|
MDC.put("requestMethod",url);
|
||||||
MDC.put("requestIp", StringUtils.getIp(request));
|
MDC.put("requestIp", StringUtils.getIp(request));
|
||||||
MDC.put("traceId", BaseCode.intToChars(IdUtil.getLongId()));
|
MDC.put("traceId",BaseCode.intToChars(IdUtil.getLongId()));
|
||||||
MDC.put("requestTime", DateUtil.now());
|
MDC.put("requestTime", DateUtil.now());
|
||||||
|
|
||||||
Object result = null;
|
Object result = null;
|
||||||
@@ -111,6 +111,7 @@ public class LogAspect {
|
|||||||
}finally {
|
}finally {
|
||||||
log.info("[--response--][请求接口:{} 执行结束][耗时:{}s]",url,(System.currentTimeMillis() - comming)/1000);
|
log.info("[--response--][请求接口:{} 执行结束][耗时:{}s]",url,(System.currentTimeMillis() - comming)/1000);
|
||||||
MDC.clear();
|
MDC.clear();
|
||||||
|
LuceneAppender.traceIdTL.remove();
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package org.nl.modules.logging.rest;
|
package org.nl.modules.logging.rest;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.annotation.SaIgnore;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.nl.common.anno.Log;
|
||||||
import org.nl.modules.logging.service.LuceneLogService;
|
import org.nl.modules.logging.service.LuceneLogService;
|
||||||
import org.nl.modules.logging.service.dto.LogQuery;
|
import org.nl.modules.logging.service.dto.LogQuery;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -16,6 +19,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/esLog")
|
@RequestMapping("/api/esLog")
|
||||||
|
@Slf4j
|
||||||
public class LuceneLogController {
|
public class LuceneLogController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -40,4 +44,23 @@ public class LuceneLogController {
|
|||||||
return new ResponseEntity<>(HttpStatus.OK);
|
return new ResponseEntity<>(HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/thread")
|
||||||
|
@SaIgnore
|
||||||
|
@Log("线程追踪测试")
|
||||||
|
//("清空日志")
|
||||||
|
public ResponseEntity<Object> thread(@RequestBody LogQuery query) {
|
||||||
|
log.info("线程链路测试"+Thread.currentThread().getName());
|
||||||
|
Thread thread = new Thread(()->{
|
||||||
|
try {
|
||||||
|
Thread.sleep(10000);
|
||||||
|
log.info("线程链路测试"+Thread.currentThread().getName());
|
||||||
|
log.error("线程链路测试"+Thread.currentThread().getName());
|
||||||
|
}catch (Exception ex){
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
thread.start();
|
||||||
|
return new ResponseEntity<>(HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.nl.modules.logging.service.impl;
|
package org.nl.modules.logging.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateField;
|
||||||
import cn.hutool.core.date.DateTime;
|
import cn.hutool.core.date.DateTime;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
@@ -11,10 +12,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|||||||
import org.apache.lucene.analysis.Analyzer;
|
import org.apache.lucene.analysis.Analyzer;
|
||||||
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.index.DirectoryReader;
|
import org.apache.lucene.index.*;
|
||||||
import org.apache.lucene.index.IndexReader;
|
|
||||||
import org.apache.lucene.index.Term;
|
|
||||||
import org.apache.lucene.index.Terms;
|
|
||||||
import org.apache.lucene.queryparser.classic.QueryParser;
|
import org.apache.lucene.queryparser.classic.QueryParser;
|
||||||
import org.apache.lucene.search.*;
|
import org.apache.lucene.search.*;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
@@ -22,6 +20,8 @@ import org.apache.lucene.store.FSDirectory;
|
|||||||
import org.apache.lucene.util.BytesRef;
|
import org.apache.lucene.util.BytesRef;
|
||||||
import org.nl.common.lucene.LogMessageConstant;
|
import org.nl.common.lucene.LogMessageConstant;
|
||||||
import org.nl.common.lucene.LuceneAppender;
|
import org.nl.common.lucene.LuceneAppender;
|
||||||
|
import org.nl.common.utils.YmlConfigFileUtil;
|
||||||
|
import org.nl.modules.common.exception.BadRequestException;
|
||||||
import org.nl.modules.logging.service.LuceneLogService;
|
import org.nl.modules.logging.service.LuceneLogService;
|
||||||
import org.nl.modules.logging.service.dto.LogQuery;
|
import org.nl.modules.logging.service.dto.LogQuery;
|
||||||
import org.nl.modules.logging.service.dto.LogRepositoryDTO;
|
import org.nl.modules.logging.service.dto.LogRepositoryDTO;
|
||||||
@@ -43,12 +43,29 @@ public class LuceneLogServiceImpl implements LuceneLogService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearLogs(LogQuery query) {
|
public void clearLogs(LogQuery query) {
|
||||||
|
try {
|
||||||
|
// 初始化 Lucene 索引
|
||||||
|
Analyzer analyzer = new IKAnalyzer();
|
||||||
|
IndexWriterConfig config = new IndexWriterConfig(analyzer);
|
||||||
|
IndexWriter indexWriter = new IndexWriter(FSDirectory.open(Paths.get(indexUrl)), config);
|
||||||
|
if (indexWriter != null){
|
||||||
|
BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
|
||||||
|
DateTime offset = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -10);
|
||||||
|
TermRangeQuery termRangeQuery = new TermRangeQuery(
|
||||||
|
"requestTime",null,
|
||||||
|
new BytesRef(DateUtil.format(offset, "yyyy-MM-dd HH:mm:ss.SSS")), true, true);
|
||||||
|
booleanQueryBuilder.add(termRangeQuery,BooleanClause.Occur.MUST);
|
||||||
|
indexWriter.deleteDocuments(termRangeQuery);
|
||||||
|
indexWriter.commit();
|
||||||
|
indexWriter.close();
|
||||||
|
}
|
||||||
|
}catch (Exception ex){
|
||||||
|
throw new BadRequestException("删除失败:"+ex.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONArray getLabelsValues(String type) {
|
public JSONArray getLabelsValues(String type) {
|
||||||
|
|
||||||
JSONArray result = new JSONArray();
|
JSONArray result = new JSONArray();
|
||||||
for (String v : INFO_LEVEL) {
|
for (String v : INFO_LEVEL) {
|
||||||
JSONObject item = new JSONObject();
|
JSONObject item = new JSONObject();
|
||||||
|
|||||||
@@ -184,4 +184,4 @@ es:
|
|||||||
schedulerFile: /Users/mima0000/Desktop/scheduler.xml
|
schedulerFile: /Users/mima0000/Desktop/scheduler.xml
|
||||||
lucene:
|
lucene:
|
||||||
index:
|
index:
|
||||||
path: /Users/mima0000/Desktop/index
|
path: E:\lms\lucene\index
|
||||||
|
|||||||
@@ -88,7 +88,7 @@
|
|||||||
</springProfile>
|
</springProfile>
|
||||||
|
|
||||||
<springProfile name="dev3">
|
<springProfile name="dev3">
|
||||||
<root level="DEBUG">
|
<root level="info">
|
||||||
<appender-ref ref="CONSOLE"/>
|
<appender-ref ref="CONSOLE"/>
|
||||||
<appender-ref ref="asyncLuceneAppender"/>
|
<appender-ref ref="asyncLuceneAppender"/>
|
||||||
</root>
|
</root>
|
||||||
|
|||||||
Reference in New Issue
Block a user