Skip to content

Commit

Permalink
[MNG-8512] Fix org.slf4j.simpleLogger.defaultLogLevel Configuration c…
Browse files Browse the repository at this point in the history
…onflict

Signed-off-by: crazyhzm <[email protected]>
  • Loading branch information
CrazyHZM committed Jan 18, 2025
1 parent 0a588ea commit dc4328b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import org.apache.maven.cling.logging.BaseSlf4jConfiguration;
import org.apache.maven.slf4j.MavenLoggerFactory;
import org.apache.maven.slf4j.MavenSimpleLogger;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
Expand All @@ -29,6 +31,8 @@
* @since 3.1.0
*/
public class MavenSimpleConfiguration extends BaseSlf4jConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(MavenSimpleConfiguration.class);

@Override
public void setRootLoggerLevel(Level level) {
String value =
Expand All @@ -37,7 +41,14 @@ public void setRootLoggerLevel(Level level) {
case INFO -> "info";
default -> "error";
};
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", value);

String current = System.setProperty(MavenSimpleLogger.DEFAULT_LOG_LEVEL_KEY, value);
if (current != null && !value.equalsIgnoreCase(current)) {
LOGGER.info(
"System property '" + MavenSimpleLogger.DEFAULT_LOG_LEVEL_KEY + "' is already set to '" + current
+ "' - ignoring system property and get log level from -X/-e/-q options, log level will be set to"
+ value);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.maven.logging.api.LogLevelRecorder;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;

/**
* LogFactory for Maven which can create a simple logger or one which, if set, fails the build on a severity threshold.
*/
public class MavenLoggerFactory implements org.apache.maven.logging.api.LogLevelRecorder, ILoggerFactory {
public class MavenLoggerFactory implements LogLevelRecorder, ILoggerFactory {
final DefaultLogLevelRecorder logLevelRecorder = new DefaultLogLevelRecorder();
final ConcurrentMap<String, Logger> loggerMap = new ConcurrentHashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class MavenSimpleLogger extends MavenBaseLogger {

static Consumer<String> logSink;

public static final String DEFAULT_LOG_LEVEL_KEY = "org.slf4j.simpleLogger.defaultLogLevel";

public static void setLogSink(Consumer<String> logSink) {
MavenSimpleLogger.logSink = logSink;
}
Expand Down

0 comments on commit dc4328b

Please sign in to comment.