Search code examples
javalogginglogbacklog-level

simple application with only main class doesn't see logback.xml


My project structure:

enter image description here

As you can see I tried to put logback.xml everywhere.

I want to setup info log level for root logger.

logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>

    <!-- Send logs to both console and file audit -->
    <logger name="kafka_test" level="info">
        <appender-ref ref="STDOUT" />
    </logger>


</configuration>

I have following code:

public class Consumer {
    private static final String name = "consumer_1";

    private static Logger logger = LoggerFactory.getLogger(Consumer.class);

    public static void main(String[] args) throws InterruptedException {
        logger.debug("Consumer {} started", name);
        ...

And it prints

17:51:45.096 [main] DEBUG kafka_test.Consumer - Consumer consumer_1 started

into console.

What do I wrong?

P.S.

I tried to print:

    System.out.println(Consumer.class.getClassLoader().getResource("kafka_test/Consumer.class"));

It prints:

file:/D:/work/kafka_samples/out/production/classes/kafka_test/Consumer.class

I put logback.xml into

D:\work\kafka_samples\out\production\classes\kafka_test

But it still doesn't work

Solution:

I've put logback.xml into

D:\work\kafka_samples\out\production\classes

Solution

  • You need to put it at the top-level of your classpath. The standard location for a maven/gradle project would be in src/main/resources. Gradle/maven will include that resources directory on your classpath (and most IDEs will understand that too).