自定义注解,使用Spring AOP实现日志记录
SpringBoot使用切面实现日志记录
首先是自定义日志记录注解
1 2 3 4 5 6 7 8
| import java.lang.annotation.*;
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MyLog { String value() default ""; }
|
然后是实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import com.alibaba.fastjson.JSON; import com.example.entity.SysLog; import com.example.service.SysLogService; import com.example.utils.HttpContextUtils; import com.example.utils.IPUtils; import com.example.utils.ShiroUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.util.Date;
@Aspect @Component public class SysLogAspect {
@Autowired private SysLogService sysLogService;
@Pointcut("@annotation( com.example.aspect.MyLog)") public void logPoinCut() { }
@AfterReturning("logPoinCut()") public void saveSysLog(JoinPoint joinPoint) { System.out.println("切面。。。。。"); SysLog sysLog = new SysLog();
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod();
MyLog myLog = method.getAnnotation(MyLog.class); if (myLog != null) { String value = myLog.value(); sysLog.setOperation(value); }
String className = joinPoint.getTarget().getClass().getName(); String methodName = method.getName(); sysLog.setMethod(className + "." + methodName);
Object[] args = joinPoint.getArgs(); String params = JSON.toJSONString(args); sysLog.setParams(params);
sysLog.setCreateDate(new Date()); sysLog.setUsername(ShiroUtils.getUserEntity().getUsername()); HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); sysLog.setIp(IPUtils.getIpAddr(request));
sysLogService.save(sysLog); } }
|
最后直接在例如Controller中使用即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import com.example.aspect.MyLog; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping;
@Controller @RequestMapping("/sys") public class SysMenuController {
@MyLog(value = "测试") @PostMapping("/test") public String test(@RequestBody String test) { System.out.println(test); return "index"; } }
|
下面是几个Utils
HttpContextUtils:
1 2 3 4 5 6 7 8 9
| import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest;
public class HttpContextUtils { public static HttpServletRequest getHttpServletRequest() { return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); } }
|
IPUtils:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import javax.servlet.http.HttpServletRequest;
public class IPUtils { public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-real-ip"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("x-forwarded-for"); if (ip != null) { ip = ip.split(",")[0].trim(); } } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } }
|
ShiroUtils:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import com.example.entity.User; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession;
public class ShiroUtils {
public static HttpSession getSession(){ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpSession httpSession = request.getSession(); return httpSession; }
public static User getUserEntity(){ return (User)getSession().getAttribute("user"); }
}
|
此为博主副博客,留言请去主博客,转载请注明出处:https://www.baby7blog.com/myBlog/87.html