2016-07-21 47 views
0

私は、ロギング目的でSpring AOPを使用しています。すべてが機能していますが、ログではAspectクラスの名前しか取得できません。私はクラスのそれぞれの名前を印刷したい。コードスニペットは次のとおりです。私の要件を満たすには、次のコードでどのような変更が必要ですか。Spring AOPを使用し、ログファイル内のそれぞれのクラス名を取得

プライベート静的最終ロガーロガー= Logger.getLogger(LogginAspect.class);

@After( "実行(* com.app.c2pc .. (..))") 公共ボイドlogAfter(ジョインポイントジョインポイント){

logger.info("After executing method : " + joinPoint.getSignature().getName()); 
    logger.info("***************************************************************************"); 
} 

@Before("execution(* com.app.c2pc..*.*(..))") 
public void logBefore(JoinPoint joinPoint) { 

    logger.info("***************************************************************************"); 
    logger.info("Before executing method : " + joinPoint.getSignature().getName()); 
} 

@Around("execution(* com.app.c2pc..*.*(..)) && !execution(* com.app.c2pc.login.LoginController.*(..)) ") 
public Object logAround(ProceedingJoinPoint pjp) throws Throwable { 

    long start = System.currentTimeMillis(); 
    Object clazz = pjp.getTarget().getClass().getName(); 
    String methodName = pjp.getSignature().getName(); 
    logger.info("Entering Class " + clazz + " With Method Name " + methodName); 
    Object[] obj = pjp.getArgs(); 
    int i = 0; 
    try { 
     for (Object o : obj) { 
      logger.info(++i + " : Parameter Name :" + (null != o ? o.toString() : "")); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    Object output = pjp.proceed(pjp.getArgs()); 

    logger.info("Excecution Completed for method : " + methodName + " in Class : " + clazz + " with result " 
      + output); 
    long elapsedTime = System.currentTimeMillis() - start; 
    logger.info("Execution time for method : " + methodName + " in Class : " + clazz + " : " + elapsedTime 
      + " milliseconds."); 
    return output; 
} 

@AfterThrowing(pointcut = "execution(* com.app.c2pc..*.*(..))", throwing = "error") 
public void logAfterThrowing(JoinPoint joinPoint, Throwable error) { 

    logger.info("Exception thrown by method : " + joinPoint.getSignature().getName()); 
    logger.info("Exception name : " + error); 
} 

@AfterReturning(pointcut = "execution(* com.app.c2pc..*.*(..))", returning = "result") 
public void logAfterReturning(JoinPoint joinPoint, Object result) { 

    logger.info("Method : " + joinPoint.getSignature().getName() + " returned value is : " + result); 
} 

これは、O/PIでありますログファイルを取得します。 -

2016-07-20 23:41:20 | | INFO LogginAspect:23 - メソッド実行後:checkSubmitEvalFlag 2016-07-20 23:41:20 | | INFO LogginAspect:24 - ********************************************* ***************************** 2016-07-20 23:41:20 | | INFO LogginAspect:70 - メソッド:checkSubmitEvalFlag戻り値:0 2016-07-20 23:41:20 | | INFO LogginAspect:40 - クラスを入力するcom.app.c2pc.scie.daoImpl.PbdDAOImpl $$ EnhancerBySpringCGLIB $$メソッド名で$ 688f6ece getBackUpUserDetails 2016-07-20 23:41:20 | | INFO LogginAspect:45から1:パラメータ名:[email protected]

答えて

-1

あなたが完全に取得する"joinPoint.getTarget()のgetClass()のgetName()。。"を使用することができます"com.abc.pqr.MyClass"

のような修飾クラス名は、あなたは "MyClassの" のようなクラス名のみを取得するには、 "joinPoint.getTarget()。のgetClass()。getSimpleName()" を使用することができます。

+0

ログファイルのクラス名がリクエストごとに異なるように、このレベルでコードを変更します。 プライベート静的最終ロガーロガー= Logger.getLogger(LogginAspect.class); –

+0

あなたが使用することができます**プライベート静的最終ロガーロガー= Logger.getLogger(joinPoint.getTarget()。getClass()); ** – Rupesh

+0

推奨されている回答ではありません。使用しないと思われる反射を使用するため、アプリケーションが遅くなります – AurA

関連する問題