maven中使用logback时,配置文件不生效

原因主要是因为logback.xml需要放在classpath,而maven中的classpath默认是在target/classes中,把配置文件放在这,每次clean就删除了,显然不对。

解决方法有两种:

1、

官方提供的是

java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1

这种方法指定配置文件位置。

但太麻烦,所以找到:

https://stackoverflow.com/questions/21885787/setting-logback-xml-path-programmatically

You could use:

System.setProperty("logback.configurationFile", "/path/to/config.xml");

But it would have to happen before logback is loaded, i.e. something like:

class Main {
  static { System.setProperty("logback.configurationFile", "/path/to/config.xml");}
  private final Logger LOG = LoggerFactory.getLogger(Main.class);

  public void main (String[] args) { ... }
}

这样就完美解决了。


2、

It looks like you need to add the location of your log4j.properties file or logback config file to the Classpath in Eclipse.

Make sure your project is open in Eclipse, then click on the "Run" menu at the top of Eclipse and click on the following:

  1. Run
  2. Run Configurations
  3. Classpath (tab)
  4. User Entries
  5. Advanced (button on the right)
  6. Add Folders
  7. then navigate to the folder that contains your log4j.properties file
  8. Apply
  9. Run

The error message should no longer appear.


你可能感兴趣的:(Java,maven)