log4j - 如何记录单独/不同的文件

时间:2014-11-13 08:32:46

标签: jboss log4j

我的应用程序在jboss 7.3上运行。 Log4j配置文件看起来

<log4j:configuration debug="true"
                 xmlns:log4j='http://jakarta.apache.org/log4j/'>


<appender name="fileAppender" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="encoding" value="UTF-8" />
    <param name="append" value="true" />
    <param name="file" value="${jboss.server.log.dir}/mainWS.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <param name="Threshold" value="DEBUG"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p %d{yyyy-MM-dd HH:mm:ss:SSS} %m rid%X{rid}%n" />
    </layout>
</appender>

<appender name="File1" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="encoding" value="UTF-8" />
    <param name="append" value="true" />
    <param name="file" value="${jboss.server.log.dir}/file1.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <param name="Threshold" value="DEBUG"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p %d{yyyy-MM-dd HH:mm:ss:SSS} %m rid%X{rid}%n" />
    </layout>
</appender>

<appender name="File2" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="encoding" value="UTF-8" />
    <param name="append" value="true" />
    <param name="file" value="${jboss.server.log.dir}/file2.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <param name="Threshold" value="DEBUG"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p %d{yyyy-MM-dd HH:mm:ss:SSS} %m rid%X{rid}%n" />
    </layout>
</appender>

<appender name="opTimeFile" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="encoding" value="UTF-8" />
    <param name="append" value="true" />
    <param name="file" value="${jboss.server.log.dir}/opTime.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <param name="Threshold" value="DEBUG"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p %d{yyyy-MM-dd HH:mm:ss:SSS} %m rid%X{rid}%n" />
    </layout>
</appender>

<logger name="debugLogger" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="fileAppender" />
</logger>

<logger name="Logger1" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="File1" />
</logger>

<logger name="Logger2" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="File2" />
</logger>

<logger name="opTimeLogger" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="opTimeFile" />
</logger>

<root>
    <priority value="OFF" />    
</root> 

文件在日志目录中创建,但loggin仅在mainWS.log中执行

我试过

private static final Logger log = Logger.getLogger("debugLogger");
private static final Logger vLog = Logger.getLogger("Logger1");
private static final Logger eLog = Logger.getLogger("Logger2");
private static final Logger tLog = Logger.getLogger("opTimeLogger");

for (Enumeration loggers= LogManager.getCurrentLoggers(); loggers.hasMoreElements(); )  {
            Logger logger = (Logger) loggers.nextElement();
            log.debug("logger - " + logger.getName());
            vLog.debug("vLogger - " + logger.getName());
            eLog.debug("eLogger - " + logger.getName());
            tLog.debug("tLogger - " + logger.getName());
            for (Enumeration appenders=logger.getAllAppenders(); appenders.hasMoreElements(); )  {
                Appender appender = (Appender) appenders.nextElement();
                log.debug("appender - " + appender.getName());
                vLog.debug("vAppender - " + appender.getName());
                eLog.debug("eAppender - " + appender.getName());
                tLog.debug("tAppender - " + appender.getName());
            }
        }

但结果仅在mainWS.log文件中

DEBUG 2014-11-13 10:29:03:125 logger -  rid
DEBUG 2014-11-13 10:29:03:138 logger - opTimeLogger rid
DEBUG 2014-11-13 10:29:03:138 logger - Logger1 rid
DEBUG 2014-11-13 10:29:03:138 logger - debugLogger rid
DEBUG 2014-11-13 10:29:03:138 appender - fileAppender rid
DEBUG 2014-11-13 10:29:03:139 logger - Logger2 rid

似乎只有debugLogger有一个appender。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我猜log4j与应用服务器的默认日志记录实现之间存在冲突。我不得不从部署中排除该模块。

我添加了一个WEB-INF / jboss-deployment-structure.xml文件

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
    <!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
    <exclusions>
        <module name="org.apache.log4j" />
    </exclusions>
</deployment>

我还必须在我的应用程序类路径中包含log4j库。

现在可行了

答案 1 :(得分:-1)

我的第一个猜测是log4j读取的文件与您预期的不同。检查log4j启动时的调试输出,特别是那里的路径。