我在我的代码中设置了一些stop watch调用来测量一些代码块,所有的消息都会进入我的主日志,而不是计时日志。可以很好地创建perfStats.log文件,但所有消息都会写入根日志,根据我读过的文档,我认为这是不应该发生的。有什么明显的我遗漏了什么吗?
perf4j教程link
示例代码
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();
}
}示例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>发布于 2010-04-15 23:53:08
我检查了jar,在perf4j jar中没有名为TimingLogger的类,这也可以通过searching their code repository在这里看到,这可以解释消息不会进入您期望的文件。如果您使用示例中显示的默认LoggingStopWatch类,那么它唯一要做的就是打印到std err,您可以看到here。我尝试将我的代码中的记录器改为使用它们的Log4JStopWatch类,并将xml文件中的记录器更改为org.perf4j.log4j.Log4JStopWatch,但是消息变成标准输出,而不是文件,这可能是因为它没有使用我猜的log4j.xml中指定的配置。我将尝试与维护该项目的团队进行跟进,看看他们是否有更新的示例或错误修复。
发布于 2010-04-16 01:25:59
问题是您使用的是LoggingStopWatch,它默认情况下会将其所有输出发送到stderr。您希望使用Log4JStopWatch。LoggingStopWatch的每个直接子类都是为不同的框架设计的(请参阅http://perf4j.codehaus.org/apidocs/org/perf4j/LoggingStopWatch.html)。
发布于 2012-07-04 05:19:23
org.log4j.TimingLogger不是类名,而是Perf4j使用的默认记录器名称。如果您使用的是Log4j,那么您应该使用org.perf4j.log4j.Log4JStopWatch类,并且不要忘记在创建它时使用正确的标记,因为统计数据将按该标记进行分组。
按照您的示例进行操作:
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();
}
}使用该实现和相同的配置文件,您会得到如下所示的输出:
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希望这能澄清这个问题。
https://stackoverflow.com/questions/2645670
复制相似问题