在没有其他库的情况下使用 perf4J Profiled 注释(例如 log4j)

Using perf4J Profiled annotation without other libraries (like log4j for example)

使用 perf4J 时,此代码运行良好:

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

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

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

当使用 @Profiled 注释向您的代码添加秒表时,您通常会使用 AspectJ 加载时间编织 (LTW) 在 load/runtime 检测您的代码,这会添加秒表 [=24] =] 和有关注释方法的记录。

使用哪种日志记录 library/API(log4j、slf4j、commons-logging、JUL)取决于您在 aop.xml 配置 LTW 时配置的 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 记录器改变这个。