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

Log4j无法输出至控制台,已配置依赖与配置文件求助

Alright, let's troubleshoot why your Log4j setup isn't outputting to the console—there are a few clear issues in your configuration and environment setup that we can fix step by step:

1. Fix Critical Environment Variable Errors

First off, your environment variable has two key mistakes:

  • Wrong variable name: If you're using Log4j 2, the correct environment variable to specify the config file is LOG4J_CONFIGURATION_FILE (not LOG4J_log4j.configurationFile).
  • Typos in config filename: You wrote log4j.propertie (missing the final s)—your actual file is log4j.properties, so this mismatch means Log4j can't find your config and falls back to a default (non-console) setup.

Also, if you're using Log4j 1.x, you don't even need this environment variable at all—Log4j 1 automatically loads log4j.properties from the resources directory by default.

2. Clean Up Your rootLogger Configuration

Your root logger line declares two appenders (stdout and console):

log4j.rootLogger=INFO, stdout, console

But you only defined the stdout appender in your config—there's no configuration for the console appender. This causes Log4j to fail initializing your custom setup, so it ignores your config entirely.

Simplify this line to only include the appender you actually configured:

log4j.rootLogger=INFO, stdout

3. Confirm Which Log4j Version You're Using

This is a common gotcha—Log4j 1.x and Log4j 2.x have completely different config syntax and dependencies:

If you're using Log4j 1.x:

Your appender configuration is correct (org.apache.log4j.ConsoleAppender), but make sure your pom.xml is pulling in the Log4j 1 dependency:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

If you're using Log4j 2.x:

Your current log4j.properties format is for Log4j 1, which won't work for Log4j 2. Log4j 2 uses a different properties syntax, and the default config filename is log4j2.properties. Here's a corrected version for Log4j 2:

status = error
name = PropertiesConfig

appenders = console

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

loggers = root
logger.root.name = root
logger.root.level = info
logger.root.additivity = false
logger.root.appenderRefs = stdout
logger.root.appenderRef.stdout.ref = STDOUT

Also ensure your pom.xml has Log4j 2 core dependencies (not the old Log4j 1 artifacts):

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.20.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.20.0</version>
</dependency>

4. Quick Debug Test

To rule out other issues, add a simple test class to force logging and verify your setup:

For Log4j 1.x:

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class LogTest {
    public static void main(String[] args) {
        // Explicitly load the config file to confirm it's accessible
        PropertyConfigurator.configure("src/main/resources/log4j.properties");
        Logger logger = Logger.getLogger(LogTest.class);
        logger.info("This is a test log message!");
    }
}

For Log4j 2.x:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LogTest {
    private static final Logger logger = LogManager.getLogger(LogTest.class);
    
    public static void main(String[] args) {
        logger.info("This is a test log message!");
    }
}

Run this class—if the test message shows up in the console, you know your config is working, and any remaining issues are in your main application code.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:28:40