簡體   English   中英

如何在 Java Spring 引導中更改 log4j2.xml 的默認位置?

[英]How can I change the default location of log4j2.xml in Java Spring Boot?

Log4j2 與 Spring 配合得很好,通過根類路徑中的log4j2.xml配置文件啟動,正如文檔所述。

但是,當嘗試將此文件移動到其他位置時,我無法在啟動時將新位置傳遞給 Spring。 文檔中

可以通過在類路徑中包含適當的庫來激活各種日志系統,並通過在類路徑的根目錄中或在 Spring 環境屬性logging.config指定的位置中提供合適的配置文件來進一步定制。

我嘗試使用 Java 系統屬性設置新位置

java -jar -Dlogging.config="classpath:/config/log4j2.xml" target/app.jar

或使用包含相關屬性的外部application.properties

logging.config=classpath:/config/log4j2.xml

但我經常收到以下錯誤消息。

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

正如Spring 參考文檔中所指定的,無法在應用程序屬性中設置logging.config屬性,因為它們是在日志記錄初始化之后讀取的。

解決方案是以這種方式提供外部日志配置的路徑:

java -Dlogging.config='/path/to/log4j2.xml' -jar app-current.jar

正如此處的 log4j2 文檔中所述,您可以在資源文件夾(或類路徑中的任何位置)中包含一個名為log4j2.component.properties文件,在該文件中,您可以提供文件位置的名稱(或新文件名) 像這樣:

log4j.configurationFile=path/to/log4j2.xml

或者

log4j.configurationFile=classpath:log4j2-custom.xml (if the file is on the classpath)

您也可以通過提供的配置文件位置context-param領域web.xml提到這里,但我還沒有嘗試過該方法

(這也適用於 Spring Boot)

micpalmia的回答是絕對正確的。

我需要將配置放在類路徑之外,我不想將配置文件作為參數傳遞。 因此,我在類路徑資源中放置了一個非常簡單的日志記錄配置,並在啟動時讓 Spring Boot 應用程序重新配置日志記錄,如下所示:

@SpringBootApplication
public class Application implements CommandLineRunner {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... param) throws UnsupportedEncodingException {
        Configurator.initialize(null, "config/log4j2.xml");
        // ...
    }
}

這種方法有一個明顯的缺點:整個應用程序啟動過程不會被記錄為外部配置。 但是一旦自定義代碼運行,記錄器就會按預期工作。 雖然你可能不會,但我發現這是我可以接受的妥協。

我的項目中有同樣的問題,除了 log4j2.xml 我還需要類路徑中的其他配置文件。 這是我的 2 個有效的解決方案:

解決方案 1:使用 org.springframework.boot.loader.JarLauncher 啟動 spring boot 應用程序

java -classpath SpringBootProject.jar;./config org.springframework.boot.loader.JarLauncher

解決方案二:在jar中的MANIFEST.MF中寫入'./config'類路徑項

    <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifestEntries>
               <Class-Path>./config/</Class-Path>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.5.3.RELEASE</version>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

如果是屬性文件:

java -Dlog4j.configuration=file:/path/to/log4j.properties -jar app.jar

命令行在 Spring Boot 2 中有效。不要忘記在路徑之前添加file :。

我有工作解決方案來設置自定義路徑或更改日志文件的現有文件路徑。 如果您已配置 log4j2.xml 文件,請打開它並查看您必須對配置日志文件路徑進行一行更改的位置。 在此處輸入圖片說明

如果您嘗試重命名 log4j2.xml 文件而不是默認名稱。 為了加載文件。 對於 application.properties 文件中的 spring 引導應用程序,添加以下屬性,這將起作用

logging.config=classpath:logfilename.xml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM