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

时间:2019-05-13 20:17:07

标签: java logging logback apache-commons-logging james

我正在尝试将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

对此有何想法?

1 个答案:

答案 0 :(得分:1)

您的设置可能需要修复2个问题,至少删除immediateFlush标签以进行测试,然后向JAMES-APPENDER记录器添加root。当需要进行大量日志记录活动时,应设置immediateFlush=false,出于测试目的,请将其设置为true

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

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

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]
 ...

此外,此示例包还允许“播放”配置并检查结果。例如:

  <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]