本文介绍了log4j-如何记录分开/不同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在jboss 7.3上运行. Log4j配置文件的外观

my application is runnig on jboss 7.3. Log4j configuration file looks

<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中执行

files are created in log directory, but loggin only performed in 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文件中

but result is only in mainWS.log file

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具有附加程序.

it seems that only debugLogger has an appender.

有什么想法吗?

推荐答案

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

I guess there was a conflict between log4j and the default logging implementation of the application server. I had to exclude the module from your deployment.

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

I added a WEB-INF/jboss-deployment-structure.xml file

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

I had to include log4j libraries in my application classpath as well.

现在可以使用

这篇关于log4j-如何记录分开/不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 18:07