利用Spring
框架中aop
,我们可以实现业务代码与系统级服务进行解耦,例如日志记录、事务及其他安全业务等,可以使得我们的工程更加容易维护、优雅。
可以利用注解实现,也可以利用拦截器(继承HandlerInterceptor接口,实现preHandle方法),这里只介绍注解实现方式。
一、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
二、自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
String value() default "";
}
三、配置切面
将自定义的注解作为切入点,参数是ProceedingJoinPoint
和sysLog
,ProceedingJoinPoint
用来获取当前执行的方法,syslog
用来获取注解里面的值。
@Aspect
@Component
public class SysLogAspect {
@Autowired
private SysLogService sysLogService;
private static Logger logger = LoggerFactory.getLogger(SysLogAspect.class);
@Around("@annotation(sysLog)")
public Object around(ProceedingJoinPoint joinPoint,SysLog sysLog) throws Throwable {
long beginTime = SystemClock.now();
//执行方法
Object result = joinPoint.proceed();
//执行时长(毫秒)
long time = SystemClock.now() - beginTime;
SysLog sysLogEntity = new SysLog();
if(sysLog != null){
//注解上的描述
sysLogEntity.setOperation(sysLog.value());
}
//请求的方法名
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
sysLogEntity.setMethod(className + "." + methodName + "()");
//请求的参数
Object[] args = joinPoint.getArgs();
String params = Json.toJsonString(args[0]);
sysLogEntity.setParams(params);
//设置IP地址
sysLogEntity.setIp(IPHelper.getIpAddr());
//用户名
String username = SecurityUtils.getSysUser().getUsername();
sysLogEntity.setUsername(username);
sysLogEntity.setTime(time);
sysLogEntity.setCreateDate(new Date());
//保存系统日志
sysLogService.save(sysLogEntity);
return result;
}
}
另一种方式切面
@Slf4j
@Aspect
@Component
//@Profile({"dev"}) //只对某个环境打印日志
public class LogAspect {
private static final String LINE_SEPARATOR = System.lineSeparator();
/**
* 以自定义 @PrintlnLog 注解作为切面入口
*/
@Pointcut("@annotation(com.zhangsan.unifiedlog.config.PrintlnLog)")
public void PrintlnLog() {
}
/**
* @param joinPoint
* @description 切面方法入参日志打印
*/
@Before("PrintlnLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String methodDetailDescription = this.getAspectMethodLogDescJP(joinPoint);
log.info("------------------------------- start --------------------------");
/**
* 打印自定义方法描述
*/
log.info("Method detail Description: {}", methodDetailDescription);
/**
* 打印请求入参
*/
log.info("Request Args: {}", JSON.toJSONString(joinPoint.getArgs()));
/**
* 打印请求方式
*/
log.info("Request method: {}", request.getMethod());
/**
* 打印请求 url
*/
log.info("Request URL: {}", request.getRequestURL().toString());
/**
* 打印调用方法全路径以及执行方法
*/
log.info("Request Class and Method: {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
}
/**
* @param proceedingJoinPoint
* @description 切面方法返回结果日志打印
*/
@Around("PrintlnLog()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
String aspectMethodLogDescPJ = getAspectMethodLogDescPJ(proceedingJoinPoint);
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
/**
* 输出结果
*/
log.info("{},Response result : {}", aspectMethodLogDescPJ, JSON.toJSONString(result));
/**
* 方法执行耗时
*/
log.info("Time Consuming: {} ms", System.currentTimeMillis() - startTime);
return result;
}
/**
* @description 切面方法执行后执行
*/
@After("PrintlnLog()")
public void doAfter(JoinPoint joinPoint) throws Throwable {
log.info("------------------------------- End --------------------------" + LINE_SEPARATOR);
}
/**
* @param joinPoint
* @description @PrintlnLog 注解作用的切面方法详细细信息
*/
public String getAspectMethodLogDescJP(JoinPoint joinPoint) throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
return getAspectMethodLogDesc(targetName, methodName, arguments);
}
/**
* @param proceedingJoinPoint
* @description @PrintlnLog 注解作用的切面方法详细细信息
*/
public String getAspectMethodLogDescPJ(ProceedingJoinPoint proceedingJoinPoint) throws Exception {
String targetName = proceedingJoinPoint.getTarget().getClass().getName();
String methodName = proceedingJoinPoint.getSignature().getName();
Object[] arguments = proceedingJoinPoint.getArgs();
return getAspectMethodLogDesc(targetName, methodName, arguments);
}
/**
* @param targetName
* @param methodName
* @param arguments
* @description 自定义注解参数
*/
public String getAspectMethodLogDesc(String targetName, String methodName, Object[] arguments) throws Exception {
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
StringBuilder description = new StringBuilder("");
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
description.append(method.getAnnotation(PrintlnLog.class).description());
break;
}
}
}
return description.toString();
}
}
四、在需要记录日志的方法上,添加注解
@SysLog(value)
@SysLog("获取配置信息")
@GetMapping("/info/{key}")
@ApiOperation(value = "获取配置信息", notes = "获取配置信息")
@ApiImplicitParam(name = "key", value = "参数名")
public ResponseEntity<String> info(@PathVariable("key")String key){
return ResponseEntity.ok(sysConfigService.getValue(key));
}
当操作这个方法时,将会被记录到数据库中,在日志管理中能看到相应操作的内容。
参考文章
springboot 之aop实现日志记录
springboot AOP 实现日志管理
spring boot + AOP 实现日志记录
又被逼着优化代码,这次我干掉了出入参 Log日志