如何修改logback.xml配置文件在resource以外的位置

因為spring搭建的web項目打包時默認將resource文件夾下的文件一起打包,但是我們又想在打包後修改某些配置

這裡以我遇到的logback.xml配置更改默認路徑來看

根據LoggerFactory.getLogger的方法找到加載文件的位置,如下

 public URL findURLOfDefaultConfigurationFile(boolean updateStatus) {
 ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this);
 URL url = findConfigFileURLFromSystemProperties(myClassLoader, updateStatus);
 if (url != null) {
  return url;
 }
 url = getResource(TEST_AUTOCONFIG_FILE, myClassLoader, updateStatus);
 if (url != null) {
  return url;
 }
 url = getResource(GROOVY_AUTOCONFIG_FILE, myClassLoader, updateStatus);
 if (url != null) {
  return url;
 }
 return getResource(AUTOCONFIG_FILE, myClassLoader, updateStatus);
 }

可以看出是根據順序依次往下尋找配置文件位置,在該類的屬性中定義瞭對應的變量值如下

public class ContextInitializer {
 final public static String GROOVY_AUTOCONFIG_FILE = "logback.groovy";
 final public static String AUTOCONFIG_FILE = "logback.xml";
 final public static String TEST_AUTOCONFIG_FILE = "logback-test.xml";
 final public static String CONFIG_FILE_PROPERTY = "logback.configurationFile";

具體加載順序參照靜態變量上面代碼塊的查找順序

那麼我們想修改配置文件位置,隻需找到第一個加載的方法中是如何加載的,因為後面加載文件的位置都是代碼默認寫死瞭的

可以看到上面代碼塊中,最先加載的代碼塊是

 URL url = findConfigFileURLFromSystemProperties(myClassLoader, updateStatus);
 if (url != null) {
  return url;
 }

點進去之後可以看到

private URL findConfigFileURLFromSystemProperties(ClassLoader classLoader, boolean updateStatus) {
 String logbackConfigFile = OptionHelper.getSystemProperty(CONFIG_FILE_PROPERTY);

這裡可以看到OptionHelper.getSystemProperty(CONFIG_FILE_PROPERTY)傳入的是靜態變量中的

final public static String CONFIG_FILE_PROPERTY = "logback.configurationFile";</div>
<div>OptionHelper.getSystemProperty中的內容是</div>
 public static String getSystemProperty(String key) {
 try {
  return System.getProperty(key);
 } catch (SecurityException e) {
  return null;
 }
 }

可以看出是從 System.getProperty()中獲取的,而key是靜態變量。

所以我們隻要在系統啟動時,設置一個System.setProperty()就可以瞭,如下

這一步設置配置文件路徑

private static final Logger log;
 static {
 System.setProperty("logback.configurationFile","./logback.xml");
 log = LoggerFactory.getLogger(MonitorApplication.class);
 }

就是根據代碼裡定義的key,傳一個文件路徑的value到System的Peoperty裡

提示:此方式與@Slf4j一起使用時,在設置 System.setProperty()代碼塊之前就加載的類中不適用,因為此時還未設置文件位置,但是靜態常量就已經被加載賦值瞭,比如下面我在main方法中這麼定義,main方法中的日志將失去配置文件效果

private static final Logger log = LoggerFactory.getLogger(MonitorApplication.class);;
 static {
 System.setProperty("logback.configurationFile","./logback.xml");
 }
 public static void main(String[] args) {}

因為如果在定義時就賦值,那麼jvm是先加載 靜態屬性,然後在執行靜態代碼塊的,所以導致System.setProperty()賦值在 log變量賦值以後執行,那麼設置的文件位置也就不生效瞭

@Slf4j註解也一樣,@Slf4j註解後生成的class是下面這樣的:

public class MonitorApplication {
 private static final Logger log = LoggerFactory.getLogger(MonitorApplication.class);

所以我們可以選擇下面這中jvm啟動時帶的參數

打包後的控制臺啟動設置參數可以百度一下,下面是我打包後控制臺啟動的參數設置例子

java -Dlogback.configurationFile=./logback.xml -jar monitor-1.0-SNAPSHOT.jar

補充:springboot打包去除資源文件,啟動時指定配置文件位置,使用log4j2替換默認logback

springboot打包時,去掉資源文件

<build>
 <resources>
  <resource>
  <directory>src/main/resources</directory>
  <excludes>
   <exclude>*.properties</exclude>
   <exclude>*.xml</exclude>
  </excludes>
  </resource>
 </resources>
 <plugins>
  <plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  </plugin>
  
  <plugin>
  <groupId>org.apache.maven.plugins</groupId> 
  <artifactId>maven-surefire-plugin</artifactId> 
  <configuration> 
   <skipTests>true</skipTests> 
  </configuration>
  </plugin>
 </plugins>
 </build>

但這樣配置後,在eclipse中啟動springboot項目,則會出現讀取不到配置資源的情況,所以在eclipse啟動項目時,需要註釋掉如下配置

<resources>
 <resource>
  <directory>src/main/resources</directory>
  <excludes>
   <exclude>*.properties</exclude>
   <exclude>*.xml</exclude>
  </excludes>
 </resource>
</resources> 

如果修改瞭pom文件之後,程序運行異常,如果使用eclipse,則可通過右鍵Maven — Update Project更新下maven依賴,再次啟動服務

啟動時指定配置文件位置

項目打成jar包後,運行時,可將配置文件放入jar包同級目錄下或者在同級的config目錄下(放入classpath下或者classpath下config目錄下也可以,但是打成jar包,就需要一起打包出來)

配置文件加載順序為:

jar包同級目錄下的config文件夾下配置

jar包同級目錄下配置

classpath下config目錄下配置

classpath下配置

java -Xms100m -Xmx100m -jar myboot001-0.0.1-SNAPSHOT.jar &

也可指定加載配置文件的地址

java -Xms100m -Xmx100m -jar myboot001-0.0.1-SNAPSHOT.jar --spring.config.location=configs/application.properties --logging.config=./log4j2.xml >> /dev/null 2>&1 &

以DEBUG方式啟動

java -Xms100m -Xmx100m -jar myboot001-0.0.1-SNAPSHOT.jar --spring.config.location=configs/application.properties --debug

使用log4j2替換默認日志框架logback

添加log4j2日志框架依賴

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

此時啟動服務時,將會有如下提示

從日志記錄看,依然使用的為logback日志,網上搜索瞭一些資料得知,需要排除掉默認的日志框架才會生效

本例使用瞭兩個spring-boot-starter-data-redis和spring-boot-starter-jdbc依賴,且它們也都有日志框架的依賴,排除默認框架時,隻需將寫在最前面的starter依賴中添加排除默認日志框架的代碼即可

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-redis</artifactId>
 <exclusions>
  <exclusion> 
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-logging</artifactId>
  </exclusion>
 </exclusions>
</dependency>
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

或者添加如下依賴處理(位置不限)

 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter</artifactId>
 <exclusions>
  <exclusion>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-logging</artifactId>
  </exclusion>
 </exclusions>
 </dependency>

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: