Skip to content

Commit

Permalink
minimum event level filter for logback
Browse files Browse the repository at this point in the history
warning: this breaks current behavior, since default filter level is WARN, events with lower levels will not be recorded anymore. See logback/README to change filter level.
  • Loading branch information
galmeida committed Oct 8, 2015
1 parent 5eb88ba commit e41d1d6
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
2 changes: 2 additions & 0 deletions raven-logback/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ In the `logback.xml` file set:
<!--<ravenFactory>net.kencochrane.raven.DefaultRavenFactory</ravenFactory>-->
<!-- Optional, allows setting app release information -->
<!-- <release>1.0.0</release>
<!-- Optional, events with level bellow minLevel won't be sent, defaults to WARN -->
<!-- <minLevel>ERROR</minLevel>
</appender>
<root level="warn">
<appender-ref ref="Sentry"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public class SentryAppender extends AppenderBase<ILoggingEvent> {
* Might be null in which case the release information will not be sent with the event.
*/
protected String release;
/**
* If set, only events with level = minLevel and up will be recorded.
*/
protected Level minLevel = Level.WARN;
/**
* Additional tags to be sent to sentry.
* <p>
Expand Down Expand Up @@ -135,6 +139,9 @@ protected void append(ILoggingEvent iLoggingEvent) {
if (raven == null)
initRaven();

if (minLevel != null && !iLoggingEvent.getLevel().isGreaterOrEqual(minLevel))
return;

Event event = buildEvent(iLoggingEvent);
raven.sendEvent(event);
} catch (Exception e) {
Expand Down Expand Up @@ -292,6 +299,11 @@ public void setRavenFactory(String ravenFactory) {
public void setRelease(String release) {
this.release = release;
}

public void setMinLevel(String minLevel) {
this.minLevel = Level.toLevel(minLevel, Level.WARN);
}

/**
* Set the tags that should be sent along with the events.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ public class SentryAppenderEventBuildingTest {
@Injectable
private Context mockContext = null;
private String mockExtraTag = "60f42409-c029-447d-816a-fb2722913c93";
private String mockMinLevel = "ALL";

@BeforeMethod
public void setUp() throws Exception {
new MockUpStatusPrinter();
sentryAppender = new SentryAppender(mockRaven);
sentryAppender.setContext(mockContext);
sentryAppender.setExtraTags(mockExtraTag);
sentryAppender.setMinLevel(mockMinLevel);

new NonStrictExpectations() {{
final BasicStatusManager statusManager = new BasicStatusManager();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package net.kencochrane.raven.logback;

import ch.qos.logback.classic.Level;
import ch.qos.logback.core.Context;
import mockit.Injectable;
import mockit.Tested;
import mockit.Verifications;
import net.kencochrane.raven.Raven;
import net.kencochrane.raven.event.Event;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/**
* @author Felipe G Almeida
*/
public class SentryAppenderEventLevelFilterTest {
@Tested
private SentryAppender sentryAppender = null;
@Injectable
private Raven mockRaven = null;
@Injectable
private Context mockContext = null;

@BeforeMethod
public void setUp() throws Exception {
new MockUpStatusPrinter();
sentryAppender = new SentryAppender(mockRaven);
sentryAppender.setContext(mockContext);
}

@DataProvider(name = "levels")
private Object[][] levelConversions() {
return new Object[][]{
{"ALL", 5},
{"TRACE", 5},
{"DEBUG", 4},
{"INFO", 3},
{"WARN", 2},
{"ERROR", 1},
{"error", 1},
{"xxx", 2},
{null, 2}};
}

@Test(dataProvider = "levels")
public void testLevelFilter(final String minLevel, final Integer expectedEvents) throws Exception {
sentryAppender.setMinLevel(minLevel);
sentryAppender.append(new MockUpLoggingEvent(null, null, Level.TRACE, null, null, null).getMockInstance());
sentryAppender.append(new MockUpLoggingEvent(null, null, Level.DEBUG, null, null, null).getMockInstance());
sentryAppender.append(new MockUpLoggingEvent(null, null, Level.INFO, null, null, null).getMockInstance());
sentryAppender.append(new MockUpLoggingEvent(null, null, Level.WARN, null, null, null).getMockInstance());
sentryAppender.append(new MockUpLoggingEvent(null, null, Level.ERROR, null, null, null).getMockInstance());

new Verifications() {{
mockRaven.sendEvent((Event) any);
minTimes = expectedEvents;
maxTimes = expectedEvents;
}};
}

@Test
public void testDefaultLevelFilter() throws Exception {
sentryAppender.append(new MockUpLoggingEvent(null, null, Level.TRACE, null, null, null).getMockInstance());
sentryAppender.append(new MockUpLoggingEvent(null, null, Level.DEBUG, null, null, null).getMockInstance());
sentryAppender.append(new MockUpLoggingEvent(null, null, Level.INFO, null, null, null).getMockInstance());
sentryAppender.append(new MockUpLoggingEvent(null, null, Level.WARN, null, null, null).getMockInstance());
sentryAppender.append(new MockUpLoggingEvent(null, null, Level.ERROR, null, null, null).getMockInstance());

new Verifications() {{
mockRaven.sendEvent((Event) any);
minTimes = 2;
maxTimes = 2;
}};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void setUp() throws Exception {
public void testRavenFailureDoesNotPropagate() throws Exception {
final SentryAppender sentryAppender = new SentryAppender(mockRaven);
sentryAppender.setContext(mockContext);
sentryAppender.setMinLevel("ALL");
new NonStrictExpectations() {{
mockRaven.sendEvent((Event) any);
result = new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public void tearDown() throws Exception {
}

@Test
public void testInfoLog() throws Exception {
public void testWarnLog() throws Exception {
assertThat(sentryStub.getEventCount(), is(0));
logger.info("This is a test");
logger.warn("This is a test");
assertThat(sentryStub.getEventCount(), is(1));
}

Expand Down

0 comments on commit e41d1d6

Please sign in to comment.