Csaba Csaba - 3 months ago 10
Java Question

Using Log4J 1.*, how can I write two packages to two separate files?

I have the following two packages:

com.mycorp.project.first

com.mycorp.project.second

I'd like to configure Log4J (SLF4J) to write the logs from one package to one file, and from the other package to a second file. I do not want them to be mixed in together.

To be clear, this is one project/one process running.

I've tried filtering and with loggers but they seem to be ignored by log4j. Both files are always identical.

Edit: Thank you for the answers so far, this is what I've got and it's not working. Both output files are identical.

<configuration debug="true">
<contextName>dev</contextName>

<appender name="FIRST_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>A:/dev/LogTesting/logs/first.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>A:/dev/LogTesting/logs/first.%d{yyyyMMdd}%d{_HHmmss,aux}.log.gz</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{MMM dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<appender name="SECOND_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>A:/dev/LogTesting/logs/second.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>A:/dev/LogTesting/logs/second.%d{yyyyMMdd}%d{_HHmmss,aux}.log.gz</fileNamePattern>
</rollingPolicy>
<encoder>
<Pattern>%d{MMM dd HH:mm:ss.SSS} %property{HOSTNAME} [%thread] %level %logger{36} %msg%n</Pattern>
</encoder>
</appender>

<!-- =============================================================== -->
<logger name="com.test.apples" additivity="false">
<level value="DEBUG" />
<appender-ref ref="FIRST_FILE" />
</logger>

<logger name="com.test.oranges" additivity="false">
<level value="DEBUG" />
<appender-ref ref="SECOND_FILE" />
</logger>
<!-- =============================================================== -->

<category name="com.test.apples" additivity="false">
<priority value="DEBUG"/>
<appender-ref ref="FIRST_FILE"/>
</category>

<category name="com.test.oranges" additivity="false">
<priority value="DEBUG"/>
<appender-ref ref="SECOND_FILE"/>
</category>

<!-- =============================================================== -->

<!-- default -->
<root level="WARN">
<appender-ref ref="FIRST_FILE" />
<appender-ref ref="SECOND_FILE" />
</root>



Answer

Try this is the example of properties file:

log4j.rootLogger=DEBUG, CONSOLE
# Each package has different appender name     
log4j.logger.com.mycorp.project.first=DEBUG, FIRST
log4j.logger.com.mycorp.project.second=DEBUG, SECOND

log4j.appender.FIRST=org.apache.log4j.RollingFileAppender
log4j.appender.FIRST.File=./first.log
log4j.appender.FIRST.layout=org.apache.log4j.PatternLayout

log4j.appender.SECOND=org.apache.log4j.RollingFileAppender
log4j.appender.SECOND.File=./second.log
log4j.appender.SECOND.layout=org.apache.log4j.PatternLayout

or this for XML (only highlight important part):

<!-- FIRST appender definition -->
<appender name="FIRST" class="org.apache.log4j.FileAppender">
    ...
</appender> 

<!-- SECOND appender definition -->
<appender name="SECOND" class="org.apache.log4j.FileAppender">
    ...
</appender>

<!-- logger package com.mycorp.project.first -->
<logger name="com.mycorp.project.first">
    <level value="DEBUG" />
    <appender-ref ref="FIRST"/>
</logger>

<!-- logger for package com.mycorp.project.second -->
<logger name="com.mycorp.project.second">
    <level value="DEBUG" />
    <appender-ref ref="SECOND"/>
</logger>