Perf4J @Profiled注释+ log4j不起作用

时间:2013-07-18 07:45:02

标签: annotations log4j aspectj perf4j

我遇到了以下问题:我想通过使用perf4j @Profiled注释和log4j监视一些低性能方法的执行时间。 问题是监视日志文件为空,如下所示:

Performance Statistics   2013-07-17 18:07:50 - 2013-07-17 18:08:00
Tag                                                  Avg(ms)         Min         Max     Std Dev       Count

Performance Statistics   2013-07-17 18:08:00 - 2013-07-17 18:08:10
Tag                                                  Avg(ms)         Min         Max     Std Dev       Count

log4j.xml包含在projectRoot / conf / exp目录中,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
</appender>

<appender name="logfile" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="./logs/exp-log.txt"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%p %d{HH:mm:ss.SSS}    %c          %m%n"/>
</layout>
</appender>

<appender name="CoalescingStatistics"
          class="org.perf4j.log4j.AsyncCoalescingStatisticsAppender">

    <param name="TimeSlice" value="10000"/>
    <appender-ref ref="fileAppender"/>

    <appender-ref ref="graphExecutionTimes"/>
    <appender-ref ref="graphExecutionTPS"/>
</appender>

<appender name="fileAppender" class="org.apache.log4j.FileAppender">
    <param name="File" value="./logs/perfStats.log"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%m%n"/>
    </layout>
</appender>

<appender name="graphExecutionTimes"
          class="org.perf4j.log4j.GraphingStatisticsAppender">
    <!-- Possible GraphTypes are Mean, Min, Max, StdDev, Count and TPS -->
    <param name="GraphType" value="Mean"/>
    <!-- The tags of the timed execution blocks to graph are specified here -->
    <param name="TagNamesToGraph" value="firstBlock,secondBlock"/>
    <appender-ref ref="graphsFileAppender"/>
</appender>

<appender name="graphExecutionTPS"
          class="org.perf4j.log4j.GraphingStatisticsAppender">
    <param name="GraphType" value="TPS"/>
    <param name="TagNamesToGraph" value="firstBlock,secondBlock"/>
    <appender-ref ref="graphsFileAppender"/>
</appender>

<appender name="graphsFileAppender" class="org.apache.log4j.FileAppender">
    <param name="File" value="./logs/perfGraphs.log"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%m%n"/>
    </layout>
</appender>

<logger name="org.perf4j.TimingLogger" additivity="false">
    <level value="INFO"/>
    <appender-ref ref="CoalescingStatistics"/>
</logger>

<logger name="sui">
<level value="debug"/>
</logger>

<root>
    <priority value="debug"/>
    <appender-ref ref="logfile"/>
    <appender-ref ref="stdout"/>
</root>
</log4j:configuration>

我的来源中有以下注释:

@Profiled(tag = "exportRecsForAccount")
    protected void exportRecsForAccount(NetSuiteAcctInfo acctInfo) {

@Profiled(tag = "exportRecsForEachAccount")
    protected boolean exportRecsForEachAccount() throws QueueManagerException, CriticalDBConnectionFailed {

除此之外,我还有一个位于projectRoot / recources / META-INF中的aop.xml。其内容如下:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">

<aspectj>
    <aspects>
        <aspect name="org.perf4j.log4j.aop.TimingAspect"/>
    </aspects>

    <weaver options="-verbose -showWeaveInfo">
        <include within="comp.sui.ExportService"/>
    </weaver>
</aspectj>

其中comp.sui.ExportService是源代码类的链接。

所以问题是我做错了什么?当我使用StopWatches标记时,一切都很好,但是使用@Profiled注释似乎没什么用。

1 个答案:

答案 0 :(得分:0)

尝试:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">

<aspectj>
    <aspects>
        <aspect name="org.perf4j.log4j.aop.TimingAspect"/>
    </aspects>

    <weaver options="-verbose -showWeaveInfo">
        <include within="comp.sui.ExportService"/>

        <!-- Weave in perf4j aspects for Aspectj 1.6.7+ -->
        <include within="org.perf4j.slf4j.aop.*"/>
        <include within="org.perf4j.aop.*"/>
    </weaver>

</aspectj>