我知道之前已经问过这个问题,但是我仍然无法让log4j2写入文件(甚至没有创建文件)。但是它会在控制台上显示。
我已将log4j2.xml放在src / main / resources中:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="error.log" immediateFlush="false"
append="false">
<PatternLayout
pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile" />
</Root>
</Loggers>
</Configuration>
我的主要方法,它调用日志记录功能
@SpringBootApplication
public class SpringApplication1 {
public static final Logger logger = LogManager.getLogger(SpringApplication1.class);
public static void main(String[] args) {
SpringApplication.run(SpringApplication1.class, args);
logger.info("testing info");
logger.error("this is an error");
}
我的pom.xml中有这两个依赖项
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.0</version>
</dependency>
答案 0 :(得分:1)
spring-jcl
需要在类路径上。
Spring Boot的默认日志记录机制是logback。
一种方法是使用Spring Boot Log4j starter。要使用log4j,应排除logback:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>