如何为Log4J使用两个不同的XML配置文件

时间:2015-01-29 10:19:43

标签: java log4j

我有两个XML配置文件。一个用于我的Java Jersey WebService,一个用于我的数据访问对象(= DAO; MySQL-DB通过JDBC)。当我运行我的应用程序时,日志分成两个不同的文件夹,但日志文件彼此相同。

我该如何避免?

DAO的XML配置:

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

    <!-- CONSOLE -->    
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <!-- ERROR LOG INTO FILE -->
    <appender name="dberr" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="true" />
        <param name="maxFileSize" value="5MB" />
        <param name="maxBackupIndex" value="5" />
        <param name="file" value="logs/database/errlog.log" />
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="ERROR" />
            <param name="LevelMax" value="FATAL" />
        </filter>
    </appender>

    <!-- STANDARD LOG INTO FILE -->
    <appender name="dbstd" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="true" />
        <param name="maxFileSize" value="5MB" />
        <param name="maxBackupIndex" value="5" />
        <param name="file" value="logs/database/std.log" />
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="INFO" />
            <param name="LevelMax" value="WARN" />
        </filter>
    </appender>

    <!-- DEBUG LOG INTO FILE -->
    <appender name="dbdebug" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="true" />
        <param name="maxFileSize" value="5MB" />
        <param name="maxBackupIndex" value="5" />
        <param name="file" value="logs/database/debug.log" />
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG" />
            <param name="LevelMax" value="DEBUG" />
        </filter>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="dberr" />
        <appender-ref ref="dbstd" />
        <appender-ref ref="dbdebug" />
    </root>

</log4j:configuration>

WebService的XML配置:

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

    <!-- CONSOLE -->    
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <!-- ERROR LOG INTO FILE -->
    <appender name="wserr" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="true" />
        <param name="maxFileSize" value="5MB" />
        <param name="maxBackupIndex" value="5" />
        <param name="file" value="logs/webservice/errlog.log" />
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="ERROR" />
            <param name="LevelMax" value="FATAL" />
        </filter>
    </appender>

    <!-- STANDARD LOG INTO FILE -->
    <appender name="wsstd" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="true" />
        <param name="maxFileSize" value="5MB" />
        <param name="maxBackupIndex" value="5" />
        <param name="file" value="logs/webservice/std.log" />
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="INFO" />
            <param name="LevelMax" value="WARN" />
        </filter>
    </appender>

    <!-- DEBUG LOG INTO FILE -->
    <appender name="wsdebug" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="true" />
        <param name="maxFileSize" value="5MB" />
        <param name="maxBackupIndex" value="5" />
        <param name="file" value="logs/webservice/debug.log" />
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG" />
            <param name="LevelMax" value="DEBUG" />
        </filter>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="wserr" />
        <appender-ref ref="wsstd" />
        <appender-ref ref="wsdebug" />
    </root>

</log4j:configuration>

这就是我调用Log4J的方式:

Logger log  =   Logger.getLogger(DAO.class);
DOMConfigurator.configure("C:\file\path\to\config.xml");

我希望日志文件包含来自DAO的日志olny在一个文件夹中,以及WebService的等效文件。什么是坏事?

1 个答案:

答案 0 :(得分:0)

我将这两个文件合并为一个XML文件。我需要做的是:

    <logger name="webservice_container">
        <appender-ref ref="wserr" />
        <appender-ref ref="wsstd" />
    </logger>

    <logger name="db_container">
        <appender-ref ref="dberr" />
        <appender-ref ref="dbstd" />
    </logger>

    <root>
        <level value="DEBUG" />
    </root>

现在我有一个单独的文件,我有Log4J的所有配置,可以登录2个不同的文件夹和4个不同的文件。