11.spring boot aop 日志

業(yè)務(wù)場(chǎng)景:pc端,登陸頁(yè)面,登陸賬戶(hù)后,訪(fǎng)問(wèn)各個(gè)模塊,每次訪(fǎng)問(wèn)和操作都記錄日志.

1.開(kāi)啟aop,就一步.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

application.yml中下面這個(gè)設(shè)置默認(rèn)開(kāi)啟

spring.aop.auto=true

2.增加一個(gè)自定義annotation

package com.yunchuang.config.annotation;

import java.lang.annotation.*;

/**
 * 日志
 *
 * @author 尹冬飛
 * @date 2015-07-02 15:43:26
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface Log {
    /**
     * 1.日志內(nèi)容
     */
    String value();
}

3.定義切面類(lèi)

package com.yunchuang.config.aspect;

import com.yunchuang.config.annotation.Log;
import com.yunchuang.console.user.domain.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * aop切面日志
 *
 * @author 尹冬飛
 * @create 2017-05-02 14:16
 */
@Aspect
@Component
public class LogAspect {
    private Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(public * com.yunchuang.console.*.web..*.*(..))")
    public void consoleLog() {
    }

    @Before("consoleLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到請(qǐng)求,記錄請(qǐng)求內(nèi)容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //獲取request
        HttpServletRequest request = attributes.getRequest();
        //獲取session
        HttpSession session = request.getSession();
        //獲取方法簽名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //獲取方法
        Method method = signature.getMethod(); //獲取被攔截的方法
        //獲取自定義Log注解
        Log annotation = method.getAnnotation(Log.class);
        //如果有Log注解,則寫(xiě)入日志
        if (annotation != null) {
            //獲取session里的用戶(hù)對(duì)象
            User user = (User) session.getAttribute("loginUser");
            //獲取用戶(hù)名
            String name = user != null ? "[" + user.getNickname() + "]" : "";
            logger.info(name + "[" + annotation.value() + "][" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + "][" + Arrays.toString(joinPoint.getArgs()) + "][" + request.getRemoteAddr() + "]");
        }
    }

    @AfterReturning(returning = "ret", pointcut = "consoleLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 處理完請(qǐng)求,返回內(nèi)容
        //logger.info("RESPONSE : " + ret);
    }
}

這里關(guān)鍵的2個(gè)地方:
獲取方法,就能獲取自定義注解,就能獲取里面的值

//獲取方法簽名
MethodSignature signature = (MethodSignature) joinPoint.getSignature();

獲取請(qǐng)求內(nèi)容就能獲取request和session

// 接收到請(qǐng)求,記錄請(qǐng)求內(nèi)容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

4.在controller的方法上增加自定義注解.

因?yàn)長(zhǎng)og類(lèi)里屬性是value(),所以注解里不用寫(xiě)@Log(value="日志內(nèi)容")

    @Log("日志內(nèi)容")
    @GetMapping("/list")
    public String list() {
        return "console/user/list";
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,284評(píng)論 6 342
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,680評(píng)論 19 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,316評(píng)論 25 708
  • "There is only one heroism in the world: to see the world...
    Carson找不到北閱讀 216評(píng)論 0 1
  • 【感恩有你】201700715學(xué)習(xí)力踐行記錄D61 學(xué)習(xí):《孩子是如何學(xué)習(xí)的》P58-P74學(xué)習(xí)到孩子知道的遠(yuǎn)比能...
    恩恩媽閱讀 374評(píng)論 0 0

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