Spring AOP實現後臺管理系統日誌管理

張三瘋不瘋發表於2017-02-08

Spring AOP實現後臺管理系統日誌管理

設計原則和思路:

  • 元註解方式結合AOP,靈活記錄操作日誌
  • 能夠記錄詳細錯誤日誌為運維提供支援
  • 日誌記錄儘可能減少效能影響

1.定義日誌記錄元註解

package com.myron.ims.annotation;

import java.lang.annotation.*;

/**
 * 自定義註解 攔截Controller
 * 
 * @author lin.r.x
 *
 */
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface SystemControllerLog {
    /**
     * 描述業務操作 例:Xxx管理-執行Xxx操作
     * @return
     */
    String description() default "";
}

2.定義用於記錄日誌的實體類

package com.myron.ims.bean;

import java.io.Serializable;
import com.myron.common.util.StringUtils;
import com.myron.common.util.UuidUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
import java.util.Map;

/**
 * 日誌類-記錄使用者操作行為
 * @author lin.r.x
 *
 */
public class Log implements Serializable{
    private static final long serialVersionUID = 1L;

    private String logId;           //日誌主鍵  
    private String type;            //日誌型別  
    private String title;           //日誌標題  
    private String remoteAddr;          //請求地址  
    private String requestUri;          //URI   
    private String method;          //請求方式  
    private String params;          //提交引數  
    private String exception;           //異常    
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date operateDate;           //開始時間  
    private String timeout;         //結束時間  
    private String userId;          //使用者ID  

    public String getLogId() {
        return StringUtils.isBlank(logId) ? logId : logId.trim();
    }
    public void setLogId(String logId) {
        this.logId = logId;
    }


    public String getType() {
        return StringUtils.isBlank(type) ? type : type.trim();
    }
    public void setType(String type) {
        this.type = type;
    }


    public String getTitle() {
        return StringUtils.isBlank(title) ? title : title.trim();
    }
    public void setTitle(String title) {
        this.title = title;
    }


    public String getRemoteAddr() {
        return StringUtils.isBlank(remoteAddr) ? remoteAddr : remoteAddr.trim();
    }
    public void setRemoteAddr(String remoteAddr) {
        this.remoteAddr = remoteAddr;
    }


    public String getRequestUri() {
        return StringUtils.isBlank(requestUri) ? requestUri : requestUri.trim();
    }
    public void setRequestUri(String requestUri) {
        this.requestUri = requestUri;
    }


    public String getMethod() {
        return StringUtils.isBlank(method) ? method : method.trim();
    }
    public void setMethod(String method) {
        this.method = method;
    }


    public String getParams() {
        return StringUtils.isBlank(params) ? params : params.trim();
    }
    public void setParams(String params) {
        this.params = params;
    }

    /**
     * 設定請求引數
     * @param paramMap
     */
    public void setMapToParams(Map<String, String[]> paramMap) {
        if (paramMap == null){
            return;
        }
        StringBuilder params = new StringBuilder();
        for (Map.Entry<String, String[]> param : ((Map<String, String[]>)paramMap).entrySet()){
            params.append(("".equals(params.toString()) ? "" : "&") + param.getKey() + "=");
            String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
            params.append(StringUtils.abbr(StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "" : paramValue, 100));
        }
        this.params = params.toString();
    }


    public String getException() {
        return StringUtils.isBlank(exception) ? exception : exception.trim();
    }
    public void setException(String exception) {
        this.exception = exception;
    }


    public Date getOperateDate() {
        return operateDate;
    }
    public void setOperateDate(Date operateDate) {
        this.operateDate = operateDate;
    }


    public String getTimeout() {
        return StringUtils.isBlank(timeout) ? timeout : timeout.trim();
    }
    public void setTimeout(String timeout) {
        this.timeout = timeout;
    }


    public String getUserId() {
        return StringUtils.isBlank(userId) ? userId : userId.trim();
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }

}

3.定義日誌AOP切面類

package com.myron.ims.aop;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
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.beans.factory.annotation.Autowired;
import org.springframework.core.NamedThreadLocal;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.myron.common.util.DateUtils;
import com.myron.common.util.UuidUtils;
import com.myron.ims.annotation.SystemControllerLog;
import com.myron.ims.annotation.SystemServiceLog;
import com.myron.ims.bean.Log;
import com.myron.ims.bean.User;
import com.myron.ims.service.LogService;

/**
 * 系統日誌切面類
 * @author lin.r.x
 *
 */
@Aspect
@Component
public class SystemLogAspect {
    private  static  final Logger logger = LoggerFactory.getLogger(SystemLogAspect. class);

    private static final ThreadLocal<Date> beginTimeThreadLocal =
            new NamedThreadLocal<Date>("ThreadLocal beginTime");
    private static final ThreadLocal<Log> logThreadLocal = 
            new NamedThreadLocal<Log>("ThreadLocal log");

    private static final ThreadLocal<User> currentUser=new NamedThreadLocal<>("ThreadLocal user");

    @Autowired(required=false)
    private HttpServletRequest request;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    private LogService logService;

    /**
     * Controller層切點 註解攔截
     */
    @Pointcut("@annotation(com.myron.ims.annotation.SystemControllerLog)")
    public void controllerAspect(){}

    /**
     * 前置通知 用於攔截Controller層記錄使用者的操作的開始時間
     * @param joinPoint 切點
     * @throws InterruptedException 
     */
    @Before("controllerAspect()")
    public void doBefore(JoinPoint joinPoint) throws InterruptedException{
        Date beginTime=new Date();
        beginTimeThreadLocal.set(beginTime);//執行緒繫結變數(該資料只有當前請求的執行緒可見)  
        if (logger.isDebugEnabled()){//這裡日誌級別為debug
            logger.debug("開始計時: {}  URI: {}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
                .format(beginTime), request.getRequestURI());
        }

        //讀取session中的使用者 
        HttpSession session = request.getSession();       
        User user = (User) session.getAttribute("ims_user");    
        currentUser.set(user);

    }

    /**
     * 後置通知 用於攔截Controller層記錄使用者的操作
     * @param joinPoint 切點
     */
    @SuppressWarnings("unchecked")
    @After("controllerAspect()")
    public void doAfter(JoinPoint joinPoint) {
        User user = currentUser.get();
        if(user !=null){
            String title="";
            String type="info";                       //日誌型別(info:入庫,error:錯誤)
            String remoteAddr=request.getRemoteAddr();//請求的IP
            String requestUri=request.getRequestURI();//請求的Uri
            String method=request.getMethod();        //請求的方法型別(post/get)
            Map<String,String[]> params=request.getParameterMap(); //請求提交的引數

            try {
                title=getControllerMethodDescription2(joinPoint);
            } catch (Exception e) {
                e.printStackTrace();
            }    
            // 列印JVM資訊。
            long beginTime = beginTimeThreadLocal.get().getTime();//得到執行緒繫結的區域性變數(開始時間)  
            long endTime = System.currentTimeMillis();  //2、結束時間  
            if (logger.isDebugEnabled()){
                logger.debug("計時結束:{}  URI: {}  耗時: {}   最大記憶體: {}m  已分配記憶體: {}m  已分配記憶體中的剩餘空間: {}m  最大可用記憶體: {}m",
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(endTime), 
                        request.getRequestURI(), 
                        DateUtils.formatDateTime(endTime - beginTime),
                        Runtime.getRuntime().maxMemory()/1024/1024, 
                        Runtime.getRuntime().totalMemory()/1024/1024, 
                        Runtime.getRuntime().freeMemory()/1024/1024, 
                        (Runtime.getRuntime().maxMemory()-Runtime.getRuntime().totalMemory()+Runtime.getRuntime().freeMemory())/1024/1024); 
            }

            Log log=new Log();
            log.setLogId(UuidUtils.creatUUID());
            log.setTitle(title);
            log.setType(type);
            log.setRemoteAddr(remoteAddr);
            log.setRequestUri(requestUri);
            log.setMethod(method);
            log.setMapToParams(params);
            log.setUserId(user.getId());
            Date operateDate=beginTimeThreadLocal.get();
            log.setOperateDate(operateDate);
            log.setTimeout(DateUtils.formatDateTime(endTime - beginTime));

            //1.直接執行儲存操作
            //this.logService.createSystemLog(log);

            //2.優化:非同步儲存日誌
            //new SaveLogThread(log, logService).start();

            //3.再優化:通過執行緒池來執行日誌儲存
            threadPoolTaskExecutor.execute(new SaveLogThread(log, logService));
            logThreadLocal.set(log);
        }

    }

    /**
     *  異常通知 記錄操作報錯日誌
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(pointcut = "controllerAspect()", throwing = "e")  
    public  void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
        Log log = logThreadLocal.get();
        log.setType("error");
        log.setException(e.toString());
        new UpdateLogThread(log, logService).start();
    }

    /**
     * 獲取註解中對方法的描述資訊 用於service層註解
     * @param joinPoint切點
     * @return discription
     */
    public static String getServiceMthodDescription2(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SystemServiceLog serviceLog = method
                .getAnnotation(SystemServiceLog.class);
        String discription = serviceLog.description();
        return discription;
    }

    /**
     * 獲取註解中對方法的描述資訊 用於Controller層註解
     * 
     * @param joinPoint 切點
     * @return discription
     */
    public static String getControllerMethodDescription2(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SystemControllerLog controllerLog = method
                .getAnnotation(SystemControllerLog.class);
        String discription = controllerLog.description();
        return discription;
    }

    /**
     * 儲存日誌執行緒
     */
    private static class SaveLogThread implements Runnable {
        private Log log;
        private LogService logService;

        public SaveLogThread(Log log, LogService logService) {
            this.log = log;
            this.logService = logService;
        }

        @Override
        public void run() {
            logService.createLog(log);
        }
    }

    /**
     * 日誌更新執行緒
     */
    private static class UpdateLogThread extends Thread {
        private Log log;
        private LogService logService;

        public UpdateLogThread(Log log, LogService logService) {
            super(UpdateLogThread.class.getSimpleName());
            this.log = log;
            this.logService = logService;
        }

        @Override
        public void run() {
            this.logService.updateLog(log);
        }
    }
}

4.spring 配置掃描切面,開啟@AspectJ註解的支援

    <!-- 啟動對@AspectJ註解的支援 -->  
    <aop:aspectj-autoproxy/> 
    <!-- 掃描切點類元件 -->
    <context:component-scan base-package="com.myron.ims.aop" />
    <context:component-scan base-package="com.myron.ims.service"/>

    <bean id="taskExecutor"   class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">  
        <property name="corePoolSize" value="5" />  
        <property name="maxPoolSize" value="10" />  
        <property name="WaitForTasksToCompleteOnShutdown" value="true" />  
    </bean>  

5.使用範例LoginController方法中新增日誌註解

    /**
    *系統登入
    */
    @RequestMapping("/login.do")
    @SystemControllerLog(description="登入系統")
    @ResponseBody
    public Map<String, Object> login(String username, String password, Boolean rememberMe, HttpServletRequest req){
    //業務程式碼省略...
}   

    /**
     * 安全退出登入
     * @return
     */
    @SystemControllerLog(description="安全退出系統")
    @RequestMapping("logout.do")
    public String logout(){
        Subject subject=SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            subject.logout(); // session 會銷燬,在SessionListener監聽session銷燬,清理許可權快取
        }
        return "/login.jsp";
    }

6.執行效果
這裡寫圖片描述

7.補充原始碼地址:https://github.com/MusicXi/demo-aop-log

相關文章