問題描述
Log4j2 未在 Linux 中使用 Spring Boot 創建日誌文件 (Log4j2 not creating log file with Spring Boot in Linux)
我正在使用 Log4j2 通過 Spring Boot 進行日誌記錄,但它沒有創建日誌文件。下面給出的是我對 Log4j2 的配置和我添加的依賴項。
Log4j2 配置 ‑
<?xml version="1.0" encoding="UTF‑8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%‑5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
</Console>
<!‑‑ File Appender ‑‑>
<File name="File" fileName=".logs/app.log">
<PatternLayout pattern="%d{yyyy‑MMM‑dd HH:mm:ss a} [%t] %‑5level %logger{36} ‑ %msg%n" />
</File>
</Appenders>
<Loggers>
<!‑‑ LOG everything at INFO level ‑‑>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="File" />
</Root>
<!‑‑ LOG "com.baeldung*" at TRACE level ‑‑>
<Logger name="com.ams" level="trace">
<AppenderRef ref="File" />
</Logger>
</Loggers>
</Configuration>
Pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‑boot‑starter‑web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‑boot‑starter‑logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!‑‑ Add Log4j2 Dependency ‑‑>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‑boot‑starter‑log4j2</artifactId>
</dependency>
我需要任何其他配置或依賴項添加 ?因為根據 web 上的博客 log4j2 應該使用上面提供的配置創建日誌文件。
參考解法
方法 1:
Maybe this link will help you. In my case i had to exclude the spring boot startet logging in the pom xml because otherwise the default logging logback is active and log4j won't be used.
<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>
(by Jainesh kumar、FishingIsLife)