package com.didispace.aspect;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Aspect
@Component
public class WebLogAspect {
private Logger logger = Logger.getLogger(WebLogAspect.class);
ThreadLocal<Long> startTime = new ThreadLocal();
@Pointcut("execution(public * com .didispace.web..*.*(..))")
public void webLog() {}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
logger.info("URL : " + request.getRequestURL().toString());
logger.info("HTTP_METHOD : " + request.getMethod());
logger.info("IP : " + request.getRemoteAddr());
logger.info("CLASS_METHOD : "+ joinPoint.getSignature().getDeclaringTypeName() + "."+ joinPoint.getSignature().getName());
logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
System.out.println("before");
startTime.set(System.currentTimeMillis());
logger.info("startTime="+startTime.get());
}
@Around("webLog()")
public Object doAround(ProceedingJoinPoint jp) throws Throwable {
// 处理完请求,返回内容
logger.info("RESPONSE : " + jp);
System.out.println("around before...");
Object obj = jp.proceed();
System.out.println("around after...");
System.out.println(System.currentTimeMillis());
return obj;
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
logger.info("RESPONSE : " + ret);
System.out.println("after Returning");
logger.info("startTime="+startTime.get());
logger.info("SPENDTIME:"+(System.currentTimeMillis()-startTime.get()));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Chapter4-2-2.rar (21个子文件)
Chapter4-2-2
pom.xml 2KB
target
test-classes
com
didispace
ApplicationTests.class 1KB
classes
application.properties 21B
log4j.properties 1KB
com
didispace
aspect
WebLogAspect.class 4KB
web
HelloController.class 1KB
Application.class 686B
logs
error.log 0B
all.log 62KB
my.log 9KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.core.resources.prefs 267B
org.eclipse.jdt.core.prefs 243B
src
test
java
com
didispace
ApplicationTests.java 588B
main
resources
application.properties 33B
log4j.properties 1KB
java
com
didispace
aspect
WebLogAspect.java 2KB
Application.java 364B
web
HelloController.java 735B
.project 564B
.classpath 1KB
共 21 条
- 1
资源评论
小皮de梦想
- 粉丝: 18
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功