【问题标题】:Using perf4J Profiled annotation without other libraries (like log4j for example)在没有其他库的情况下使用 perf4J Profiled 注释(例如 log4j)
【发布时间】:2016-07-24 12:44:27
【问题描述】:

当使用 perf4J 时,这段代码运行良好:

StopWatch stopWatch = new LoggingStopWatch();
stopWatch.stop("example1", "custom message text");

但是在使用@Profiled 时,如何输出或获取度量,用最少的代码

@Profiled(tag = "dynamicTag_{$0}")
public void boucle(int k)
{
    // My code to profile
}

【问题讨论】:

    标签: java perf4j


    【解决方案1】:

    当使用@Profiled 注释将秒表添加到您的代码中时,您通常会使用 AspectJ 加载时间编织 (LTW) 在加载/运行时检测您的代码,这会在带注释的方法周围添加秒表开始/停止和记录。

    使用哪个日志库/API(log4j、slf4j、commons-logging、JUL)取决于您在配置 LTW 的 aop.xml 中配置的 org.perf4j.aop.ProfiledTimingAspect 的特定实例。例如这个配置:

    <aspectj>
      <!--
        We only want to weave in the log4j TimingAspect into the @Profiled classes.
        Note that Perf4J provides TimingAspects for the most popular Java logging
        frameworks and facades: log4j, java.util.logging, Apache Commons Logging
        and SLF4J. The TimingAspect you specify here will depend on which logging
        framework you wish to use in your code.
      -->
      <aspects>
        <aspect name="org.perf4j.log4j.aop.TimingAspect"/>
        <!-- if SLF4J/logback use org.perf4j.slf4j.aop.TimingAspect instead -->
      </aspects>
    
      <weaver options="-verbose -showWeaveInfo">
        <!--
          Here is where we specify the classes to be woven. You can specify package
          names like com.company.project.*
        -->
        <include within="ProfiledExample"/>
      </weaver>
    </aspectj>
    

    ... 使用org.perf4j.log4j.aop.TimingAspect,因此秒表将记录到您配置的 Log4J 秒表记录器中。如果您想避免使用第三方库,您可以为 JUL JDK 记录器更改此设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      相关资源
      最近更新 更多