All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xfmeet.core.log.aspect.CommonLogAspect Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package com.xfmeet.core.log.aspect;

import com.alibaba.fastjson.JSON;
import com.xfmeet.core.log.annotation.CommonLog;
import com.xfmeet.core.log.common.CommonLogUtils;
import org.aspectj.lang.JoinPoint;
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.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author meetzy
 */
@Aspect
@Component
@Order(Integer.MAX_VALUE-1)
public class CommonLogAspect {


    @Pointcut("@annotation(com.xfmeet.core.log.annotation.CommonLog)")
    public void serviceLog() {
    }

    @Around("serviceLog()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String className = joinPoint.getTarget().getClass().getCanonicalName();
        String methodName = method.getName();
        String level = getLogLevel(method);
        CommonLogUtils.log(level, getLogString(className, methodName, joinPoint.getArgs()));
        Object obj = joinPoint.proceed();
        if (method.getReturnType() != void.class) {
            CommonLogUtils.log(level, getLogString(className, methodName, obj));
        }
        return obj;
    }

    private String getLogString(String className, String methodName, Object arg) {
        return String.format("className:{%s}", className) +
                String.format("-->methodName:{%s}", methodName) +
                String.format("-->params:{%s}", JSON.toJSONString(arg));
    }

    private String getLogLevel(Method method) {
        CommonLog log = method.getAnnotation(CommonLog.class);
        return log.value();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy