Configuring Logback in Eclipse

2019-06-16 10:39发布

问题:

I'm in the process of switching from Log4j to Logback but I'm not having success at making Logback work yet. I have placed logback.xml in the root directory of my Eclipse Java project and below is its content:

<configuration>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>myApp.log</file>

    <encoder>
      <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

And below is the relevant content of my Main.java:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main
{
  static final Logger logger = LoggerFactory.getLogger(Main.class);

  public static void main(String[] args)
  {
    logger.info("Main started");
  }
}

This does not seem to be working as no file named myApp.log is created in the root of my Eclipse Java application. Any idea what I'm doing wrong?

回答1:

The configuration file needs to be on the classpath. My guess is that it isn't. Check the build path for the project.