2017-06-18 21 views
2

私はこの投稿に基づいてパフォーマンスログを実装しようとしています:http://www.baeldung.com/spring-performance-logging。私は各コントローラのエンドポイントとすべてのデータベース要求を記録したいと思います。プロジェクト全体を表示したい場合は、hereがあります。私がエンドポイントにヒットしたとき、何も記録されません。インターセプタクラス内にブレークポイントを配置すると、ブレークポイントも停止しません。私はすでにパッケージのトレースをトレースレベルに設定しました。私は何が欠けていますか?私はそれが@PointCutで何かだと信じていますが、ドキュメントを見た後、私はそれが正しいと信じています。Spring BootとAOPによるパフォーマンスログ

インターセプタ

public class PerformanceMonitorInterceptor extends AbstractMonitoringInterceptor 
{ 
    @Override 
    protected Object invokeUnderTrace(MethodInvocation methodInvocation, Log log) throws Throwable 
    { 
     String name = createInvocationTraceName(methodInvocation); 
     StopWatch stopWatch = new StopWatch(); 
     stopWatch.start(); 
     log.trace(String.format("Method %s execution start at %s", name, LocalDateTime.now())); 

     try 
     { 
      return methodInvocation.proceed(); 
     } 
     finally 
     { 
      stopWatch.stop(); 
      log.trace(String.format("Method %s execution took %dms (%s)", name, 
       stopWatch.getTotalTimeMillis(), DurationFormatUtils 
        .formatDurationWords(stopWatch.getTotalTimeMillis(), true, true))); 
     } 
    } 
} 

構成

@Configuration 
@EnableAspectJAutoProxy 
@Aspect 
public class ContactControllerPerfLogConfig 
{ 
    @Bean 
    public PerformanceMonitorInterceptor performanceMonitorInterceptor() 
    { 
     return new PerformanceMonitorInterceptor(); 
    } 

    // Any public method on the ContactController 
    @Pointcut("execution(public * org.example.phonebookexample.app.contact.ContactController.*(..))") 
    public void contactControllerMonitor() 
    { 
    } 

    @Bean 
    public Advisor contactControllerMonitorAdvisor(
     PerformanceMonitorInterceptor performanceMonitorInterceptor) 
    { 
     AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); 
     pointcut.setExpression("org.example.phonebookexample.app.contact.ContactControllerPerfLogConfig.contactControllerMonitor()"); 
     return new DefaultPointcutAdvisor(pointcut, performanceMonitorInterceptor); 
    } 
} 
+0

「application.properties」のログプロパティを指定しましたか? –

+0

はいトレースレベルで設定されています – ndrone

+0

これだけに変更しようとしましたか: 'logging.level = trace'? –

答えて

1

AbstractTraceInterceptorMethodInterceptorを実装し、以下のようにinvoke()方法を実装している:

したがって、InterceptorクラスのロガーはTRACEに設定する必要があります。基本PerformanceMonitorInterceptorについては、org.springframework.aop.interceptor.PerformanceMonitorInterceptorとなります。独自のインターセプタを作成したので、独自のクラスのログレベルを設定する必要があります。 代替アプローチの例として、JamonPerformanceMonitorInterceptorを見てください。必要に応じて、ログレベルに関係なくすべての呼び出しを追跡できます。

それはAOPに来るとき、春のJavaのconfigsは、XMLのconfigsに比べてとてもエレガントではありませんので、完全を期すために、私はまた、XMLの構成例を投稿します:

<bean id="performanceMonitorInterceptor" class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor"/> 

<aop:config> 
    <aop:pointcut id="contactControllerMonitor" expression="execution(public * org.example.phonebookexample.app.contact.ContactController.*(..))" /> 
    <aop:advisor id="contactControllerMonitorAdvisor" pointcut-ref="contactControllerMonitor" advice-ref="performanceMonitorInterceptor"/> 
</aop:config> 

この設定をすることができは、

@ImportResource("classpath:/aop-config.xml") 
public class MainConfig { ... } 
+0

それは、私のカスタムインターセプターのための 'TRACE'レベルの設定が必要でした。私は、Javaのconfigsには、XMLのようにエレガントではないという意見に同意する必要があります。 – ndrone