|
| 1 | +package io.ymq.logback.config; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSONObject; |
| 4 | +import com.alibaba.fastjson.serializer.SerializerFeature; |
| 5 | +import io.ymq.logback.config.annotation.*; |
| 6 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 7 | +import org.aspectj.lang.annotation.Around; |
| 8 | +import org.aspectj.lang.annotation.Aspect; |
| 9 | +import org.aspectj.lang.annotation.Pointcut; |
| 10 | +import org.aspectj.lang.reflect.MethodSignature; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | +import org.springframework.stereotype.Component; |
| 14 | + |
| 15 | +import java.lang.reflect.Method; |
| 16 | + |
| 17 | + |
| 18 | +/** |
| 19 | + * Created by junbaor on 2017/5/12. |
| 20 | + */ |
| 21 | +@Aspect |
| 22 | +@Component |
| 23 | +public class LogAspect { |
| 24 | + |
| 25 | + private static final Logger log = LoggerFactory.getLogger(LogAspect.class); |
| 26 | + private static final String dateFormat = "yyyy-MM-dd HH:mm:ss"; |
| 27 | + private static final String STRING_START = "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"; |
| 28 | + private static final String STRING_END = "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"; |
| 29 | + |
| 30 | + @Pointcut("execution(* io.ymq.logback..*(..))") |
| 31 | + public void serviceLog() { |
| 32 | + } |
| 33 | + |
| 34 | + @Around("serviceLog()") |
| 35 | + public Object around(ProceedingJoinPoint joinPoint) { |
| 36 | + try { |
| 37 | + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
| 38 | + Method method = signature.getMethod(); |
| 39 | + Class<?> targetClass = method.getDeclaringClass(); |
| 40 | + |
| 41 | + StringBuffer classAndMethod = new StringBuffer(); |
| 42 | + |
| 43 | + Log classAnnotation = targetClass.getAnnotation(Log.class); |
| 44 | + Log methodAnnotation = method.getAnnotation(Log.class); |
| 45 | + |
| 46 | + if (classAnnotation != null) { |
| 47 | + if (classAnnotation.ignore()) { |
| 48 | + return joinPoint.proceed(); |
| 49 | + } |
| 50 | + classAndMethod.append(classAnnotation.value()).append("-"); |
| 51 | + } |
| 52 | + |
| 53 | + if (methodAnnotation != null) { |
| 54 | + if (methodAnnotation.ignore()) { |
| 55 | + return joinPoint.proceed(); |
| 56 | + } |
| 57 | + classAndMethod.append(methodAnnotation.value()); |
| 58 | + } |
| 59 | + |
| 60 | + String target = targetClass.getName() + "#" + method.getName(); |
| 61 | + String params = JSONObject.toJSONStringWithDateFormat(joinPoint.getArgs(), dateFormat, SerializerFeature.WriteMapNullValue); |
| 62 | + |
| 63 | + log.info(STRING_START + "{} 开始调用--> {} 参数:{}", classAndMethod.toString(), target, params); |
| 64 | + |
| 65 | + long start = System.currentTimeMillis(); |
| 66 | + Object result = joinPoint.proceed(); |
| 67 | + long timeConsuming = System.currentTimeMillis() - start; |
| 68 | + |
| 69 | + log.info("\n{} 调用结束<-- {} 返回值:{} 耗时:{}ms" + STRING_END, classAndMethod.toString(), target, JSONObject.toJSONStringWithDateFormat(result, dateFormat, SerializerFeature.WriteMapNullValue), timeConsuming); |
| 70 | + return result; |
| 71 | + } catch (Throwable throwable) { |
| 72 | + log.error(throwable.getMessage(), throwable); |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments