应用程序不将日志保存到特定的附加程序

Application not saving logs to specific appender

我正在尝试将一些日志从 Apache James 保存到特定的附加程序。

Apache James 默认使用添加了 Logback 的 Apache Commons Logging。 Logback.xml 配置如下:

    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
            <resetJUL>true</resetJUL>
    </contextListener>

    <!--  Appenders for console and logs -->

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                    <pattern>%d{HH:mm:ss.SSS} %highlight([%-5level]) %thread %logger{15}:%line - %msg%n%rEx</pattern>
                    <immediateFlush>false</immediateFlush>
            </encoder>
    </appender>

    <appender name="JAMES" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>./logs/james.log</file>
            <encoder>
                    <pattern>%d{HH:mm:ss.SSS} [%-5level] %thread %logger{15}:%line - %msg%n%rEx</pattern>
                    <immediateFlush>false</immediateFlush>
            </encoder>

            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">       
               <fileNamePattern>./logs/james.%d{yyyy-MM-dd}.log</fileNamePattern>
               <maxHistory>30</maxHistory>
            </rollingPolicy>
    </appender>

<appender name="JAMES-SERVER" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>./logs/james-server.log</file>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%-5level] %thread %logger{15}:%line - %msg%n%rEx</pattern>
        <immediateFlush>false</immediateFlush>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>./logs/james-server.log.%d{yyyy-MM-dd}</fileNamePattern>
        <maxHistory>30</maxHistory>
    </rollingPolicy>
</appender>

    <logger name="org.domain.mypackage" level="DEBUG">
        <appender-ref ref="JAMES-SERVER" />
    </logger>

    <root level="INFO">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="JAMES"/>
    </root>

Apache James 首发为

java -classpath "james-server-jpa-guice.jar:james-server-jpa-guice.lib/*:james-server-jpa-guice.lib" -javaagent:james-server-jpa-guice.lib/openjpa-3.0.0.jar -Dlogback.configurationFile=./conf/logback.xml -Dworking.directory=. org.apache.james.JPAJamesServerMain >> ./logs/james.log

所有记录到 james.log 都很好,但是 org.domain.mypackage 上面的特定包没有记录到 james-server.log 但似乎在 james.log

有什么想法吗?

您的设置可能需要 2 个修复,删除 immediateFlush 标签至少用于测试,并将 JAMES-APPENDER 添加到 root 记录器。 immediateFlush=false 应该在大量日志记录 activity 时设置,出于测试目的,将其设置为 true.

  <root level="DEBUG">
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="JAMES" />
    <appender-ref ref="JAMES-SERVER" />
  </root>

可以使用 logback-examples 包方便地测试 Logback 设置,如下所示:

export LOGBACK_CP="/home/luis.munoz/.m2/repository/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar:/home/luis.munoz/.m2/repository/ch/qos/logback/logback-core/1.1.3/logback-core-1.1.3.jar:logback-examples-1.1.3.jar:/home/luis.munoz/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar"

java -cp "$LOGBACK_CP" chapters.appenders.ConfigurationTester ~/tmp/logback.xml

以下消息将写入控制台和任何已配置的文件

12:37:45.336 [DEBUG] main c.a.ConfigurationTester:50 - **Hello test 123
12:37:45.338 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 0
12:37:45.439 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 1
12:37:45.540 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 2
12:37:45.640 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 3
12:37:45.742 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 4
12:37:45.843 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 5
12:37:45.943 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 6
12:37:46.044 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 7
12:37:46.145 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 8
12:37:46.246 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 9
12:37:46.355 [ERROR] main c.a.s.s.Bar:32 - error-level request
java.lang.Exception: test exception
        at chapters.appenders.sub.sample.Bar.subMethodToCreateRequest(Bar.java:32) [logback-examples-1.1.3.jar:na]
        at chapters.appenders.sub.sample.Bar.createLoggingRequest(Bar.java:27) [logback-examples-1.1.3.jar:na]
        at chapters.appenders.ConfigurationTester.main(ConfigurationTester.java:58) [logback-examples-1.1.3.jar:na]

在控制台上,写入了几个对调试配置有用的日志记录

...
12:37:45,333 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[ROOT]
12:37:45,333 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [JAMES] to Logger[ROOT]
12:37:45,333 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [JAMES-SERVER] to Logger[ROOT]
 ...

此外,此示例包允许 "play" 配置并检查结果。例如:

  <logger name="chapters.appenders.ConfigurationTester" level="DEBUG" additivity="false">
    <appender-ref ref="JAMES" />
  </logger>

  <logger name="chapters.appenders.sub.sample" level="DEBUG" additivity="false">
    <appender-ref ref="JAMES-SERVER" />
  </logger>

  <root level="DEBUG">
    <appender-ref ref="CONSOLE" />
  </root>

会将此记录到 james.log

16:04:44.487 [DEBUG] main c.a.ConfigurationTester:50 - **Hello test 123
16:04:44.490 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 0
16:04:44.590 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 1
16:04:44.691 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 2
16:04:44.792 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 3
16:04:44.892 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 4
16:04:44.993 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 5
16:04:45.093 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 6
16:04:45.194 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 7
16:04:45.295 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 8
16:04:45.395 [DEBUG] main c.a.ConfigurationTester:54 - logging statement 9

并将此添加到 james-server.log

16:04:45.502 [ERROR] main c.a.s.s.Bar:32 - error-level request
java.lang.Exception: test exception
    at chapters.appenders.sub.sample.Bar.subMethodToCreateRequest(Bar.java:32) [logback-examples-1.1.3.jar:na]
    at chapters.appenders.sub.sample.Bar.createLoggingRequest(Bar.java:27) [logback-examples-1.1.3.jar:na]
    at chapters.appenders.ConfigurationTester.main(ConfigurationTester.java:58) [logback-examples-1.1.3.jar:na]