使用系统属性并重新加载配置文件看起来更干净:
更改logback.xml文件:
<file>${log_path:-}myfile.log</file>....<FileNamePattern>${log_path:-}myfile.%i.log</FileNamePattern>这会将默认位置设置为工作目录。然后,使用:
System.setProperty("log_path", my_log_path);//Reload:LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();ContextInitializer ci = new ContextInitializer(lc);lc.reset();try { //I prefer autoConfig() over JoranConfigurator.doConfigure() so I wouldn't need to find the file myself. ci.autoConfig(); } catch (JoranException e) { // StatusPrinter will try to log this e.printStackTrace();}StatusPrinter.printInCaseOfErrorsOrWarnings(lc);


