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

基于Maven的Spring Boot项目:无log4j.properties时Log4j日志仅输出至控制台如何处理?

Fixing Log4j Console-Only Output in Spring Boot Maven Project

Hey there! The behavior you're seeing is totally expected—Log4j defaults to console-only output when it can’t find a configuration file. Let’s fix this step by step to get your logs routed to files (or other targets) as needed:

Step 1: Create a Log4j Configuration File

First, add a log4j.properties file to your Maven project’s src/main/resources directory. Spring Boot automatically scans this folder for configuration files, so Log4j will pick it up once it’s placed here.

Step 2: Add Practical Configuration to the File

Here’s a sample configuration that sends logs to both the console and a rolling file (to avoid oversized log files). You can tweak this based on your specific needs:

# Root logger level and attached appenders
log4j.rootLogger=DEBUG, stdout, file

# Console appender setup
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Custom log format: timestamp, log level, class, line number, message
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Rolling file appender setup
log4j.appender.file=org.apache.log4j.RollingFileAppender
# Path to your log file (creates a 'logs' folder in project root if missing)
log4j.appender.file.File=logs/app.log
# Max size per log file before rolling over to a new one
log4j.appender.file.MaxFileSize=10MB
# Number of backup log files to retain
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Optional: Set custom log levels for specific packages
# Reduce debug noise from Spring framework
log4j.logger.org.springframework=INFO
# Boost verbosity for your application's code
log4j.logger.com.yourprojectpackage=DEBUG

Step 3: Verify Maven Dependencies (If Needed)

Spring Boot’s default spring-boot-starter-logging uses Logback instead of Log4j. If you run into classpath conflicts or Log4j still doesn’t pick up the config, adjust your pom.xml to exclude Logback and explicitly include Log4j:

<dependencies>
    <!-- Exclude default Logback logging starter -->
    <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>

    <!-- Add Log4j starter matching your Spring Boot version -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
        <version>1.3.8.RELEASE</version> <!-- Adjust to match your Spring Boot version -->
    </dependency>

    <!-- Your other project dependencies go here -->
</dependencies>

Step 4: Test the Setup

Restart your Spring Boot application. You should now see logs in both the console and the logs/app.log file (or whatever path you configured). If you want to disable console output entirely, just remove stdout from the log4j.rootLogger line.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:13:35