内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

怎么對(duì)logback.xml配置文件在resource外的位置進(jìn)行修改-創(chuàng)新互聯(lián)

怎么對(duì)logback.xml配置文件在resource外的位置進(jìn)行修改?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供陸港企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為陸港眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。

根據(jù)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);
 }

可以看出是根據(jù)順序依次往下尋找配置文件位置,在該類的屬性中定義了對(duì)應(yīng)的變量值如下

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";

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

那么我們想修改配置文件位置,只需找到第一個(gè)加載的方法中是如何加載的,因?yàn)楹竺婕虞d文件的位置都是代碼默認(rèn)寫死了的

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

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

點(diǎn)進(jìn)去之后可以看到

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

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

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

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

所以我們只要在系統(tǒng)啟動(dòng)時(shí),設(shè)置一個(gè)System.setProperty()就可以了,如下

這一步設(shè)置配置文件路徑

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

就是根據(jù)代碼里定義的key,傳一個(gè)文件路徑的value到System的Peoperty里

提示:此方式與@Slf4j一起使用時(shí),在設(shè)置 System.setProperty()代碼塊之前就加載的類中不適用,因?yàn)榇藭r(shí)還未設(shè)置文件位置,但是靜態(tài)常量就已經(jīng)被加載賦值了,比如下面我在main方法中這么定義,main方法中的日志將失去配置文件效果

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

因?yàn)槿绻诙x時(shí)就賦值,那么jvm是先加載 靜態(tài)屬性,然后在執(zhí)行靜態(tài)代碼塊的,所以導(dǎo)致System.setProperty()賦值在 log變量賦值以后執(zhí)行,那么設(shè)置的文件位置也就不生效了

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

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

所以我們可以選擇下面這中jvm啟動(dòng)時(shí)帶的參數(shù)

怎么對(duì)logback.xml配置文件在resource外的位置進(jìn)行修改

打包后的控制臺(tái)啟動(dòng)設(shè)置參數(shù)可以百度一下,下面是我打包后控制臺(tái)啟動(dòng)的參數(shù)設(shè)置例子

怎么對(duì)logback.xml配置文件在resource外的位置進(jìn)行修改

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

補(bǔ)充:springboot打包去除資源文件,啟動(dòng)時(shí)指定配置文件位置,使用log4j2替換默認(rèn)logback

springboot打包時(shí),去掉資源文件

<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中啟動(dòng)springboot項(xiàng)目,則會(huì)出現(xiàn)讀取不到配置資源的情況,所以在eclipse啟動(dòng)項(xiàng)目時(shí),需要注釋掉如下配置

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

如果修改了pom文件之后,程序運(yùn)行異常,如果使用eclipse,則可通過(guò)右鍵Maven -- Update Project更新下maven依賴,再次啟動(dòng)服務(wù)

啟動(dòng)時(shí)指定配置文件位置

項(xiàng)目打成jar包后,運(yùn)行時(shí),可將配置文件放入jar包同級(jí)目錄下或者在同級(jí)的config目錄下(放入classpath下或者classpath下config目錄下也可以,但是打成jar包,就需要一起打包出來(lái))

配置文件加載順序?yàn)椋?/p>

jar包同級(jí)目錄下的config文件夾下配置

jar包同級(jí)目錄下配置

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方式啟動(dòng)

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

使用log4j2替換默認(rèn)日志框架logback

添加log4j2日志框架依賴

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

此時(shí)啟動(dòng)服務(wù)時(shí),將會(huì)有如下提示

怎么對(duì)logback.xml配置文件在resource外的位置進(jìn)行修改

從日志記錄看,依然使用的為logback日志,網(wǎng)上搜索了一些資料得知,需要排除掉默認(rèn)的日志框架才會(huì)生效

本例使用了兩個(gè)spring-boot-starter-data-redis和spring-boot-starter-jdbc依賴,且它們也都有日志框架的依賴,排除默認(rèn)框架時(shí),只需將寫在最前面的starter依賴中添加排除默認(rèn)日志框架的代碼即可

<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>

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

新聞標(biāo)題:怎么對(duì)logback.xml配置文件在resource外的位置進(jìn)行修改-創(chuàng)新互聯(lián)
本文來(lái)源:http://www.rwnh.cn/article24/ccegce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、軟件開發(fā)、ChatGPT云服務(wù)器、虛擬主機(jī)建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站
剑阁县| 黄陵县| 婺源县| 柯坪县| 东丽区| 青田县| 兴安盟| 林甸县| 普兰店市| 本溪| 新乐市| 武清区| 张家界市| 沿河| 丘北县| 沽源县| 四会市| 尼玛县| 正定县| 二连浩特市| 沁源县| 监利县| 西城区| 哈尔滨市| 汤原县| 纳雍县| 唐海县| 胶州市| 水富县| 靖江市| 东台市| 额济纳旗| 万安县| 娄烦县| 房山区| 霍州市| 洛隆县| 长泰县| 泰宁县| 巧家县| 秀山|