Log4j - 如何从log4j隐藏启动配置消息

时间:2016-02-09 18:27:43

标签: logging log4j startup

我正在使用log4j(1.7.10)的命令行应用程序。

在log4j.xml中,应用程序配置了4个appender,用于不同类型的日志,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="true">

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p %c{1}:%L - %m%n" />
    </layout>
</appender>

<appender name="regularAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="maxBackupIndex" value="10" />
    <param name="file" value="./target/logs/develop.log" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601}|%p|%t|%m%n" />
    </layout>
</appender>

<appender name="errorsAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="maxBackupIndex" value="10" />
    <param name="file" value="./target/logs/errors.log" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601}|%p|%t|%m%n" />
    </layout>
</appender>

 <!-- with keystore in config -->
 <appender name="secure-appender" class="com.company.slogger.SecureAppender">
    <param name="file" value="./target/logs/secure.log" />
    <param name="pattern" value="%d{ISO8601}|%p|%t|%m%n" />
    <param name="verifyPattern" value="TIMESTAMP|LEVEL|THREAD|MESSAGE" />
    <param name="numberLogLines" value="10000" />
    <param name="timerLogMilliseconds" value="300000" />
    <param name="triggeringTime" value="3600000" />
    <param name="encoding" value="UTF-8" />
    <param name="logFileNamePolicyName" value="SEQUENTIAL_DATE_FILENAME" />
</appender>-->

<logger name="std">
    <level value="INFO" />
    <appender-ref ref="regularAppender" />
</logger>

<!--Secure logger -->
<logger name="SecureLogger" additivity="false">
    <level value="INFO" />
    <appender-ref ref="secure-appender" />
</logger>

<root>
    <level value="INFO" />
    <appender-ref ref="console" />
</root>
</log4j:configuration>

日志记录工作正常。问题是当应用程序启动时,屏幕上会显示大约30行log4j配置消息,如下所示:

log4j:WARN Continuable parsing error 61 and column 23
log4j:WARN The content of element type "log4j:configuration" must match "(renderer*,appender*,(category|logger)*,root?,categoryFactory?)".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [std] additivity to [true].
log4j: Level value for std is  [INFO].
log4j: std level set to INFO
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [maxBackupIndex] to [10].
log4j: Setting property [file] to [./target/logs/develop.log].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{ISO8601}|%p|%t|%m%n].
log4j: setFile called: ./target/logs/develop.log, true
log4j: setFile ended
log4j: Adding appender named [regularAppender] to category [std].
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [errorLog] additivity to [true].
log4j: Level value for errorLog is  [INFO].
log4j: errorLog level set to INFO
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [maxBackupIndex] to [10].
log4j: Setting property [file] to [./target/logs/errors.log].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{ISO8601}|%p|%t|%m%n].
log4j: setFile called: ./target/logs/errors.log, true
log4j: setFile ended
log4j: Adding appender named [errorsAppender] to category [errorLog].
log4j: Retreiving an instance of org.apache.log4j.Logger.
....

这是一个命令行应用程序,因此用户在启动时看到所有这些消息看起来不太好。

如何配置log4j以将这些消息写入日志文件而不是屏幕。

谢谢!

1 个答案:

答案 0 :(得分:3)

我有类似的问题。我通过将log4j:configuration标记的属性从debug="true"更改为debug="false"来解决此问题。