YAZONG 我的开源

迁移dubbo2.6.6日志切换功能的影响

  ,
0 评论0 浏览

dubbo的日志切换通过参数的获取来加载不同的日志适配器,从而在业务层打印日志时,来使用不同的日志对象来输入内容。

在现阶段springboot的项目中,我通过上述dubbo功能点,改造到了自己的项目中,由于是通过elasticsearch查询数据直接返回的接口,就在接收参数和返回结果集时使用了上述打印日志功能。

经压测,如果不加日志输出,那么在200个线程,60秒,无限调用线程的情况下,每秒的吞吐量大概在1500左右,如果加上述日志输出,那么这里的吞吐量大约降低了30%-50%。个人没加日志缓存功能,推测,如果加上之后,那么会有更大的性能提升,这里只是把根据dubbo改造的日志改成了log4j2。

现在没有做服务治理,只是通过Springcloud做的微服务架构,接口链路调用为APP-JAVA-JAVA(ES),在这种思想下,个人觉得这种日志切换并没有意义。

案例代码如下:

public class LoggerFactory {

    private static final ConcurrentMap<String,FailsafeLogger> LOGGERS = new ConcurrentHashMap<>();
    private static volatile LoggerAdapter LOGGER_APAPTER;
    private static String LOGGER_TYPE = "SLF4J";

    private LoggerFactory(){}

    static{

        initLoggerType();

    }

    /**
     * 初始化日志对象类型
     */
    private static void initLoggerType(){

        if(LOGGER_TYPE.equals(LoggerTypeEnum.SLF4J.name())){

            setLoggerAdapter(new Slf4jLoggerAdapter());

        }else if(LOGGER_TYPE.equals(LoggerTypeEnum.JDK.name())){

            setLoggerAdapter(new JdkLoggerAdapter());

        }else if(LOGGER_TYPE.equals(LoggerTypeEnum.JCL.name())){

            setLoggerAdapter(new JclLoggerAdapter());

        }else{

            try {
                setLoggerAdapter(new Slf4jLoggerAdapter());
            }catch (Throwable t1){
                try {
                    setLoggerAdapter(new JclLoggerAdapter());
                }catch (Throwable t2){
                    setLoggerAdapter(new JdkLoggerAdapter());
                }
            }

        }
    }

    /**
     * 重置日志对象
     * @param resetVal
     */
    public static void resetLoggerType(String resetVal){
        if(resetVal != null){
            LOGGER_TYPE = resetVal;
            initLoggerType();
        }

    }

    /**
     * 设置日志适配器
     * @param loggerAdapter
     */
    public static void setLoggerAdapter(LoggerAdapter loggerAdapter){
        if(loggerAdapter != null){
            LoggerFactory.LOGGER_APAPTER = loggerAdapter;
            for(Map.Entry<String,FailsafeLogger> entry : LOGGERS.entrySet()){
                entry.getValue().setLogger(LOGGER_APAPTER.getLogger(entry.getClass()));
            }

        }

    }

    /**
     * 获取日志对象
     * @param key
     * @return
     */
    public static Logger getLogger(Class<?> key){

        FailsafeLogger logger = LOGGERS.get(key.getName());
        if(logger == null){
            LOGGERS.putIfAbsent(key.getName(),new FailsafeLogger(LOGGER_APAPTER.getLogger(key)));
            logger = LOGGERS.get(key.getName());
        }

        return logger;

    }

    /**
     * 获取日志对象
     * @param key
     * @return
     */
    public static Logger getLogger(String key){
        FailsafeLogger logger = LOGGERS.get(key);
        if(logger == null){
            LOGGERS.putIfAbsent(key,new FailsafeLogger(LOGGER_APAPTER.getLogger(key)));
            logger = LOGGERS.get(key);
        }
        return logger;
    }

    /**
     * 获取日志级别
     * @return
     */
    public static LoggerLeveEnum getLevel(){
        return LOGGER_APAPTER.getLevel();
    }

    /**
     * 设置日志级别
     * @param level
     */
    public static void setLevel(LoggerLeveEnum level){
        LOGGER_APAPTER.setLevel(level);
    }



}

public enum LoggerLeveEnum {

    ALL,
    TRACE,
    DEBUG,
    INFO,
    WARN,
    ERROR,
    OFF
    
}
public enum LoggerTypeEnum {

    SLF4J,JCL,JDK

}
public interface LoggerAdapter {

    Logger getLogger(Class<?> key);

    Logger getLogger(String key);

    LoggerLeveEnum getLevel();

    void setLevel(LoggerLeveEnum level);


}
public interface Logger {

    void trace(String msg);
    void trace(Throwable e);
    void trace(String msg,Throwable e);

    void debug(String msg);
    void debug(Throwable e);
    void debug(String msg,Throwable e);

    void info(String msg);
    void info(Throwable e);
    void info(String msg,Throwable e);

    void warn(String msg);
    void warn(Throwable e);
    void warn(String msg,Throwable e);

    void error(String msg);
    void error(Throwable e);
    void error(String msg,Throwable e);

    boolean isTraceEnabled();

    boolean isDebugEnabled();

    boolean isInfoEnabled();

    boolean isWarnEnabled();

    boolean isErrorEnabled();




}

public class FailsafeLogger implements Logger {

    private Logger logger;

    public FailsafeLogger(Logger logger) {
        this.logger = logger;
    }

    public Logger getLogger() {
        return logger;
    }

    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    private String appendContextMessage(String msg){
        return "version : " + Version.getVersion() + ", current host : " + NetUtils.getLocalHost()
                + SysConstants.LEFT_BRACKET_STR + SysConstants.PROJECT_NAME + SysConstants.RIGHT_BRACKET_STR + msg;
    }

    @Override
    public void trace(String msg, Throwable e) {
        try {
            logger.trace(appendContextMessage(msg),e);
        }catch (Throwable t){}
    }

    @Override
    public void trace(Throwable e) {
        try {
            logger.trace(e);
        }catch (Throwable t){}
    }

    @Override
    public void trace(String msg) {
        try{
            logger.trace(appendContextMessage(msg));
        }catch (Throwable t){}
    }

    @Override
    public void debug(String msg, Throwable e) {
        try{
            logger.debug(appendContextMessage(msg),e);
        }catch (Throwable t){}
    }

    @Override
    public void debug(Throwable e) {
        try {
            logger.debug(e);
        }catch (Throwable t){}
    }

    @Override
    public void debug(String msg) {
        try {
            logger.debug(appendContextMessage(msg));
        }catch (Throwable e){}
    }

    @Override
    public void info(String msg, Throwable e) {
        try {
            logger.info(appendContextMessage(msg),e);
        }catch (Throwable t){}
    }

    @Override
    public void info(Throwable e) {
        try{
            logger.info(e);
        }catch (Throwable t){}
    }

    @Override
    public void info(String msg) {
        try{
            logger.info(appendContextMessage(msg));
        }catch (Throwable t){}
    }

    @Override
    public void warn(String msg, Throwable e) {
        try{
            logger.warn(appendContextMessage(msg),e);
        }catch (Throwable t){}
    }

    @Override
    public void warn(Throwable e) {
        try{
            logger.warn(e);
        }catch (Throwable t){}
    }

    @Override
    public void warn(String msg) {
        try {
            logger.warn(appendContextMessage(msg));
        }catch (Throwable t){}
    }

    @Override
    public void error(String msg, Throwable e) {
        try{
            logger.error(appendContextMessage(msg),e);
        }catch (Throwable t){}
    }

    @Override
    public void error(Throwable e) {
        try{
            logger.error(e);
        }catch (Throwable t){}
    }

    @Override
    public void error(String msg) {
        try{
            logger.error(appendContextMessage(msg));
        }catch (Throwable e){}
    }

    @Override
    public boolean isTraceEnabled() {
        try{
            return logger.isTraceEnabled();
        }catch (Throwable t){
            return Boolean.FALSE;
        }
    }

    @Override
    public boolean isDebugEnabled() {
        try{
            return logger.isDebugEnabled();
        }catch (Throwable t){
            return Boolean.FALSE;
        }
    }

    @Override
    public boolean isInfoEnabled() {
        try {
            return logger.isInfoEnabled();
        }catch (Throwable t){
            return Boolean.FALSE;
        }
    }

    @Override
    public boolean isWarnEnabled() {
        try{
            return logger.isWarnEnabled();
        }catch (Throwable t){
            return Boolean.FALSE;
        }
    }

    @Override
    public boolean isErrorEnabled() {
        try{
            return logger.isErrorEnabled();
        }catch (Throwable t){
            return Boolean.FALSE;
        }
    }
}

public class Slf4jLoggerAdapter implements LoggerAdapter {

    private LoggerLeveEnum level;

    @Override
    public Logger getLogger(Class<?> key) {
        return new Slf4jLogger(org.slf4j.LoggerFactory.getLogger(key));
    }

    @Override
    public Logger getLogger(String key) {
        return new Slf4jLogger(org.slf4j.LoggerFactory.getLogger(key));
    }

    @Override
    public LoggerLeveEnum getLevel() {
        return level;
    }

    @Override
    public void setLevel(LoggerLeveEnum level) {
        this.level = level;
    }


}
public class Slf4jLogger implements Logger,Serializable {

    private static final long serialVersionUID = 1L;

    private static final String CLASS_NAME = FailsafeLogger.class.getName();
    private final org.slf4j.Logger logger;

    private final LocationAwareLogger locationAwareLogger;

    public Slf4jLogger(org.slf4j.Logger logger) {
        if(logger instanceof LocationAwareLogger){
            locationAwareLogger = (LocationAwareLogger) logger;
        }else{
            locationAwareLogger = null;
        }
        this.logger = logger;
    }

    @Override
    public void trace(String msg) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.TRACE_INT,msg,null,null);
            return;
        }
        logger.trace(msg);
    }

    @Override
    public void trace(Throwable e) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.TRACE_INT,e.getMessage(),null,e);
            return;
        }
        logger.trace(e.getMessage(),e);
    }

    @Override
    public void trace(String msg, Throwable e) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.TRACE_INT,msg,null,e);
            return;
        }
        logger.trace(msg,e);
    }

    @Override
    public void debug(String msg) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.DEBUG_INT,msg,null,null);
            return;
        }
        logger.debug(msg);
    }

    @Override
    public void debug(Throwable e) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.DEBUG_INT,e.getMessage(),null,e);
            return;
        }
        logger.debug(e.getMessage(),e);
    }

    @Override
    public void debug(String msg, Throwable e) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.DEBUG_INT,msg,null,e);
            return;
        }
        logger.debug(msg,e);
    }

    @Override
    public void info(String msg) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.INFO_INT,msg,null,null);
            return;
        }
        logger.info(msg);
    }

    @Override
    public void info(Throwable e) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.INFO_INT,e.getMessage(),null,e);
            return;
        }
        logger.info(e.getMessage());
    }

    @Override
    public void info(String msg, Throwable e) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.INFO_INT,msg,null,e);
            return;
        }
        logger.info(msg,e);
    }

    @Override
    public void warn(String msg) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.WARN_INT,msg,null,null);
            return;
        }
        logger.warn(msg);
    }

    @Override
    public void warn(Throwable e) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.WARN_INT,e.getMessage(),null,e);
            return;
        }
        locationAwareLogger.warn(e.getMessage());
    }

    @Override
    public void warn(String msg, Throwable e) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.WARN_INT,msg,null,e);
            return;
        }
        locationAwareLogger.warn(msg,e);
    }

    @Override
    public void error(String msg) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.ERROR_INT,msg,null,null);
            return;
        }
        locationAwareLogger.error(msg);
    }

    @Override
    public void error(Throwable e) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.ERROR_INT,e.getMessage(),null,e);
            return;
        }
        locationAwareLogger.error(e.getMessage());
    }

    @Override
    public void error(String msg, Throwable e) {
        if(locationAwareLogger != null){
            locationAwareLogger.log(null,CLASS_NAME,LocationAwareLogger.ERROR_INT,msg,null,e);
            return;
        }
        locationAwareLogger.error(msg,e.getMessage());
    }

    @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();
    }
}


标题:迁移dubbo2.6.6日志切换功能的影响
作者:yazong
地址:https://blog.llyweb.com/articles/2019/04/25/1578151418599.html