[MNG-6891] Improve user-friendliness --fail-on-severity

This commit is contained in:
Maarten Mulders 2020-05-03 22:04:37 +02:00 committed by rfscholte
parent 50119d4f73
commit 7aef391551
2 changed files with 45 additions and 3 deletions

View File

@ -21,17 +21,29 @@
import org.slf4j.event.Level; import org.slf4j.event.Level;
import java.util.HashMap;
import java.util.Map;
/** /**
* Responsible for keeping state of whether the threshold of the --fail-on-severity flag has been hit. * Responsible for keeping state of whether the threshold of the --fail-on-severity flag has been hit.
*/ */
public class LogLevelRecorder public class 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 final Level logThreshold; private final Level logThreshold;
private boolean metThreshold = false; private boolean metThreshold = false;
public LogLevelRecorder( String threshold ) public LogLevelRecorder( String threshold )
{ {
Level level = Level.valueOf( threshold ); Level level = determineThresholdLevel( threshold );
if ( level.toInt() < Level.WARN.toInt() ) if ( level.toInt() < Level.WARN.toInt() )
{ {
throw new IllegalArgumentException( "Logging severity thresholds can only be set to WARN or ERROR" ); throw new IllegalArgumentException( "Logging severity thresholds can only be set to WARN or ERROR" );
@ -40,6 +52,19 @@ public LogLevelRecorder( String threshold )
logThreshold = level; logThreshold = level;
} }
private 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;
}
public void record( Level logLevel ) public void record( Level logLevel )
{ {
if ( !metThreshold && logLevel.toInt() >= logThreshold.toInt() ) if ( !metThreshold && logLevel.toInt() >= logThreshold.toInt() )

View File

@ -22,7 +22,10 @@
import org.junit.Test; import org.junit.Test;
import org.slf4j.event.Level; import org.slf4j.event.Level;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
public class LogLevelRecorderTest public class LogLevelRecorderTest
{ {
@ -41,9 +44,23 @@ public void failsOnLowerThanWarn ()
new LogLevelRecorder( "INFO" ); new LogLevelRecorder( "INFO" );
} }
@Test( expected = IllegalArgumentException.class ) @Test
public void createsLogLevelRecorderWithWarning()
{
LogLevelRecorder logLevelRecorder = new LogLevelRecorder( "WARNING" );
logLevelRecorder.record( Level.ERROR );
assertTrue( logLevelRecorder.metThreshold() );
}
@Test
public void failsOnUnknownLogLevel () public void failsOnUnknownLogLevel ()
{ {
new LogLevelRecorder( "SEVERE" ); Throwable thrown = assertThrows( IllegalArgumentException.class, () -> new LogLevelRecorder( "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" ) );
} }
} }