You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止Spring Boot应用日志输出到Tomcat的catalina.out

解决Spring Boot WAR部署Tomcat时日志同时输出到catalina.out和自定义路径的问题

问题分析

核心矛盾点:

  1. 排除spring-boot-starter-logging后,Log4j2未能完全接管所有日志,导致自定义日志文件无输出,而HikariCP、JPA等框架的日志直接通过System.out输出到catalina.out
  2. 修改catalina.sh会影响Tomcat上的其他应用,必须从应用内部实现日志分流

解决方案

1. 修正Maven依赖配置

spring-boot-starter-log4j2已包含log4j-core和log4j-api,无需重复引入。同时要确保所有Spring Boot Starter都排除默认的spring-boot-starter-logging,避免日志框架冲突:

<dependencies>
    <!-- Web Starter排除默认日志 -->
    <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>

    <!-- JPA Starter排除默认日志 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Security Starter排除默认日志 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- 引入Log4j2 Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

    <!-- 移除重复的log4j-core和log4j-api依赖 -->
</dependencies>

2. 调整Log4j2配置,捕获System.out/System.err

Tomcat会将应用的System.out和System.err重定向到catalina.out,需要让Log4j2接管这些输出,在log4j2.xml的<Loggers>节点添加以下配置:

<Loggers>
    <!-- 业务代码日志配置 -->
    <Logger name="com.xxxx.yyyyy" level="debug" additivity="false">
        <AppenderRef ref="RollingFile"/>
    </Logger>

    <!-- 捕获System.out输出,路由到自定义日志文件 -->
    <Logger name="System.out" level="info" additivity="false">
        <AppenderRef ref="RollingFile"/>
    </Logger>

    <!-- 捕获System.err输出,路由到自定义日志文件 -->
    <Logger name="System.err" level="error" additivity="false">
        <AppenderRef ref="RollingFile"/>
    </Logger>

    <Root level="info">
        <AppenderRef ref="RollingFile"/>
    </Root>
</Loggers>

3. 移除冲突的Spring Boot日志配置

application.properties中的logging.file.path和logging.file.name是Spring Boot默认日志框架的配置,会与Log4j2配置冲突,直接删除这两行:

# 移除以下两行
#logging.file.path=/var/log/restapi/
#logging.file.name=/var/log/restapi/restapi.log

4. 确保Log4j2配置文件位置正确

将log4j2.xml放在src/main/resources目录下,打包后会自动进入WAR包的WEB-INF/classes目录,Tomcat启动时能正确加载该配置。

5. 验证配置效果

重新打包WAR并部署到Tomcat:

  • 检查自定义日志路径/var/log/restapi/restapi.log是否有正常日志输出
  • 查看catalina.out,确认不再有应用相关的日志内容

错误原因说明

排除spring-boot-starter-logging后,HikariCP、JPA等依赖组件的日志会直接输出到System.out,而原Log4j2配置未捕获这些输出;同时重复引入log4j-core/log4j-api可能导致版本冲突,最终导致自定义日志文件无输出,仅框架关闭日志通过System.out进入catalina.out。

内容的提问来源于stack exchange,提问作者Manoj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 15:29:55