在Spring Boot项目中,有几种方法可以查看当前使用的具体日志实现:
一、通过代码检查
1. 编写测试类查看
java
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class LogFrameworkTest {
@Test
void detectLogFramework() {
Logger logger = LoggerFactory.getLogger(LogFrameworkTest.class);
// 查看Logger实现类
System.out.println("======= 日志框架信息 =======");
System.out.println("Logger类名: " + logger.getClass().getName());
System.out.println("Logger类型: " + logger.getClass().getSimpleName());
// 查看LoggerFactory实现类
System.out.println("LoggerFactory类名: " +
LoggerFactory.getILoggerFactory().getClass().getName());
// 更详细的诊断
if (logger.getClass().getName().contains("ch.qos.logback")) {
System.out.println("✅ 当前使用: Logback");
} else if (logger.getClass().getName().contains("org.apache.logging.log4j")) {
System.out.println("✅ 当前使用: Log4j2");
} else if (logger.getClass().getName().contains("java.util.logging")) {
System.out.println("✅ 当前使用: JUL (java.util.logging)");
} else {
System.out.println("❓ 未知日志框架: " + logger.getClass().getName());
}
}
}
2. 启动时查看输出
java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class MyApplication {
private static final Logger log = LoggerFactory.getLogger(MyApplication.class);
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@PostConstruct
public void init() {
// 启动时打印日志框架信息
log.info("当前使用的日志框架: {}",
org.slf4j.LoggerFactory.getILoggerFactory().getClass().getName());
}
}
二、通过依赖分析
1. 查看Maven依赖树
bash
# Maven项目
mvn dependency:tree | grep -E "(logback|log4j|slf4j)"
# Gradle项目
gradle dependencies | grep -E "(logback|log4j|slf4j)"
2. 检查依赖文件
pom.xml 中的关键依赖:
xml
<!-- 默认使用 Logback -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<!-- 隐含包含 spring-boot-starter-logging -->
</dependency>
<!-- 如果使用 Log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
三、通过配置文件判断
1. 配置文件类型
text
# Logback 配置文件(默认)
logback-spring.xml
logback.xml
# Log4j2 配置文件
log4j2-spring.xml
log4j2.xml
2. 检查是否存在配置文件
java
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.IOException;
public void checkConfigFiles() {
PathMatchingResourcePatternResolver resolver =
new PathMatchingResourcePatternResolver();
String[] configFiles = {
"classpath:logback-spring.xml",
"classpath:logback.xml",
"classpath:log4j2-spring.xml",
"classpath:log4j2.xml"
};
for (String file : configFiles) {
try {
Resource resource = resolver.getResource(file);
if (resource.exists()) {
System.out.println("✅ 找到配置文件: " + file);
}
} catch (IOException e) {
// 忽略
}
}
}
四、通过应用启动日志判断
启动应用时,观察控制台输出的前几行:
Logback 的特征输出:
text
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.5)
2023-11-20 10:00:00.000 INFO 12345 --- [ main] com.example.App
⬆️ 这是Logback的默认格式
Log4j2 的特征(如果有特殊配置):
text
2023-11-20 10:00:00,000 INFO [main] com.example.App
⬆️ 日期格式和逗号分隔是Log4j2的常见特征
五、完整的诊断工具类
java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.ILoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
import java.util.Arrays;
@SpringBootApplication
public class LogFrameworkDetector {
public static void main(String[] args) {
SpringApplication.run(LogFrameworkDetector.class, args);
}
@PostConstruct
public void detect() {
System.out.println("\n========== 日志框架诊断报告 ==========");
// 1. 检查Logger实现
Logger logger = LoggerFactory.getLogger(LogFrameworkDetector.class);
System.out.println("1. Logger实现类: " + logger.getClass().getName());
// 2. 检查LoggerFactory实现
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
System.out.println("2. LoggerFactory实现类: " +
loggerFactory.getClass().getName());
// 3. 检查绑定的桥接包
printStaticLoggerBinder();
// 4. 输出结论
printConclusion(loggerFactory);
System.out.println("==================================\n");
}
private void printStaticLoggerBinder() {
try {
// SLF4J绑定的静态绑定类
Class<?> binderClass = Class.forName(
"org.slf4j.impl.StaticLoggerBinder");
Object binder = binderClass.getDeclaredMethod("getSingleton")
.invoke(null);
String version = (String) binderClass
.getDeclaredMethod("getLoggerFactoryClassStr")
.invoke(binder);
System.out.println("3. SLF4J绑定实现: " + version);
} catch (Exception e) {
System.out.println("3. 无法获取StaticLoggerBinder: " + e.getMessage());
}
}
private void printConclusion(ILoggerFactory loggerFactory) {
String className = loggerFactory.getClass().getName();
if (className.contains("ch.qos.logback")) {
System.out.println("✅ 结论: 当前使用 Logback");
} else if (className.contains("org.apache.logging.log4j")) {
System.out.println("✅ 结论: 当前使用 Log4j2");
} else if (className.contains("org.slf4j.impl.JDK14LoggerFactory")) {
System.out.println("✅ 结论: 当前使用 JUL (java.util.logging)");
} else if (className.contains("org.slf4j.impl.SimpleLoggerFactory")) {
System.out.println("✅ 结论: 当前使用 SLF4J Simple");
} else if (className.contains("org.slf4j.impl.NOPLoggerFactory")) {
System.out.println("✅ 结论: 当前使用 SLF4J NOP (无操作)");
} else {
System.out.println("❓ 结论: 未知日志框架: " + className);
}
}
}
六、命令行快速检查
在项目根目录运行:
bash
# 查看所有相关依赖
find . -name "*.jar" | xargs -I {} basename {} | grep -i "log" | sort | uniq
# 典型输出:
# logback-classic-1.4.11.jar # ← Logback实现
# logback-core-1.4.11.jar # ← Logback核心
# slf4j-api-2.0.9.jar # ← SLF4J接口
# 或
# log4j-api-2.20.0.jar # ← Log4j2 API
# log4j-core-2.20.0.jar # ← Log4j2核心
# log4j-slf4j2-impl-2.20.0.jar # ← SLF4J桥接
# slf4j-api-2.0.9.jar # ← SLF4J接口
七、总结判断方法
| 特征 | Logback | Log4j2 |
|---|---|---|
| 实现类 | ch.qos.logback.classic.Logger |
org.apache.logging.log4j.spi.AbstractLogger |
| 工厂类 | ch.qos.logback.classic.LoggerContext |
org.apache.logging.log4j.core.LoggerContext |
| 配置文件 | logback-spring.xml |
log4j2-spring.xml |
| Spring Boot Starter | spring-boot-starter-logging (默认) |
spring-boot-starter-log4j2 |
| 依赖JAR | logback-classic.jar |
log4j-core.jar |
最简单的方法:
-
运行测试代码查看Logger类名
-
检查pom.xml中是否有
spring-boot-starter-log4j2 -
默认情况下Spring Boot使用Logback