Last active
December 16, 2016 19:13
-
-
Save noel-yap/904cd1a75d4820ea689813314d925b9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copied from https://github.com/Mahoney/slf4j-test/issues/3 and slightly modified. | |
import org.hamcrest.BaseMatcher; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.slf4j.event.Level; | |
import org.slf4j.event.LoggingEvent; | |
import org.slf4j.helpers.MessageFormatter; | |
public class LoggerMatcher { | |
public static Matcher<LoggingEvent> logWithLevelAndMessage(final Level level, final String substring) { | |
return new BaseMatcher<LoggingEvent>() { | |
@Override | |
public boolean matches(Object item) { | |
if (item instanceof LoggingEvent) { | |
final LoggingEvent le = (LoggingEvent) item; | |
return le.getLevel().equals(level) && getFormattedMessage(le).contains(substring); | |
} | |
return false; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description | |
.appendText("LoggingEvent with level ") | |
.appendValue(level) | |
.appendText(" and a formatted message containing '") | |
.appendText(substring) | |
.appendText("'"); | |
} | |
}; | |
} | |
private static String getFormattedMessage(LoggingEvent event) { | |
return MessageFormatter.arrayFormat(event.getMessage(), event.getArgumentArray()).getMessage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment