【发布时间】:2021-04-24 14:14:13
【问题描述】:
我正在使用 Log4j2 通过 Spring Boot 进行日志记录,但它没有创建日志文件。下面给出的是我对 Log4j2 的配置和我添加的依赖项。我尝试了所有可能的解决方案。 Log4j2 配置 -
Configuration:
name: Default
Properties:
Property:
name: log-path
value: "logs"
Appenders:
Console:
name: Console_Appender
target: SYSTEM_OUT
PatternLayout:
pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
File:
name: File_Appender
fileName: ${log-path}/logfile.log
PatternLayout:
pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
Loggers:
Root:
level: debug
AppenderRef:
- ref: Console_Appender
Logger:
- name: com.example
level: debug
AppenderRef:
- ref: File_Appender
level: error
build.gradle 文件中使用的依赖项:
configurations {
all*.exclude group:'org.springframework.boot', module: 'spring-boot-starter-logging'
all*.exclude module: 'logback-classic'
compileOnly {
extendsFrom annotationProcessor
}
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-log4j2'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
}
申请文件:
@SpringBootApplication
public class DemoApplication {
private static final Logger logger = LogManager.getLogger(DemoApplication.class);
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
logger.fatal("This is a fatal message");
}
}
启动应用程序文件后登录控制台但未生成文件。
【问题讨论】:
标签: spring-boot logging log4j2