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

com.soento.core.acpects.LogAspect Maven / Gradle / Ivy

package com.soento.core.acpects;

import com.soento.core.annotation.Log;
import com.soento.core.support.AbstractLogAspect;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author soento
 */
@Slf4j
@Aspect
@Component
public class LogAspect extends AbstractLogAspect {

    @Pointcut("@annotation(com.soento.core.annotation.Log)")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Class clazz = getClass(joinPoint);
        Method method = getMethod(joinPoint);

        Log l = method.getAnnotation(Log.class);
        String description = "";
        if (l != null) {
            description = l.description();
        }

        logStart(description, clazz.getSimpleName(), method.getName());
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            logRequest(description, args[i], i);
        }
        Long start = System.currentTimeMillis();
        Object result = null;
        try {
            result = joinPoint.proceed();
        } catch (Throwable e) {
            for (int i = 0; i < args.length; i++) {
                String print = convert2String(args[i]);
                if (StringUtils.isNoneBlank(print)) {
                    log.info("{}出现异常,【输入参数】[{}]: {}", description, i, print);
                }
            }
            log.info("{}出现异常,错误信息:{},堆栈信息:", description, e.getMessage(), e);
            throw e;
        }

        Long end = System.currentTimeMillis();
        if (result != null) {
            logResponse(description, result);
        }
        logEnd(description, clazz.getSimpleName(), method.getName(), end - start);
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy