0

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.

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 3
    Your configuration file looks like a mixup of Log4j and Logback syntax (i.e. is not valid). Did you try adapting the configuration from the question you cite? Besides you use the `slf4j-simple` binding, which does not use Logback. On the other hand `logback-classic` (which contains Logback's SLF4J binding) is missing. – Piotr P. Karwasz Jan 02 '22 at 21:22
  • 1
    Enable logback [debug](https://stackoverflow.com/a/3802093/4803842) option to see how logback is loading – Ivan Stanislavciuc Jan 02 '22 at 21:24

2 Answers2

1

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");
blue-sky
  • 51,962
  • 152
  • 427
  • 752
-2

This result from Google has answers to all your questions...

I'd expect, that you know, that <Configuration> and <configuration> is not the same thing as well as console output is somehow unrelated, when you are logging into file(s)...

Betlista
  • 10,327
  • 13
  • 69
  • 110