mirror of https://github.com/apache/maven.git
[MNG-8405] Fail On Severity, when set, is not reset (in resident instances) (#1950)
Make it reset that does not leave dangling values for next invocation. --------- https://issues.apache.org/jira/browse/MNG-8405 Co-authored-by: Tamas Cservenak <tamas@cservenak.net>
This commit is contained in:
parent
e74bde05c9
commit
93e0f1bb79
|
@ -34,4 +34,6 @@ public interface LogLevelRecorder {
|
|||
Level getMaxLevelAllowed();
|
||||
|
||||
void setMaxLevelAllowed(Level level);
|
||||
|
||||
void reset();
|
||||
}
|
||||
|
|
|
@ -18,8 +18,6 @@
|
|||
*/
|
||||
package org.apache.maven.slf4j;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.apache.maven.logging.api.LogLevelRecorder;
|
||||
|
@ -28,25 +26,9 @@ import org.apache.maven.logging.api.LogLevelRecorder;
|
|||
* Responsible for keeping state of whether the threshold of the --fail-on-severity flag has been hit.
|
||||
*/
|
||||
public class DefaultLogLevelRecorder implements LogLevelRecorder {
|
||||
private static final Map<String, Level> ACCEPTED_LEVELS = new HashMap<>();
|
||||
|
||||
static {
|
||||
ACCEPTED_LEVELS.put("WARN", Level.WARN);
|
||||
ACCEPTED_LEVELS.put("WARNING", Level.WARN);
|
||||
ACCEPTED_LEVELS.put("ERROR", Level.ERROR);
|
||||
}
|
||||
|
||||
private Level maxAllowed;
|
||||
private final AtomicReference<Level> maxReached = new AtomicReference<>(Level.DEBUG);
|
||||
|
||||
public DefaultLogLevelRecorder(String threshold) {
|
||||
this(determineThresholdLevel(threshold));
|
||||
}
|
||||
|
||||
public DefaultLogLevelRecorder(Level maxAllowed) {
|
||||
this.maxAllowed = maxAllowed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasReachedMaxLevel() {
|
||||
return maxReached.get().ordinal() > maxAllowed.ordinal();
|
||||
|
@ -67,14 +49,10 @@ public class DefaultLogLevelRecorder implements LogLevelRecorder {
|
|||
this.maxAllowed = level;
|
||||
}
|
||||
|
||||
private static Level determineThresholdLevel(String input) {
|
||||
final Level result = ACCEPTED_LEVELS.get(input);
|
||||
if (result == null) {
|
||||
String message = String.format(
|
||||
"%s is not a valid log severity threshold. Valid severities are WARN/WARNING and ERROR.", input);
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
return result;
|
||||
@Override
|
||||
public void reset() {
|
||||
this.maxAllowed = null;
|
||||
this.maxReached.set(Level.DEBUG);
|
||||
}
|
||||
|
||||
public void record(org.slf4j.event.Level logLevel) {
|
||||
|
@ -97,6 +75,6 @@ public class DefaultLogLevelRecorder implements LogLevelRecorder {
|
|||
}
|
||||
|
||||
public boolean metThreshold() {
|
||||
return maxReached.get().ordinal() >= maxAllowed.ordinal();
|
||||
return maxAllowed != null && maxReached.get().ordinal() >= maxAllowed.ordinal();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ 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 {
|
||||
DefaultLogLevelRecorder logLevelRecorder = null;
|
||||
final DefaultLogLevelRecorder logLevelRecorder = new DefaultLogLevelRecorder();
|
||||
final ConcurrentMap<String, Logger> loggerMap = new ConcurrentHashMap<>();
|
||||
|
||||
public MavenLoggerFactory() {
|
||||
|
@ -37,7 +37,7 @@ public class MavenLoggerFactory implements org.apache.maven.logging.api.LogLevel
|
|||
|
||||
@Override
|
||||
public boolean hasReachedMaxLevel() {
|
||||
return logLevelRecorder != null && logLevelRecorder.metThreshold();
|
||||
return logLevelRecorder.metThreshold();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -52,7 +52,7 @@ public class MavenLoggerFactory implements org.apache.maven.logging.api.LogLevel
|
|||
|
||||
@Override
|
||||
public void setMaxLevelAllowed(Level level) {
|
||||
this.logLevelRecorder = new DefaultLogLevelRecorder(level.name());
|
||||
this.logLevelRecorder.setMaxLevelAllowed(level);
|
||||
}
|
||||
/**
|
||||
* Return an appropriate {@link Logger} instance by name.
|
||||
|
@ -62,12 +62,13 @@ public class MavenLoggerFactory implements org.apache.maven.logging.api.LogLevel
|
|||
return loggerMap.computeIfAbsent(name, this::getNewLoggingInstance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
logLevelRecorder.reset();
|
||||
}
|
||||
|
||||
protected Logger getNewLoggingInstance(String name) {
|
||||
if (logLevelRecorder == null) {
|
||||
return new MavenSimpleLogger(name);
|
||||
} else {
|
||||
return new MavenFailOnSeverityLogger(name, logLevelRecorder);
|
||||
}
|
||||
return new MavenFailOnSeverityLogger(name, logLevelRecorder);
|
||||
}
|
||||
|
||||
public void reconfigure() {
|
||||
|
@ -78,5 +79,6 @@ public class MavenLoggerFactory implements org.apache.maven.logging.api.LogLevel
|
|||
msl.configure(config.defaultLogLevel);
|
||||
}
|
||||
});
|
||||
logLevelRecorder.reset();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,43 +18,19 @@
|
|||
*/
|
||||
package org.apache.maven.slf4j;
|
||||
|
||||
import org.apache.maven.logging.api.LogLevelRecorder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.event.Level;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class LogLevelRecorderTest {
|
||||
@Test
|
||||
void createsLogLevelRecorder() {
|
||||
DefaultLogLevelRecorder logLevelRecorder = new DefaultLogLevelRecorder("WARN");
|
||||
DefaultLogLevelRecorder logLevelRecorder = new DefaultLogLevelRecorder();
|
||||
logLevelRecorder.setMaxLevelAllowed(LogLevelRecorder.Level.WARN);
|
||||
logLevelRecorder.record(Level.ERROR);
|
||||
|
||||
assertTrue(logLevelRecorder.metThreshold());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsOnLowerThanWarn() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new DefaultLogLevelRecorder("INFO"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsLogLevelRecorderWithWarning() {
|
||||
DefaultLogLevelRecorder logLevelRecorder = new DefaultLogLevelRecorder("WARNING");
|
||||
logLevelRecorder.record(Level.ERROR);
|
||||
|
||||
assertTrue(logLevelRecorder.metThreshold());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsOnUnknownLogLevel() {
|
||||
Throwable thrown = assertThrows(IllegalArgumentException.class, () -> new DefaultLogLevelRecorder("SEVERE"));
|
||||
String message = thrown.getMessage();
|
||||
assertThat(message, containsString("SEVERE is not a valid log severity threshold"));
|
||||
assertThat(message, containsString("WARN"));
|
||||
assertThat(message, containsString("WARNING"));
|
||||
assertThat(message, containsString("ERROR"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
package org.apache.maven.slf4j;
|
||||
|
||||
import org.apache.maven.logging.api.LogLevelRecorder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
|
@ -56,7 +57,7 @@ class MavenLoggerFactoryTest {
|
|||
@Test
|
||||
void reportsWhenFailOnSeverityThresholdHasBeenHit() {
|
||||
MavenLoggerFactory mavenLoggerFactory = new MavenLoggerFactory();
|
||||
mavenLoggerFactory.logLevelRecorder = new DefaultLogLevelRecorder("ERROR");
|
||||
mavenLoggerFactory.logLevelRecorder.setMaxLevelAllowed(LogLevelRecorder.Level.ERROR);
|
||||
|
||||
MavenFailOnSeverityLogger logger = (MavenFailOnSeverityLogger) mavenLoggerFactory.getLogger("Test");
|
||||
assertFalse(mavenLoggerFactory.logLevelRecorder.metThreshold());
|
||||
|
|
Loading…
Reference in New Issue