AOP統(tǒng)一輸出Controller的日志信息

1.Log輸出 slf4j

package com.followme.common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.Util;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
 * Created by  2019-04-20.
 */
public class LogUtil { 
    private static final Map<Class<?>,Logger> loggers = new ConcurrentHashMap<>();
// info 級別日志
    public static void info(String message, Object... objects){
        Class<?> callingClass = Util.getCallingClass();
        Logger logger =loggers.computeIfAbsent(callingClass, LoggerFactory::getLogger);
        List<Object> objectList =  Arrays.stream(objects).collect(Collectors.toList());
        Object[] objectArr = new Object[objectList.size()];
        logger.info(message,objectList.toArray(objectArr));
    }
// debug 級別日志
    public static void debug(String message, Object... objects){
        Class<?> callingClass = Util.getCallingClass();
        Logger logger =loggers.computeIfAbsent(callingClass, LoggerFactory::getLogger);
          List<Object> objectList =  Arrays.stream(objects).collect(Collectors.toList());
        Object[] objectArr = new Object[objectList.size()];
        logger.debug(message,objectList.toArray(objectArr));
    }
// error 級別日志
    public static void error(String message, Object... objects){
        Class<?> callingClass = Util.getCallingClass();
        Logger logger =loggers.computeIfAbsent(callingClass, LoggerFactory::getLogger);
           List<Object> objectList =  Arrays.stream(objects).collect(Collectors.toList());
        Object[] objectArr = new Object[objectList.size()];
        logger.error(message,objectList.toArray(objectArr));
    }
}

2.AOP

package com.followme.common.aspect;
import com.alibaba.fastjson.JSON;
import com.followme.common.utils.LogUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Created by 2019-04-20.
 */
@Aspect
@Component
public class ControllerLogAspect {
    @Pointcut("execution(* com.followme.controller..*(..))")
    public void log() {
    }
// 請求入?yún)⒋蛴?    @Before("log()")
    public void doBefore(JoinPoint joinPoint) throws Exception {
        LogUtil.info(joinPoint.getSignature().getDeclaringTypeName()+"." + joinPoint.getSignature().getName() + "啟動:請求數(shù)據(jù)為:{}",
        this.buildArgs(joinPoint.getArgs()));
    }
// 返回結(jié)果打印
    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturning(JoinPoint joinPoint,Object object) throws Exception {
        LogUtil.info(joinPoint.getSignature().getDeclaringTypeName()+"." + joinPoint.getSignature().getName() + "結(jié)束:響應(yīng)數(shù)據(jù)為:{}",
                this.buildArgs(joinPoint.getArgs()));
    }
// 異常打印
    @AfterThrowing(throwing = "e",pointcut = "log()")
    public void doAfterThrowing(JoinPoint joinPoint,Exception e) throws Exception {
        LogUtil.info(joinPoint.getSignature().getDeclaringTypeName()+"." + joinPoint.getSignature().getName() + "結(jié)束:響應(yīng)異常為:{}:{}",
                e.getClass(),e.getMessage());
    }
// 獲取參數(shù)
    private String buildArgs(Object[] objects) {
        StringBuffer result = new StringBuffer();
        for (Object obj : objects) {
            result.append(JSON.toJSONString(obj));
        }
        return result.toString();
    }
}

···
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容