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

Log4j v2创建日志文件但无内容且日志级别不生效问题排查

问题根源分析
  • API与配置版本不兼容:代码中使用Log4j 2.x的org.apache.logging.log4j.Logger API,但误用Log4j 1.x的org.apache.log4j.PropertyConfigurator加载Log4j 1.x格式的配置文件,两者完全不兼容,导致Log4j 2.x无法识别配置,默认仅输出ERROR/FATAL级别日志。
  • 依赖冲突:jxls引入的Log4j 1.2.14和SLF4J 1.7.26与你使用的Log4j 2.23.1、SLF4J 2.0.13版本冲突,引发类加载混乱,进一步加剧配置失效问题。
  • 配置文件路径与格式错误:Log4j 2.x默认从classpath根目录加载配置,你的配置放在src/resources/子目录下,不在默认扫描路径;且配置格式为Log4j 1.x,无法被Log4j 2.x解析。
分步解决方案

1. 修复Maven依赖冲突

在pom.xml中排除jxls依赖中的Log4j 1.x和旧版SLF4J,确保项目仅依赖Log4j 2.x及兼容组件:

<dependency>
    <groupId>org.jxls</groupId>
    <artifactId>jxls</artifactId>
    <version>你的jxls版本</version>
    <exclusions>
        <!-- 排除Log4j 1.x依赖 -->
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
        <!-- 排除旧版SLF4J -->
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- 引入正确的Log4j 2.x核心依赖 -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.23.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.23.1</version>
</dependency>
<!-- SLF4J与Log4j 2.x的绑定桥接 -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j2-impl</artifactId>
    <version>2.23.1</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.13</version>
</dependency>

2. 替换为Log4j 2.x格式的配置文件

将原配置文件移到src/main/resources/根目录,并重命名为log4j2.properties,内容修改为Log4j 2.x格式:

# 全局日志级别设为ALL
rootLogger.level = ALL
rootLogger.appenderRefs = stdout, rollingfile
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.rollingfile.ref = ROLLINGFILE

# 控制台输出Appender
appender.stdout.type = Console
appender.stdout.name = STDOUT
appender.stdout.layout.type = PatternLayout
appender.stdout.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# 滚动文件Appender
appender.rollingfile.type = RollingFile
appender.rollingfile.name = ROLLINGFILE
appender.rollingfile.fileName = var/log/app.log
appender.rollingfile.filePattern = var/log/app-%d{yyyy-MM-dd}-%i.log.gz
appender.rollingfile.immediateFlush = true
appender.rollingfile.layout.type = PatternLayout
appender.rollingfile.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# 文件大小触发滚动策略
appender.rollingfile.policies.type = Policies
appender.rollingfile.policies.size.type = SizeBasedTriggeringPolicy
appender.rollingfile.policies.size.size = 20MB
# 历史日志保留策略
appender.rollingfile.strategy.type = DefaultRolloverStrategy
appender.rollingfile.strategy.max = 10

3. 修正代码中的日志初始化逻辑

移除Log4j 1.x的PropertyConfigurator调用,Log4j 2.x会自动加载classpath根目录下的配置文件,修改后的测试代码:

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

public class Main {
    private static final Logger logger = LogManager.getLogger(Main.class);

    public static void main(String []args){
        // 验证日志级别是否生效
        System.out.println("debug enabled: " + logger.isDebugEnabled());
        System.out.println("info enabled: " + logger.isInfoEnabled());
        System.out.println("warn enabled: " + logger.isWarnEnabled());
        System.out.println("error enabled: " + logger.isErrorEnabled());
        System.out.println("fatal enabled: " + logger.isFatalEnabled());

        // 输出各级别日志
        logger.info("xxit");
        logger.error("xxit2");
        logger.warn("xxit3");
        logger.debug("xxit4");
        logger.fatal("xxit5");
    }
}

4. JavaFX全局复用Logger实例

创建全局日志工具类,统一管理Logger实例,确保所有控制器复用同一实例:

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

public class GlobalLogger {
    // 全局唯一Logger实例,可指定固定名称(如"AppGlobalLogger")
    private static final Logger GLOBAL_LOGGER = LogManager.getLogger("AppGlobalLogger");

    private GlobalLogger() {} // 私有构造,禁止实例化

    public static Logger getLogger() {
        return GLOBAL_LOGGER;
    }
}

在JavaFX控制器中使用:

public class SampleController {
    private final Logger logger = GlobalLogger.getLogger();

    public void initialize() {
        logger.info("控制器初始化完成");
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 07:45:11