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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值