本文是SpringBoot通过AOP实现系统日志记录(三)-Mapper层日志监控及异常拦截,若要实现Service层监控,请点击传送门:
SpringBoot通过AOP实现系统日志记录(二)-Service层日志监控
由于公司业务上的需求,现在需要对整个系统做日志性能监控,方便开发人员快速定位系统瓶颈并方便开发人员去解决问题,相关代码如下:
1、引入依赖
<!-- 引入aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2、MapperMonitor监控注解
/**
* @Description: Mapper监控注解
* @Author: zhangzhixiang
* @CreateDate: 2018/12/09 12:34:56
* @Version 1.0
*/
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MapperMonitor {
String value() default "";
}
3、Mapper层代码
/**
* @Description:业务Mapper接口
* @Author:zhangzhixiang
* @Date:2018/09/08 19:56:31
*/
public interface BusinessDAO {
/**
* 根据条件查询业务数据
*
* @param businessBO
* @return List
* @author zhangzhixiang
* @date 2018/09/29 11:49:56
*/
@MapperMonitor
List<BusinessDO> selectByCondition(BusinessDO businessDO) throws Exception;
}
4、Mapper层日志监控及异常拦截
/**
* @Description:Mapper层异常拦截器
* @Author:zhangzhixiang
* @CreateDate:2018/11/08 11:19:56
* @Version:1.0
*/
@Aspect
@Component
public class MapperLogAspect {
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Pointcut("execution(public * com.cy.ops.*.dal..*(..))")
public void mapperLog() {}
@Around(value = "mapperLog()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
//1、记录执行时间
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed(joinPoint.getArgs());
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
//2、有无日志监控注解,有则输出
String methodName = joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()";
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method targetMethod = methodSignature.getMethod();
if (targetMethod.isAnnotationPresent(MapperMonitor.class)) {
logger.info("**********Method:{}, Start:{}, End:{}, Total:{}ms**********",methodName, dateFormat.format(startTime), dateFormat.format(endTime), totalTime);
}
return result;
}
@AfterThrowing(pointcut = "mapperLog()", throwing = "e")
public void doAfterThrowing(Exception e) throws Exception{
throw new SqlException(e);
}
}
5、自定义SQL异常
package com.czgo.exception;
/**
* 自定义异常类(继承运行时异常)
* @author zhangzhixiang
* @version 2018/11/09
*/
public class SqlException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 错误编码
*/
private String errorCode;
/**
* 消息是否为属性文件中的Key
*/
private boolean propertiesKey = true;
/**
* 构造一个基本异常.
*
* @param cause 异常信息
*
*/
public SqlException(Throwable cause)
{
super(cause);
}
/**
* 构造一个基本异常.
*
* @param message 信息描述
*
*/
public SqlException(String message)
{
super(message);
}
/**
* 构造一个基本异常.
*
* @param errorCode 错误编码
* @param message 信息描述
*
*/
public SqlException(String errorCode, String message)
{
this(errorCode, message, true);
}
/**
* 构造一个基本异常.
*
* @param errorCode 错误编码
* @param message
*
*/
public SqlException(String errorCode, String message, Throwable cause)
{
this(errorCode, message, cause, true);
}
/**
* 构造一个基本异常.
*
* @param errorCode 错误编码
* @param message 信息描述
* @param propertiesKey 消息是否为属性文件中的Key
*
*/
public SqlException(String errorCode, String message, boolean propertiesKey)
{
super(message);
this.setErrorCode(errorCode);
this.setPropertiesKey(propertiesKey);
}
/**
* 构造一个基本异常.
*
* @param errorCode 错误编码
* @param message 信息描述
*
*/
public SqlException(String errorCode, String message, Throwable cause, boolean propertiesKey)
{
super(message, cause);
this.setErrorCode(errorCode);
this.setPropertiesKey(propertiesKey);
}
/**
* 构造一个基本异常.
*
* @param message 信息描述
* @param cause 根异常类(可以存入任何异常)
*
*/
public SqlException(String message, Throwable cause)
{
super(message, cause);
}
public String getErrorCode()
{
return errorCode;
}
public void setErrorCode(String errorCode)
{
this.errorCode = errorCode;
}
public boolean isPropertiesKey()
{
return propertiesKey;
}
public void setPropertiesKey(boolean propertiesKey)
{
this.propertiesKey = propertiesKey;
}
}
全篇文章完全纯手打,如果觉得对您有帮助,记得加关注给好评哟~~