一、创建日志注解
1、日志类型枚举类
import lombok.Getter;
/**
* 日志类型枚举类
*/
@Getter
public enum LogType {
/**
* 登录
*/
login("LoginLog");
/**
* 实现类名称
*/
private final String implClassName;
LogType(String implClassName) {
this.implClassName = implClassName;
}
}
2、自定义日志注解
import java.lang.annotation.*;
import com.baige.enums.LogType;
/**
* 自定义日志注解
*
* 注解说明:
* @Documented:文档注解
* @Retention(RetentionPolicy.RUNTIME):生命周期注解,注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
* @Target({ ElementType.PARAMETER, ElementType.METHOD }):@Target说明了Annotation所修饰的对象范围,ElementType.TYPE:接口、类、枚举、注解,ElementType.METHOD:方法
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER, ElementType.METHOD })
public @interface Log {
/**
* 登录类型
*/
LogType logType();
}
二、利用AOP实现日志切面统一处理
1、日志实现类创建工厂
import com.baige.enums.LogType;
import com.baige.util.SpringContextHolder;
import org.springframework.context.ApplicationContext;
/**
* 日志实现类创建工厂
*/
public class LogFactory {
public static ApplicationContext context;
public static IUserLog getInstance(LogType logType) {
return (IUserLog) SpringContextHolder.getBean(logType.getImplClassName());
}
}
2、切面类
import lombok.extern.slf4j.Slf4j;
import com.baige.aop.log.IUserLog;
import com.baige.aop.annotation.Log;
import com.baige.aop.log.LogFactory;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
@Slf4j
@Aspect
@Component
public class LogAspect {
/**
* 定义切入点,切入点为com.baige.controller.AopController中的所有函数
* 通过@Pointcut注解声明频繁使用的切点表达式
*/
@Pointcut("execution(public * com.baige.controller.UserController.*(..)))")
public void pointcut() {
}
@Around("pointcut()")
public void doAround(ProceedingJoinPoint pjp) {
try {
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
Log log = methodSignature.getMethod().getAnnotation(Log.class);
IUserLog userLog = LogFactory.getInstance(log.logType());
userLog.addLog(pjp, log);
} catch (Throwable throwable) {
log.info("日志记录异常,不影响正常业务", throwable);
}
}
}
三、创建日志实现类
1、添加日志接口
import com.baige.aop.annotation.Log;
import org.aspectj.lang.ProceedingJoinPoint;
import java.net.UnknownHostException;
public interface IUserLog {
/**
* 添加日志
*/
int addLog(ProceedingJoinPoint pjp, Log log) throws UnknownHostException;
}
2、添加日志接口实现类
import java.util.Date;
import java.net.InetAddress;
import java.net.UnknownHostException;
import lombok.AllArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
import com.baige.domain.User;
import com.baige.domain.UserLog;
import com.baige.aop.log.IUserLog;
import com.baige.mapper.UserMapper;
import com.baige.aop.annotation.Log;
import com.baige.mapper.UserLogMapper;
@AllArgsConstructor
@Component("LoginLog")
public class LoginLog implements IUserLog {
private final UserMapper userMapper;
private final UserLogMapper userLogMapper;
@Override
public int addLog(ProceedingJoinPoint pjp, Log log) throws UnknownHostException {
UserLog userLog = new UserLog();
Object[] params = pjp.getArgs();
if (params.length > 0) {
User param = (User) params[0];
User user = userMapper.getOne(param.getLoginName(), param.getLoginPassword());
if (user != null) {
userLog.setUserId(user.getId());
userLog.setContent("获取用户信息成功");
} else {
userLog.setContent("未获取到用户信息");
}
}
userLog.setIpAddress(InetAddress.getLocalHost().getHostAddress());
userLog.setCreateTime(new Date());
return userLogMapper.insert(userLog);
}
}
四、controller
package com.baige.controller;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.baige.domain.User;
import com.baige.enums.LogType;
import com.baige.aop.annotation.Log;
import com.baige.service.UserService;
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {
private final UserService userService;
@PostMapping("/login")
@Log(logType = LogType.login)
public String login(@RequestBody User params) {
User user = userService.login(params.getLoginName(), params.getLoginPassword());
return user != null ? "登录成功" : "登录失败";
}
}