【问题标题】:Spring Boot Logs directed to Tomcat Catalina.out log定向到 Tomcat Catalina.out 日志的 Spring Boot 日志
【发布时间】: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


【解决方案1】:

在 tomcat 应用服务器中配置 log4j2 相关的 jar 和 log4j2.xml,并通过带有相对路径的 setenv.sh 文件使它们可用。

一旦环境可以使用 Jar 和 xml,它将根据配置开始写入。

例如:

setenv.sh

导出 CLASSPATH="$CLASSPATH:$CATALINA_HOME/log4j2/lib/*:$CATALINA_HOME/log4j2/conf/"

  • 请注意,lib 文件夹包含 log4j2 的所有库,而 Configurations 文件夹包含 log4j2.xml。此外,在 Catalina 主目录下创建 log4j2 文件夹。

使用 log4j-jul 中的日志管理器

LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"

希望这有助于解决您的问题。

【讨论】:

  • 您能否提供代码 sn-ps 以更好地解释您的解决方案?
  • 更新了帖子。干杯!
【解决方案2】:

当 Spring Boot 初始化 Log4j 2.x 时,它使用自己的配置文件查找列表:有关列表,请参见 Log4jLoggingSystem#getCurrentlySupportedConfigLocations()。虽然在 OP 的情况下,文件列表通常大于 Log4j 2.x(log4j2-test.*log4j2.*log4j2-spring.*)使用的文件列表,但未检测到 *.properties 文件。

升级到 Spring Boot 2.1.4 或更高版本,包括this commit 应该可以解决问题。

【讨论】:

    猜你喜欢
    • 2014-06-29
    • 2018-06-27
    • 2019-09-11
    • 1970-01-01
    • 2016-05-07
    • 2020-03-30
    • 2018-03-15
    • 2017-09-25
    • 1970-01-01
    相关资源
    最近更新 更多