dubbo默认使用log4j作为日志框架,如果你所使用的日志框架与之不一样可能出现日志不出现的情况。
可通过配置应用的logger属性解决
//slf4j,jcl,log4j,jdk,log4j2
如果使用的是旧版本的dubbo,不支持log4j2可通过官方提供的配置接口实现log4j2适配器便可以使用。
import com.alibaba.dubbo.common.logger.Level;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerAdapter;
import org.apache.logging.log4j.LogManager;
import java.io.File;
public class Log4j2LoggerAdapter implements LoggerAdapter {
private Level level;
public Log4j2LoggerAdapter() {
}
private static org.apache.logging.log4j.Level toLog4j2Level(Level level) {
if (level == Level.ALL) {
return org.apache.logging.log4j.Level.ALL;
}
if (level == Level.TRACE) {
return org.apache.logging.log4j.Level.TRACE;
}
if (level == Level.DEBUG) {
return org.apache.logging.log4j.Level.DEBUG;
}
if (level == Level.INFO) {
return org.apache.logging.log4j.Level.INFO;
}
if (level == Level.WARN) {
return org.apache.logging.log4j.Level.WARN;
}
if (level == Level.ERROR) {
return org.apache.logging.log4j.Level.ERROR;
}
return org.apache.logging.log4j.Level.OFF;
}
private static Level fromLog4j2Level(org.apache.logging.log4j.Level level) {
if (level == org.apache.logging.log4j.Level.ALL) {
return Level.ALL;
}
if (level == org.apache.logging.log4j.Level.TRACE) {
return Level.TRACE;
}
if (level == org.apache.logging.log4j.Level.DEBUG) {
return Level.DEBUG;
}
if (level == org.apache.logging.log4j.Level.INFO) {
return Level.INFO;
}
if (level == org.apache.logging.log4j.Level.WARN) {
return Level.WARN;
}
if (level == org.apache.logging.log4j.Level.ERROR) {
return Level.ERROR;
}
return Level.OFF;
}
@Override
public Logger getLogger(Class> key) {
return new Log4j2Logger(LogManager.getLogger(key));
}
@Override
public Logger getLogger(String key) {
return new Log4j2Logger(LogManager.getLogger(key));
}
@Override
public Level getLevel() {
return level;
}
@Override
public void setLevel(Level level) {
this.level = level;
}
@Override
public File getFile() {
return null;
}
@Override
public void setFile(File file) {
}
}
import com.alibaba.dubbo.common.logger.Logger;
public class Log4j2Logger implements Logger {
private final org.apache.logging.log4j.Logger logger;
public Log4j2Logger(org.apache.logging.log4j.Logger logger) {
this.logger = logger;
}
@Override
public void trace(String msg) {
logger.trace(msg);
}
@Override
public void trace(Throwable e) {
logger.trace(e == null ? null : e.getMessage(), e);
}
@Override
public void trace(String msg, Throwable e) {
logger.trace(msg, e);
}
@Override
public void debug(String msg) {
logger.debug(msg);
}
@Override
public void debug(Throwable e) {
logger.debug(e == null ? null : e.getMessage(), e);
}
@Override
public void debug(String msg, Throwable e) {
logger.debug(msg, e);
}
@Override
public void info(String msg) {
logger.info(msg);
}
@Override
public void info(Throwable e) {
logger.info(e == null ? null : e.getMessage(), e);
}
@Override
public void info(String msg, Throwable e) {
logger.info(msg, e);
}
@Override
public void warn(String msg) {
logger.warn(msg);
}
@Override
public void warn(Throwable e) {
logger.warn(e == null ? null : e.getMessage(), e);
}
@Override
public void warn(String msg, Throwable e) {
logger.warn(msg, e);
}
@Override
public void error(String msg) {
logger.error(msg);
}
@Override
public void error(Throwable e) {
logger.error(e == null ? null : e.getMessage(), e);
}
@Override
public void error(String msg, Throwable e) {
logger.error(msg, e);
}
@Override
public boolean isTraceEnabled() {
return logger.isTraceEnabled();
}
@Override
public boolean isDebugEnabled() {
return logger.isDebugEnabled();
}
@Override
public boolean isInfoEnabled() {
return logger.isInfoEnabled();
}
@Override
public boolean isWarnEnabled() {
return logger.isWarnEnabled();
}
@Override
public boolean isErrorEnabled() {
return logger.isErrorEnabled();
}
}
在/src/main/resources/META-INF/dubbo目录下新建文件
文件名为:com.alibaba.dubbo.common.logger.LoggerAdapter
文件内容为:log4j2=com.wisedu.wec.cpdaily.config.provider.common.log4j2.Log4j2LoggerAdapter
//slf4j,jcl,log4j,jdk,log4j2
更多扩展实现方式请参看官方说明:http://dubbo.apache.org/zh-cn/docs/dev/impls/logger-adapter.html