首先定义一个日志的注解类
@Target({
ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperationLogger {
String name() default "";
String description() default "";
}
书写一个切面类
package com.xjr.config.log;
import com.xjr.mvc.system.consts.AuthConsts;
import com.xjr.mvc.system.util.JwtTokenUtil;
import com.xjr.mvc.system.xjr_base_log.Xjr_base_log;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Enumeration;
import java.util.UUID;
@Aspect
@Component
public class LogAspect {
private static final Logger LOGGER = LogManager.getLogger(LogAspect.class);
private static final String LOG_CONTENT = "[类名]:%s,[方法]:%s,[参数]:%s";
private static final String[] METHOD_CONTENT = {
"insert", "delete", "update", "save","select" };
@Autowired
private ISysLogService logService;
@Pointcut("@annotation(com.xjr.config.log.OperationLogger)")
public void logPointCut() {
}
@Before("logPointCut()")
public void before(JoinPoint joinPoint) {
LOGGER.info("前置通知")