项目使用spring+dubbo+zookeeper
因为需要跟踪dubbo服务调用方ip,使用在<dubbo:service />接口上加accesslog="true"的方法来记录访问log。
但是发现在使用log4j的项目中是可以的,在使用logback的项目中却不行。access日志的记录是通过AccessLogFilter来实现的,通过debug发现在使用logback的项目中记录log时的Logger对象是Log4jLogger对象。
public class LoggerFactory {
private static volatile LoggerAdapter LOGGER_ADAPTER;
private static final ConcurrentMap<String, FailsafeLogger> LOGGERS = new ConcurrentHashMap();
private LoggerFactory() {
}
public static void setLoggerAdapter(String loggerAdapter) {
if(loggerAdapter != null && loggerAdapter.length() > 0) {
setLoggerAdapter((LoggerAdapter)ExtensionLoader.getExtensionLoader(LoggerAdapter.class).getExtension(loggerAdapter));
}
}
public static void setLoggerAdapter(LoggerAdapter loggerAdapter) {
if(loggerAdapter != null) {
Logger logger = loggerAdapter.getLogger(LoggerFactory.class.getName());
logger.info("using logger: " + loggerAdapter.getClass().getName());
LOGGER_ADAPTER = loggerAdapter;
Iterator i$ = LOGGERS.entrySet().iterator();
while(i$.hasNext()) {
Entry entry = (Entry)i$.next();
((FailsafeLogger)entry.getValue()).setLogger(LOGGER_ADAPTER.getLogger((String)entry.getKey()));
}
}
}
public static Logger getLogger(Class<?> key) {
FailsafeLogger logger = (FailsafeLogger)LOGGERS.get(key.getName());
if(logger == null) {
LOGGERS.putIfAbsent(key.getName(), new FailsafeLogger(LOGGER_ADAPTER.getLogger(key)));
logger = (FailsafeLogger)LOGGERS.get(key.getName());
}
return logger;
}
public static Logger getLogger(String key) {
FailsafeLogger logger = (FailsafeLogger)LOGGERS.get(key);
if(logger == null) {
LOGGERS.putIfAbsent(key, new FailsafeLogger(LOGGER_ADAPTER.getLogger(key)));
logger = (FailsafeLogger)LOGGERS.get(key);
}
return logger;
}
public static void setLevel(Level level) {
LOGGER_ADAPTER.setLevel(level);
}
public static Level getLevel() {
return LOGGER_ADAPTER.getLevel();
}
public static File getFile() {
return LOGGER_ADAPTER.getFile();
}
static {
String logger = System.getProperty("dubbo.application.logger");
if("slf4j".equals(logger)) {
setLoggerAdapter((LoggerAdapter)(new Slf4jLoggerAdapter()));
} else if("jcl".equals(logger)) {
setLoggerAdapter((LoggerAdapter)(new JclLoggerAdapter()));
} else if("log4j".equals(logger)) {
setLoggerAdapter((LoggerAdapter)(new Log4jLoggerAdapter()));
} else if("jdk".equals(logger)) {
setLoggerAdapter((LoggerAdapter)(new JdkLoggerAdapter()));
} else {
try {
setLoggerAdapter((LoggerAdapter)(new Log4jLoggerAdapter()));
} catch (Throwable var6) {
try {
setLoggerAdapter((LoggerAdapter)(new Slf4jLoggerAdapter()));
} catch (Throwable var5) {
try {
setLoggerAdapter((LoggerAdapter)(new JclLoggerAdapter()));
} catch (Throwable var4) {
setLoggerAdapter((LoggerAdapter)(new JdkLoggerAdapter()));
}
}
}
}
}
}
进一步了解dubbo的日志系统,dubbo内部的日志输出都是通过LoggerFactory来获取Logger然后进行输出。
Logger接口有JclLogger(commons.logging)、JdkLogger(java.util.logging.Logger)、Log4jLogger、Slf4jLogger几种实现。Logger对象又是通过LoggerAdapter和第三方日志框架进行对接并直接创建,于是又对应几种LoggerAdapter:JclLoggerAdapter、JDKLoggerAdapter、Log4jLoggerAdapter、SLF4JLoggerAdapter。
通过这段代码
static {
String logger = System.getProperty("dubbo.application.logger");
if("slf4j".equals(logger)) {
setLoggerAdapter((LoggerAdapter)(new Slf4jLoggerAdapter()));
} else if("jcl".equals(logger)) {
setLoggerAdapter((LoggerAdapter)(new JclLoggerAdapter()));
} else if("log4j".equals(logger)) {
setLoggerAdapter((LoggerAdapter)(new Log4jLoggerAdapter()));
} else if("jdk".equals(logger)) {
setLoggerAdapter((LoggerAdapter)(new JdkLoggerAdapter()));
} else {
try {
setLoggerAdapter((LoggerAdapter)(new Log4jLoggerAdapter()));
} catch (Throwable var6) {
try {
setLoggerAdapter((LoggerAdapter)(new Slf4jLoggerAdapter()));
} catch (Throwable var5) {
try {
setLoggerAdapter((LoggerAdapter)(new JclLoggerAdapter()));
} catch (Throwable var4) {
setLoggerAdapter((LoggerAdapter)(new JdkLoggerAdapter()));
}
}
}
}
}
可以发现在未指定dubbo.application.logger,也就是默认情况下,dubbo会优先选用log4j,而logback是和slf4j集成的,这就说明了为何在使用logback时没有日志输出了。
解决的办法就是指定dubbo.application.logger参数为slf4j:
1、在jvm启动时添加此参数
-Ddubbo.application.logger=slf4j
2、在LoggerFactory被classloader加载之前通过编码方式指定
System.setProperty("dubbo.application.logger","slf4j");