首页>代码>基于springboot+semantic-ui+mysql+thymeleaf开发小而美的个人博客网站>/blog/src/main/java/com/lrm/aspect/LogAspect.java
package com.lrm.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.util.Arrays;
/**
* Created by limi on 2017/10/13.
*/
@Aspect
@Component
public class LogAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Pointcut("execution(* com.lrm.web.*.*(..))")
public void log() {}
@Before("log()")
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String url = request.getRequestURL().toString();
String ip = request.getRemoteAddr();
String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
RequestLog requestLog = new RequestLog(url, ip, classMethod, args);
logger.info("Request : {}", requestLog);
}
@After("log()")
public void doAfter() {
// logger.info("--------doAfter--------");
}
@AfterReturning(returning = "result",pointcut = "log()")
public void doAfterRuturn(Object result) {
logger.info("Result : {}", result);
}
private class RequestLog {
private String url;
private String ip;
private String classMethod;
private Object[] args;
public RequestLog(String url, String ip, String classMethod, Object[] args) {
this.url = url;
this.ip = ip;
this.classMethod = classMethod;
this.args = args;
}
@Override
public String toString() {
return "{" +
"url='" + url + '\'' +
", ip='" + ip + '\'' +
", classMethod='" + classMethod + '\'' +
", args=" + Arrays.toString(args) +
'}';
}
}
}
最近下载更多
微信网友_7513956916826112 LV3
5月18日
xuweisong2010 LV28
2024年11月18日
陈小灏 LV18
2024年1月14日
xiongwei11231 LV9
2023年10月10日
lvhongquan LV11
2023年9月20日
meyuso LV9
2023年8月17日
tangyongheng08 LV6
2023年7月24日
monolog LV4
2023年6月18日
mengmeng12323423 LV4
2023年6月18日
* LV2
2023年6月14日
最近浏览更多
dane168 LV10
8月29日
6418368 LV10
8月16日
微信网友_7513956916826112 LV3
5月18日
19297348520
5月18日
暂无贡献等级
zdh3907 LV15
4月30日
1140717565 LV2
4月10日
xs13883100 LV1
4月4日
nightstarshii
1月26日
暂无贡献等级
ma406805131 LV19
2024年12月18日
tiancj LV1
2024年11月25日

