[Java]Configuration of Log4j

本文详细解析了一个使用Log4j进行多日志记录的配置文件,包括控制台输出、滚动日志文件的设置及标准日志级别配置。
Here is a configuration file that uses multiple appenders.
=====================================================================
log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
=====================================================================

For the standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL

If you wants more information of log4j, go to my blog in baidu to find what you need

Exception in thread "main" java.lang.AbstractMethodError: Receiver class org.springframework.boot.logging.log4j2.SpringBootConfigurationFactory does not define or inherit an implementation of the resolved method abstract getConfiguration(Lorg/apache/logging/log4j/core/config/ConfigurationSource;)Lorg/apache/logging/log4j/core/config/Configuration; of abstract class org.apache.logging.log4j.core.config.ConfigurationFactory. at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:509) at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:449) at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:257) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:565) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:582) at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:217) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45) at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194) at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:551) at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:537) at com.chinaway.execute.DeploymentListener.<clinit>(DeploymentListener.java:24)
最新发布
06-05
<think>我们遇到了一个`java.lang.AbstractMethodError`,错误信息指出`org.springframework.boot.logging.log4j2.SpringBootConfigurationFactory`没有实现其抽象父类`org.apache.logging.log4j.core.config.ConfigurationFactory`的抽象方法`getConfiguration`。错误分析:这个错误通常发生在编译时和运行时依赖的版本不一致的情况下。具体来说,`SpringBootConfigurationFactory`是SpringBoot对Log4j2的一个适配类。在运行时,JVM发现这个类没有实现其父类(或接口)要求的抽象方法,因此抛出`AbstractMethodError`。可能的原因:1.**版本冲突**:项目中可能引入了多个不同版本的Log4j2或SpringBoot的库,导致类加载器加载了不兼容的类。2.**构建问题**:构建时依赖的版本和运行时实际使用的版本不一致,例如Maven或Gradle的依赖管理出现了问题。解决方案:1.**检查依赖版本**:确保SpringBoot的版本与Log4j2的版本是兼容的。SpringBoot的每个版本都有其对应的兼容第三方库的版本列表。2.**排除冲突依赖**:如果项目中引入了多个不同版本的Log4j2相关依赖,需要排除掉冲突的版本。3.**检查依赖树**:使用Maven或Gradle的依赖树命令查看依赖关系。例如,使用Maven检查依赖树:```mvndependency:tree```或者使用Gradle:```gradledependencies```查找关于`log4j-core`和`spring-boot-starter-log4j2`的依赖,确保它们版本一致且兼容。4.**显式指定版本**:在项目的构建文件中(例如pom.xml或build.gradle),显式指定log4j2的版本,使其与SpringBoot版本兼容。以Maven为例,在`pom.xml`中,可以这样指定log4j2的版本(注意:请根据你使用的SpringBoot版本选择合适的log4j2版本):```xml<properties><log4j2.version>2.17.1</log4j2.version><!--版本需要与SpringBoot版本兼容--></properties>```或者,如果你使用的是Gradle,在`build.gradle`中:```groovyext{set('log4j2.version','2.17.1')}```5.**排除冲突**:如果发现某个依赖引入了低版本的log4j2,可以排除掉:在Maven中:```xml<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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId></dependency>```因为错误提到了`SpringBootConfigurationFactory`,这个类是SpringBoot提供的用于集成log4j2的。所以重点检查SpringBoot和log4j2的集成部分。另外,注意SpringBoot从1.3版本开始支持log4j2,但需要排除默认的logging(logback)并引入`spring-boot-starter-log4j2`。重新构建项目并运行,确保问题解决。如果以上方法未能解决问题,可能需要清理本地仓库重新下载依赖(有时本地仓库中的损坏的依赖也会导致类似问题)。由于错误信息直接指向了SpringBoot的类,所以也建议检查SpringBoot的版本是否过低,考虑升级到较新版本(但要确保与项目中其他库兼容)。相关问题:1.如何解决Java中的`AbstractMethodError`?2.SpringBoot与Log4j2的集成步骤是什么?3.如何排除Maven项目中的冲突依赖?4.如何检查Gradle项目的依赖树?</think> ### 问题分析 这个错误是典型的**版本不兼容问题**,发生在 Spring Boot 和 Log4j2 集成时。具体原因: - `SpringBootConfigurationFactory` 未实现 `ConfigurationFactory` 中的抽象方法 `getConfiguration()` - 根本原因是 **Log4j2 API 版本与 Spring Boot 使用的适配器版本不兼容** - 常见于项目中混合使用了不同来源的 Log4j2 依赖 ### 解决方案 #### 1. 强制统一 Log4j2 版本(推荐) 在 Maven 的 `pom.xml` 中显式指定 Log4j2 版本(以 2.17.2 为例): ```xml <properties> <!-- 使用与Spring Boot兼容的Log4j2版本 --> <log4j2.version>2.17.2</log4j2.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> <!-- 添加Log4j2依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies> ``` #### 2. 清理依赖冲突 使用 Maven 命令检查依赖树: ```bash mvn dependency:tree -Dincludes=org.apache.logging.log4j ``` #### 3. 检查 Spring Boot 兼容性 确保 Spring Boot 版本与 Log4j2 兼容: | Spring Boot | Log4j2 推荐版本 | |-------------|----------------| | 2.7.x | 2.17.x | | 3.0.x | 2.19.x | ### 解释说明 1. **版本冲突**:Spring Boot 的日志适配器需要特定版本的 Log4j2 API 2. **类加载机制**:JVM 加载了不兼容的类实现 3. **修复原理**: - 排除默认日志框架 (`spring-boot-starter-logging`) - 统一所有 Log4j2 组件的版本 - 使用 Spring Boot 官方的 Log4j2 启动器 ### 验证步骤 1. 删除 `maven` 本地仓库中所有 `org/apache/logging` 目录 2. 执行 `mvn clean install` 3. 检查启动日志是否有 `Log4j2` 初始化成功消息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值