logback不会将perf4j打印到日志文件

时间:2015-05-06 12:20:31

标签: java logback perf4j

我的应用程序正在使用logback。现在我想将perf4j 0.9.16用于我当前的应用程序。配置来自http://perf4j.codehaus.org/apidocs/org/perf4j/logback/package-summary.html

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern>/var/log/myapp/myapp.log.%d{yyyy-MM-dd}</FileNamePattern>
    </rollingPolicy>
    <encoder>
        <pattern>%d %5p | %t | %-55logger{55} | %m %n</pattern>
    </encoder>
</appender>

<appender name="perf4jFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern>/var/log/myapp/myapp.timer.%d{yyyy-MM-dd}</FileNamePattern>
    </rollingPolicy>
    <encoder>
        <Pattern>%date %-5level [%thread] %logger{36} [%file:%line] %msg%n</Pattern>
    </encoder>
</appender>

<logger name="org.perf4j.TimingLogger" level="DEBUG" additivity="false">
    <appender-ref ref="perf4jFileAppender"/>
</logger>

<root level="INFO">
    <appender-ref ref="file"/>
</root>

myapp.log 仍然像以前一样工作,但 myapp.timer 是空文件。它会像这样登录到控制台:

start[1430914241369] time[4] tag[firstMethod.success] message[OK]
start[1430914241375] time[0] tag[secondMethod.success] message[OK]

我已经尝试了这个perf4j settings for logback.xml,但它无能为力。 有什么想法吗?

谢谢。

===========================================

更新

使用<configuration debug="true">,应用程序日志:

14:45:57,434 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/var/app/tomcat/webapps/myapp/WEB-INF/classes/logback.xml]
14:45:57,506 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
14:45:57,512 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Namingappender as [file]
14:45:57,533 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA- Pushing component [rollingPolicy] on top of the object stack.
14:45:57,561 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
14:45:57,563 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use thepattern /var/log/myapp/myapp.log.%d{yyyy-MM-dd} for the active file
14:45:57,569 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern '/var/log/myapp/myapp.log.%d{yyyy-MM-dd}'.
14:45:57,569 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
14:45:57,572 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Wed May 06     14:45:57 CEST 2015
14:45:57,573 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA- Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
14:45:57,574 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA- Pushing component [encoder] on top of the object stack.
14:45:57,597 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file] - Active log file name: /var/log/myapp/myapp.log.2015-05-06
14:45:57,597 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file] - File property is set to [null]
14:45:57,598 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
14:45:57,598 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Namingappender as [perf4jFileAppender]
14:45:57,598 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA- Pushing component [rollingPolicy] on top of the object stack.
14:45:57,599 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
14:45:57,599 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use thepattern /var/log/myapp/myapp.timer.%d{yyyy-MM-dd} for the active file
14:45:57,600 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern '/var/log/myapp/myapp.timer.%d{yyyy-MM-dd}'.
14:45:57,600 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
14:45:57,600 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Wed May 06     14:45:57 CEST 2015
14:45:57,600 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA- Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
14:45:57,600 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA- Pushing component [encoder] on top of the object stack.
14:45:57,601 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[perf4jFileAppender] - Active log file name: /var/log/myapp/myapp.timer.2015-05-06
14:45:57,601 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[perf4jFileAppender] - File property is set to [null]
14:45:57,601 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.perf4j.TimingLogger] to DEBUG
14:45:57,601 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.perf4j.TimingLogger] to false
14:45:57,601 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [perf4jFileAppender] to Logger[org.perf4j.TimingLogger]
14:45:57,604 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
14:45:57,604 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [file] to Logger[ROOT]
14:45:57,604 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.

2 个答案:

答案 0 :(得分:3)

当您将自己的记录器传递给Slf4JStopWatch构造函数之一时会发生这种情况。使用默认构造函数(new Slf4JStopWatch())或将配置文件更改为

<logger name="com.example.MyOwnPerformanceLogger" level="DEBUG" additivity="false">
    <appender-ref ref="perf4jFileAppender"/>
</logger>

org.perf4j.TimingLogger只是秒表的默认记录器名称。

顺便说一下:你的<pattern>个元素之一是大写的。

答案 1 :(得分:0)

代码看起来正确。请注意,logback会从几个地方加载配置,因此您的配置可能无法使用。要了解其进展情况,请将<configuration>替换为<configuration debug="true">

然后,

Logback会在解析配置时打印它自己的设置。如果您没有看到任何内容,那么类路径或logback.xmllogback-test.xml上的某个位置会有一个额外的logback.groovy文件。