简体   繁体   English

Perf4j无法正确记录

[英]Perf4j Not Logging Correctly

I setup some stop watch calls in my code to measure some code blocks and all the messages are going into my primary log and not into the timing log. 我在代码中设置了一些秒表调用以测量一些代码块,并且所有消息都进入我的主日志而不是计时日志。 The perfStats.log file gets created just fine but all the messages go to the root log which I didn't think was supposed to happen according to the docs I've read. perfStats.log文件可以很好地创建,但是所有消息都转到根日志,根据我阅读的文档,我认为这应该不会发生。 Is there something obvious I'm missing here? 有什么明显的我想念的地方吗?

perf4j tutorial link perf4j教程链接

Example code 范例程式码

import org.apache.log4j.Logger;
import org.perf4j.LoggingStopWatch;
import org.perf4j.StopWatch;

public class PerfLogger {

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        Logger  logger = Logger.getLogger(PerfLogger.class.getName());

        logger.info("Starting perf log test");

        StopWatch stopWatch = new LoggingStopWatch("test time");

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        stopWatch.stop();

    }

}

Example log4j.xml 示例log4j.xml

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>    

    <appender name="STDOUT-DEBUG" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p [%t]%x %M (%F:%L) - %m%n"/>
        </layout>
    </appender>


   <!-- Perf4J appenders -->
    <!--
       This AsyncCoalescingStatisticsAppender groups StopWatch log messages
       into GroupedTimingStatistics messages which it sends on the
       file appender defined below
    -->
    <appender name="CoalescingStatistics"
              class="org.perf4j.log4j.AsyncCoalescingStatisticsAppender">
        <!--
          The TimeSlice option is used to determine the time window for which
          all received StopWatch logs are aggregated to create a single
          GroupedTimingStatistics log. Here we set it to 10 seconds, overriding
          the default of 30000 ms
        -->
        <param name="TimeSlice" value="10000"/>
        <appender-ref ref="fileAppender"/>
    </appender>


    <!-- This file appender is used to output aggregated performance statistics -->
    <appender name="fileAppender" class="org.apache.log4j.FileAppender">
        <param name="File" value="perfStats.log"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%m%n"/>
        </layout>
    </appender>

    <!-- Loggers -->
    <!--
      The Perf4J logger. Note that org.perf4j.TimingLogger is the value of the
      org.perf4j.StopWatch.DEFAULT_LOGGER_NAME constant. Also, note that
      additivity is set to false, which is usually what is desired - this means
      that timing statements will only be sent to this logger and NOT to
      upstream loggers.
    -->
    <logger name="org.perf4j.TimingLogger" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="CoalescingStatistics"/>
    </logger>

    <root>
        <priority value="info"/>
        <appender-ref ref="STDOUT-DEBUG"/>
    </root>

</log4j:configuration>

I checked the jar and there is no class named TimingLogger in the perf4j jar, this can also be seen here by searching their code repository , which would explain the messages not going to the file you would expect them to. 我检查了jar,在perf4j jar中没有名为TimingLogger的类,这也可以通过搜索其代码存储库来看到,该代码存储库将解释消息不会发送到您期望它们的文件中。 If you use the default LoggingStopWatch class they show in the example it looks like the only thing it will ever do is print to std err which you can see here . 如果您使用默认的LoggingStopWatch类,它们将在示例中显示,它看起来唯一会做的就是打印到std err,您可以在此处看到。 I tried changing the logger in my code to use their Log4JStopWatch class instead and changed the logger in the xml file to be org.perf4j.log4j.Log4JStopWatch but the messages go stdout instead of the file, which is probably because it isn't using the configs specified in the log4j.xml I'm guessing. 我尝试将代码中的记录器更改为使用其Log4JStopWatch类,并将xml文件中的记录器更改为org.perf4j.log4j.Log4JStopWatch,但消息显示为stdout而不是文件,这可能是因为未使用我猜在log4j.xml中指定的配置。 I'll try and follow up with the team maintaining the project to see if they have an updated example or bug fix. 我将尝试与维护该项目的团队进行跟进,以查看他们是否具有更新的示例或错误修复。

org.log4j.TimingLogger is not a class name but the default logger name used by Perf4j. org.log4j.TimingLogger不是类名称,而是Perf4j使用的默认记录器名称。 If you are using Log4j you should use the org.perf4j.log4j.Log4JStopWatch class and don't forget to use the proper tag when creating it as stats will be grouped by that. 如果您使用的是Log4j,则应使用org.perf4j.log4j.Log4JStopWatch类,并且在创建它时不要忘记使用正确的标记,因为统计信息将按此分组。

Following your example: 按照您的示例:

import org.apache.log4j.Logger;
import org.perf4j.LoggingStopWatch;
import org.perf4j.StopWatch;

public class PerfLogger {

    /**
     * @param args
     */
    public static void main(String[] args) 
    {

        StopWatch stopWatch = new LoggingStopWatch("main(..)");

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        stopWatch.stop();

    }
}

With that implementation and your same configuration file you get an output like the following: 使用该实现和相同的配置文件,您将得到如下输出:

Performance Statistics   2012-07-03 22:13:20 - 2012-07-03 22:13:30
Tag                        Avg(ms)         Min         Max   Std Dev       Count
main(..)                   999.0           999         999       0.0           1

Hope this clarifies this problem. 希望这可以澄清这个问题。

The issue is that you are using a LoggingStopWatch, which by default sends all of its output to stderr. 问题是您使用的是LoggingStopWatch,默认情况下会将其所有输出发送到stderr。 You want to be using a Log4JStopWatch. 您要使用Log4JStopWatch。 Each of the direct subclasses of LoggingStopWatch is designed for a different framework (see http://perf4j.codehaus.org/apidocs/org/perf4j/LoggingStopWatch.html ). LoggingStopWatch的每个直接子类都为不同的框架设计(请参阅http://perf4j.codehaus.org/apidocs/org/perf4j/LoggingStopWatch.html )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM