JavaのWebアプリケーション開発フレームワークによる、Webサイト開発の顛末記です。

EclipseのMavenを使った、Spring-MVC、Thymeleaf、MyBatis 等のプログラミングテクニックを、
備忘録的に記録しています。実際に動くソースコードを多用して説明していますので、
これからEclipseや、Spring-MVCを始めたいと思っている人にとって、少しでも参考になれば幸いです。
Spring-MVCの散歩道 > SpringMVC の小径 > ちょっと寄り道 ロギングの小径 > AOP ロギングの魔法

package jp.dip.arimodoki.common;

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.springframework.aop.ThrowsAdvice;
import org.springframework.stereotype.Component;

/**
 * Aspect Logging クラス
 */
@Aspect			//Aspect有効化
@Component		//コンポーネントスキャン対象
public class LogAspect implements CConst ,ThrowsAdvice {

    /** ログに追加する文字列*/
    private static final String LABEL_ENTER = "() Enter";
    private static final String LABEL_LEAVE = "() Leave";

    /** 対象のレイヤー(対象パッケージ) */
    private static final String LAYER_CONTROLLER = "jp.dip.arimodoki.cntl";
    private static final String LAYER_SERVICE = "jp.dip.arimodoki.blogic";

    /**
     * デフォルトコンストラクタ
     */
    public LogAspect() {}

    /**
     * ログ出力
     * @param jp JoinPoint
     * @param label ログに追加する文字列
     */
    private void logInfo(JoinPoint jp, String label) {
        Class<?> cls = jp.getTarget().getClass();
        String cName = jp.getTarget().getClass().getName(); // クラス名
        String mName = jp.getSignature().getName(); // メソッド名
        String msg = "=="+cName+":"+mName+ label;
        logger.log_info(cls, msg);   // Log4j2
    }

    /**
     * エラーログ出力
     * @param ex Exception
     */
    private void logError(Exception ex) {
        logger.log_error(ex);       // Log4j2
    }

    /**
     * コントローラ層の全メソッドに対してBeforeAdviceを実施する
     * @param jp JoinPoint
     */
    @Before("execution(* "+LAYER_CONTROLLER+".*.*(..))")
    public void beforeController(JoinPoint jp) {
        this.logInfo(jp, LABEL_ENTER);
    }

    /**
     * ビジネスロジック層の全メソッドに対してBeforeAdviceを実施する
     * @param jp JoinPoint
     */
    @Before("execution(* "+LAYER_SERVICE+".*.*(..))")
    public void beforeBlogic(JoinPoint jp) {
        this.logInfo(jp, LABEL_ENTER);
    }

    /**
     * コントローラ層の全メソッドに対してAfterAdviceを実施する
     * @param jp JoinPoint
     */
    @After("execution(* "+LAYER_CONTROLLER+".*.*(..))")
    public void afterController(JoinPoint jp) {
        this.logInfo(jp, LABEL_LEAVE);
    }

    /**
     * ビジネスロジック層の全メソッドに対してAfterAdviceを実施する
     * @param jp JoinPoint
     */
    @After("execution(* "+LAYER_SERVICE+".*.*(..))")
    public void afterBlogic(JoinPoint jp) {
        this.logInfo(jp, LABEL_LEAVE);
    }

    /**
     * コントローラ層の全メソッドに対してAfterThrowingを実施する
     * @param ex Exception
     */
    @AfterThrowing(pointcut="execution(* "+LAYER_CONTROLLER+".*.*(..))",throwing="ex")
    public void afterThrowingController(Exception ex) throws Throwable {
        this.logError(ex);
    }

    /**
     * ビジネスロジック層の全メソッドに対してAfterThrowingを実施する
     * @param ex Exception
     */
    @AfterThrowing(pointcut="execution(* "+LAYER_SERVICE+".*.*(..))",throwing="ex")
    public void afterThrowingBlogic(Exception ex) throws Throwable {
        this.logError(ex);
    }
}