log4j - how to log to seperate/different files

2020-03-31 03:37发布

问题:

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> 

files are created in log directory, but loggin only performed in mainWS.log

I've tried

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());
            }
        }

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

it seems that only debugLogger has an appender.

Any ideas?

回答1:

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.

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>

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

now it works



回答2:

My first guess is that log4j reads a different config file than you expect. Check the debug output of log4j when it starts and especially the paths in there.



标签: jboss log4j