最近在做dubbo3的相关优化,最开始时,发现没有打印dubbo日志,当然,生产环境也可以不需要打印,优化时需要开启
1. 为什么没有打印dubbo日志
先说结论,因为项目使用的是logback打印日志,但是,查了下,dubbo可配置日志输出框架,默认是log4j,所以不打印
看下源码
2. dubbo日志框架源码
java
public class LoggerFactory {
private static final ConcurrentMap<String, FailsafeLogger> LOGGERS = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, FailsafeErrorTypeAwareLogger> ERROR_TYPE_AWARE_LOGGERS = new ConcurrentHashMap<>();
private static volatile LoggerAdapter LOGGER_ADAPTER;
// search common-used logging frameworks
static {
//--这里可以配置日志输出框架
String logger = System.getProperty("dubbo.application.logger", "");
switch (logger) {
case "slf4j":
setLoggerAdapter(new Slf4jLoggerAdapter());
break;
case "jcl":
setLoggerAdapter(new JclLoggerAdapter());
break;
case "log4j":
setLoggerAdapter(new Log4jLoggerAdapter());
break;
case "jdk":
setLoggerAdapter(new JdkLoggerAdapter());
break;
case "log4j2":
setLoggerAdapter(new Log4j2LoggerAdapter());
break;
default:
//如果没有配置,则进入默认
List<Class<? extends LoggerAdapter>> candidates = Arrays.asList(
//log4j在第一个
Log4jLoggerAdapter.class,
Slf4jLoggerAdapter.class,
Log4j2LoggerAdapter.class,
JclLoggerAdapter.class,
JdkLoggerAdapter.class
);
for (Class<? extends LoggerAdapter> clazz : candidates) {
try {
//初始化第一个log4j框架
LoggerAdapter loggerAdapter = clazz.newInstance();
loggerAdapter.getLogger(LoggerFactory.class);
setLoggerAdapter(loggerAdapter);
break;
} catch (Throwable ignored) {
}
}
}
}
解决
我们在yml文件中配置:dubbo.application.logger: slf4j
外传
😜 原创不易,如若本文能够帮助到您的同学
🎉 支持我:关注我+点赞👍+收藏⭐️
📝 留言:探讨问题,看到立马回复
💬 格言:己所不欲勿施于人 扬帆起航、游历人生、永不言弃!🔥