【发布时间】:2021-07-13 19:23:45
【问题描述】:
我的 Spring Boot 应用程序部署在外部 Tomcat 9 服务器上时遇到了日志记录问题。 应用程序正在将日志写入 catalina.out 文件,而不是 log4j2.properties 中提到的文件。
请帮助确定我缺少什么。 下面是代码。
log4j2.properties
status = debug
name = PropertiesConfig
property.dir = /var/log
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = LogToRollingFile
appender.rolling.fileName = ${dir}/bt4test.log
appender.rolling.filePattern = ${dir}/bt4test-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=1000MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = fileLogger
#### Class/Package Level logging control
logger.fileLogger.name = com.example
logger.fileLogger.level = debug
logger.fileLogger.additivity = false
logger.fileLogger.appenderRefs = rolling
logger.fileLogger.appenderRef.rolling.ref = LogToRollingFile
rootLogger.level = debug
rootLogger.appenderRefs = rolling
rootLogger.appenderRef.rolling.ref = LogToRollingFile
Spring Boot 初始化类
@SpringBootApplication
public class ServletInitializer extends SpringBootServletInitializer {
public static final Logger logger = LogManager.getLogger(ServletInitializer.class.getName());
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
logger.info("ServletInitializer");
return application.sources(ServletInitializer.class);
}
}
休息控制器类
@RestController
@ComponentScan(
basePackages = {
"com.example.*"
}
)
public class Bt4TestEndpoint {
private static final Logger logger = LogManager.getLogger(Bt4TestEndpoint.class.getName());
static {
logger.info("hello static");
}
@GetMapping(value = "/hello")
public String hello() {
logger.info("hello");
return "hello";
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>BT4test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>BT4test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>bt4test</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.ServletInitializer</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
-
如果从 IDE 或 shell 运行它会发生什么?日志写在哪里?
标签: spring-boot log4j2 tomcat9