Search code examples
javalogginglog4jakkalogback

Using logback to log to two different log files


Using below logback.xml :

<Configuration status="INFO">
    <Appenders>

        <appender name="log1" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>log1.log</file>
            <append>true</append>
        </appender>

        <appender name="log2" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>log2.log</file>
            <append>true</append>
        </appender>

        <logger name="logger1" level="INFO" additivity="true">
            <appender-ref ref="log1"/>
        </logger>
        <logger name="logger2" level="INFO" additivity="true">
            <appender-ref ref="log2"/>
        </logger>

    </Appenders>

    <root level="info">
        <appender-ref ref="logger1"/>
        <appender-ref ref="logger2"/>
    </root>

</Configuration>

with dependencies :

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.2.10</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.32</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.32</version>
</dependency>

I'm attempting to log message to two different files using code :

Logger log1 = LoggerFactory.getLogger("logger1");
Logger log2 = LoggerFactory.getLogger("logger2");

log1.info("test1");
log2.info("test2");

But the files, log1.log and log2.log are not being created.

Here is the console output :

[as-akka.actor.default-dispatcher-3] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
[main] INFO logger1 - test1
[main] INFO logger2 - test2

I'm unsure why the message [as-akka.actor.default-dispatcher-3] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started is being printed, perhaps because as I'm using Akka in the project ?

Have I setup logback.xml correctly to enable logging to two separate log files ?

I have reviewed question Logback to log different messages to two files and it does not answer this question as the configuration differs in this question.


Solution

  • This logback.xml splits the logging into two separate files :

    <?xml version="1.0"?>
    <configuration>
    
        <appender name="logger1appender" class="ch.qos.logback.core.FileAppender">
            <file>logfile1.log</file>
            <append>false</append>
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
        <appender name="logger2appender" class="ch.qos.logback.core.FileAppender">
            <file>logfile2.log</file>
            <append>false</append>
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    
    
        <property name="pattern" value="%date{HH:mm:ss.SSS} %-5p %logger{36}
    %X{akkaSource} [%file:%line] - %m%n"/>
    
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%date{HH:mm:ss.SSS} %-5p %logger{36} %X{akkaSource} [%file:%line] - %m%n</pattern>
            </encoder>
        </appender>
    
        <logger name="logger1name" level="INFO" additivity="false">
            <appender-ref ref="logger1appender"/>
        </logger>
        <logger name="logger2name" level="INFO" additivity="false">
            <appender-ref ref="logger2appender"/>
        </logger>
    
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
        </root>
    

    with dependencies :

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.2.10</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.32</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.10</version>
    </dependency>
    

    using code :

    private static final Logger log1 = LoggerFactory.getLogger("logger1name");
    private static final Logger log2 = LoggerFactory.getLogger("logger2name");