LogAspect.java
1.55 KB
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
package com.xkl.authorization.aspect;
import lombok.extern.apachecommons.CommonsLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* Created by win7 on 2017/1/14.
*/
@Aspect
@Component
@CommonsLog
public class LogAspect {
/**
* 切面,定位到@ExecuteTimeAnnotation注解
*/
@Pointcut("@annotation(com.xkl.authorization.annotation.LogAnnotation)")
public void logPointCut() {
}
@Before("logPointCut()")
public void doBefore(JoinPoint joinPoint){
StringBuilder sb=new StringBuilder();
Signature signature=joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
Object[] args = joinPoint.getArgs();
sb.append("\n方法:\n").append(method.getDeclaringClass().getSimpleName())
.append("-").append(signature.getName()).append("\n").append("参数:\n");
boolean flag=true;
if(args!=null){
for(Object obj:args){
if(flag){
flag=false;
}else{
sb.append(",");
}
if(obj!=null)
sb.append(obj.toString());
}
}
log.info(sb.toString());
}
}