feature(info-query): 增加sql日志记录
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user