StopWatch工作正常,但在使用@Profiled注释时无法记录

时间:2016-04-14 10:35:57

标签: java logging perf4j

我不太了解per4j,因为我是新手,我正在尝试将perf4j计时添加到我的maven项目中。我通过查看一些示例创建了项目设置。

这是我的log4j.xml文件

<?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="C:/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="C:/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="C:/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>

这是我的aom.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"/>

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

</aspectj>

当我使用StopWatch运行示例主类时,我正在使用时间详细信息记录日志文件但是当我使用@Profiled注释时,它无法创建计时器日志。它创建新的日志文件但文件将为空。

我的示例类如下所示:

import org.apache.log4j.Logger;
import org.perf4j.StopWatch;
import org.perf4j.aop.Profiled;
import org.perf4j.log4j.Log4JStopWatch;

public class Sampletest {

private final static Logger logger = Logger.getLogger(Sampletest.class);

@Profiled(tag= "email", logFailuresSeparately=false)
public void email(){
//      StopWatch stopWatch = new Log4JStopWatch("email");
    try {
        Thread.sleep((long)(Math.random() * 1000L));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    logger.info("email");
 //     stopWatch.stop();

}

@Profiled(tag = "phone")
private void phone(){
    //StopWatch stopWatch = new Log4JStopWatch("phone");
    try {
        Thread.sleep((long)(Math.random() * 2000L));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    logger.info("phone");
    //stopWatch.stop();
    }

public static void main(String[] args) {

    logger.info("Welcome to Perf4j");
    Sampletest obj = new Sampletest();
    obj.email();
    obj.phone();
  }
  }

任何人都可以告诉我使用@Profiled anotation我做错了什么,当我使用StopWatch时一切顺利。

0 个答案:

没有答案