我遇到了以下问题:我想通过使用 perf4j @Profiled annotation 和 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 注释似乎没有任何作用。