feature(info-query): 增加sql日志记录
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package com.lanyuanxiaoyao.service.configuration.entity.info;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* SQL
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2023-07-11
|
||||
*/
|
||||
public class SQLLine {
|
||||
private String sql;
|
||||
private Long createTime;
|
||||
|
||||
public SQLLine() {
|
||||
}
|
||||
|
||||
public SQLLine(String sql) {
|
||||
this.sql = sql;
|
||||
this.createTime = Instant.now().toEpochMilli();
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
public void setSql(String sql) {
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
public Long getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Long createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SQLLine{" +
|
||||
"sql='" + sql + '\'' +
|
||||
", createTime=" + createTime +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,10 @@
|
||||
<artifactId>database</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.lanyuanxiaoyao.service.info.configuration;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* SQL记录
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2023-07-11
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@EnableAspectJAutoProxy
|
||||
public class SQLLoggerAdvice {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SQLLoggerAdvice.class);
|
||||
private final SQLLoggerProvider.SQLLogger sqlLogger;
|
||||
|
||||
public SQLLoggerAdvice(SQLLoggerProvider.SQLLogger sqlLogger) {
|
||||
this.sqlLogger = sqlLogger;
|
||||
}
|
||||
|
||||
@Around("execution(* org.springframework.jdbc.core.JdbcTemplate.query(String, org.springframework.jdbc.core.ResultSetExtractor))")
|
||||
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
Object[] args = joinPoint.getArgs();
|
||||
if (args != null && args.length > 0 && args[0] instanceof String) {
|
||||
sqlLogger.log((String) args[0]);
|
||||
}
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.lanyuanxiaoyao.service.info.configuration;
|
||||
|
||||
import com.lanyuanxiaoyao.service.configuration.entity.info.SQLLine;
|
||||
import java.time.Instant;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import org.eclipse.collections.api.factory.Lists;
|
||||
import org.eclipse.collections.api.list.ImmutableList;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* SQL记录
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2023-07-11
|
||||
*/
|
||||
@Configuration
|
||||
public class SQLLoggerProvider {
|
||||
public static final class SQLLogger {
|
||||
private final int size;
|
||||
private final Queue<SQLLine> container = new ConcurrentLinkedQueue<>();
|
||||
|
||||
public SQLLogger(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public void log(String sql) {
|
||||
if (container.size() >= size) {
|
||||
container.poll();
|
||||
}
|
||||
container.add(new SQLLine(sql));
|
||||
}
|
||||
|
||||
public ImmutableList<SQLLine> getLogs() {
|
||||
return Lists.immutable.ofAll(container);
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SQLLogger sqlLogger() {
|
||||
return new SQLLogger(200);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.lanyuanxiaoyao.service.info.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.configuration.entity.info.SQLLine;
|
||||
import com.lanyuanxiaoyao.service.info.configuration.SQLLoggerProvider;
|
||||
import org.eclipse.collections.api.list.ImmutableList;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Sql 日志
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2023-07-11
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("logs")
|
||||
public class LogController {
|
||||
private static final Logger logger = LoggerFactory.getLogger(LogController.class);
|
||||
|
||||
private final SQLLoggerProvider.SQLLogger sqlLogger;
|
||||
|
||||
public LogController(SQLLoggerProvider.SQLLogger sqlLogger) {
|
||||
this.sqlLogger = sqlLogger;
|
||||
}
|
||||
|
||||
@GetMapping("")
|
||||
public ImmutableList<SQLLine> logs() {
|
||||
return sqlLogger.getLogs();
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@ package com.lanyuanxiaoyao.service.web.controller;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import com.lanyuanxiaoyao.service.configuration.entity.loki.LokiLogLine;
|
||||
import com.lanyuanxiaoyao.service.forest.service.InfoService;
|
||||
import com.lanyuanxiaoyao.service.forest.service.LokiService;
|
||||
import com.lanyuanxiaoyao.service.web.controller.base.AmisCrudResponse;
|
||||
import com.lanyuanxiaoyao.service.web.controller.base.AmisDetailResponse;
|
||||
import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse;
|
||||
import com.lanyuanxiaoyao.service.web.controller.base.BaseController;
|
||||
@@ -25,10 +27,12 @@ public class LogController extends BaseController {
|
||||
private static final Logger logger = LoggerFactory.getLogger(LogController.class);
|
||||
|
||||
private final LokiService lokiService;
|
||||
private final InfoService infoService;
|
||||
|
||||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
||||
public LogController(LokiService lokiService) {
|
||||
public LogController(LokiService lokiService, InfoService infoService) {
|
||||
this.lokiService = lokiService;
|
||||
this.infoService = infoService;
|
||||
}
|
||||
|
||||
private String preHandle(String line) {
|
||||
@@ -74,4 +78,9 @@ public class LogController extends BaseController {
|
||||
.collect(this::preHandle)
|
||||
.makeString("\n"));
|
||||
}
|
||||
|
||||
@GetMapping("query_sql_log")
|
||||
public AmisCrudResponse querySQLLog() {
|
||||
return AmisResponse.responseCrudData(infoService.sqlLogs());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user