Skip to content

Commit

Permalink
fix log level is null in Slf4j and fix not valid in Log4j2 (#12671)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xixingya authored Jul 25, 2023
1 parent 7c7f3db commit 879fd81
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -94,6 +101,7 @@ public Level getLevel() {
@Override
public void setLevel(Level level) {
this.level = level;
Configurator.setLevel(LogManager.getRootLogger(), toLog4j2Level(level));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}

Expand Down

0 comments on commit 879fd81

Please sign in to comment.