From 879fd811283c28a5fcb4dfebd383e05b5e500cb0 Mon Sep 17 00:00:00 2001 From: xixingya <2679431923@qq.com> Date: Tue, 25 Jul 2023 21:18:30 +0800 Subject: [PATCH] fix log level is null in Slf4j and fix not valid in Log4j2 (#12671) * fix log level is null in Slf4j and fix not valid in Log4j2 * format logger code * format logger code * format logger code * format logger code * fix npe --- .../logger/log4j2/Log4j2LoggerAdapter.java | 8 +++++++ .../common/logger/slf4j/Slf4jLogger.java | 24 +++++++++++++++++++ .../logger/slf4j/Slf4jLoggerAdapter.java | 10 +++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java index 0a74a637399..c8f25d59e52 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java @@ -21,6 +21,7 @@ import org.apache.dubbo.common.logger.LoggerAdapter; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.config.Configurator; import java.io.File; @@ -30,6 +31,12 @@ public class Log4j2LoggerAdapter implements LoggerAdapter { private Level level; public Log4j2LoggerAdapter() { + try { + org.apache.logging.log4j.Logger logger = LogManager.getRootLogger(); + this.level = fromLog4j2Level(logger.getLevel()); + } catch (Exception t) { + // ignore + } } private static org.apache.logging.log4j.Level toLog4j2Level(Level level) { @@ -94,6 +101,7 @@ public Level getLevel() { @Override public void setLevel(Level level) { this.level = level; + Configurator.setLevel(LogManager.getRootLogger(), toLog4j2Level(level)); } @Override diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/slf4j/Slf4jLogger.java b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/slf4j/Slf4jLogger.java index d212cb183d7..592c1a6717e 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/slf4j/Slf4jLogger.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/slf4j/Slf4jLogger.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.common.logger.slf4j; +import org.apache.dubbo.common.logger.Level; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.support.FailsafeLogger; @@ -198,4 +199,27 @@ public boolean isErrorEnabled() { return logger.isErrorEnabled(); } + public static Level getLevel(org.slf4j.Logger logger) { + if (logger.isTraceEnabled()) { + return Level.TRACE; + } + if (logger.isDebugEnabled()) { + return Level.DEBUG; + } + if (logger.isInfoEnabled()) { + return Level.INFO; + } + if (logger.isWarnEnabled()) { + return Level.WARN; + } + if (logger.isErrorEnabled()) { + return Level.ERROR; + } + return Level.OFF; + } + + public Level getLevel() { + return getLevel(logger); + } + } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerAdapter.java b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerAdapter.java index 1f56242ddae..47f4c170c39 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerAdapter.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerAdapter.java @@ -20,14 +20,21 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerAdapter; import org.apache.dubbo.common.utils.ClassUtils; - +import org.slf4j.LoggerFactory; import java.io.File; public class Slf4jLoggerAdapter implements LoggerAdapter { public static final String NAME = "slf4j"; + private Level level; private File file; + private static final org.slf4j.Logger ROOT_LOGGER = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); + + public Slf4jLoggerAdapter() { + this.level = Slf4jLogger.getLevel(ROOT_LOGGER); + } + @Override public Logger getLogger(String key) { return new Slf4jLogger(org.slf4j.LoggerFactory.getLogger(key)); @@ -45,6 +52,7 @@ public Level getLevel() { @Override public void setLevel(Level level) { + System.err.printf("The level of slf4j logger current can not be set, using the default level: %s \n", Slf4jLogger.getLevel(ROOT_LOGGER)); this.level = level; }