package com.*.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
/**
* log 说明
*
* @return
*/
String value() default "";
/**
* 是否忽略,比如类上面加的有注解,类中某一个方法不想打印可以设置该属性为 true
*
* @return
*/
boolean ignore() default false;
}
自定义注解类 Log.java
------------------------------------------------------------------------------------------------------
package com.*.config;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
/**
*
*/
@Aspect
@Order(100)
@Component
public class AccessLogInfo {
private static final Logger log = LoggerFactory.getLogger(AccessLogInfo.class);
private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
private static final String STRING_START = "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
private static final String STRING_END = "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n";
@Pointcut("execution(* com.*.app..service.impl..*(..)) || execution(* com.*.app.web.rest..*(..))")
public void serviceLog() {
}
@Around("serviceLog()")
public Object around(ProceedingJoinPoint joinPoint) {
try {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Class<?> targetClass = method.getDeclaringClass();
StringBuffer classAndMethod = new StringBuffer();
Log classAnnotation = targetClass.getAnnotation(Log.class);
Log methodAnnotation = method.getAnnotation(Log.class);
if (classAnnotation != null) {
if (classAnnotation.ignore()) {
return joinPoint.proceed();
}
classAndMethod.append(classAnnotation.value()).append("-");
}
if (methodAnnotation != null) {
if (methodAnnotation.ignore()) {
return joinPoint.proceed();
}
classAndMethod.append(methodAnnotation.value());
}
String target = targetClass.getName() + "#" + method.getName();
String params = "";
List<Object> objects = Arrays.asList(joinPoint.getArgs());
for (Object object : objects) {
if(object instanceof MultipartFile){
MultipartFile file = (MultipartFile)object;
params += file.getOriginalFilename();
}else if(object instanceof MultipartFile[]){
MultipartFile[] files = (MultipartFile[])object;
for (MultipartFile file : files) {
params += file.getOriginalFilename()+"-";
}
}else {
params += object.toString()+",";
}
}
log.info(STRING_START + "{} 开始调用--> {} 参数:{}", classAndMethod.toString(), target, params);
long start = System.currentTimeMillis();
Object result = joinPoint.proceed();
long timeConsuming = System.currentTimeMillis() - start;
log.info("\n{} 调用结束<-- {} 返回值:{} 耗时:{}ms" + STRING_END, classAndMethod.toString(), target, result, timeConsuming);
return result;
} catch (Throwable throwable) {
log.error(throwable.getMessage(), throwable);
}
return null;
}
}
AOP切面 AccessLogInfo.java
下面是转载出处 我自己加了一个文件判断。转载的如果入参是文件的话 会报错,我对入参做了一次判断
ps: 有个BUG 会吞掉别人写的异常 ,建议异常直接抛,不要try catch