org.apache.logging.log4j.core.Filter.Result Java Examples

The following examples show how to use org.apache.logging.log4j.core.Filter.Result. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RegexFilterTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testThresholds() throws Exception {
    RegexFilter filter = RegexFilter.createFilter(".* test .*", null, false, null, null);
    filter.start();
    assertTrue(filter.isStarted());
    assertSame(Filter.Result.NEUTRAL,
            filter.filter(null, Level.DEBUG, null, (Object) "This is a test message", (Throwable) null));
    assertSame(Filter.Result.DENY, filter.filter(null, Level.ERROR, null, (Object) "This is not a test",
            (Throwable) null));
    LogEvent event = Log4jLogEvent.newBuilder() //
            .setLevel(Level.DEBUG) //
            .setMessage(new SimpleMessage("Another test message")) //
            .build();
    assertSame(Filter.Result.NEUTRAL, filter.filter(event));
    event = Log4jLogEvent.newBuilder() //
            .setLevel(Level.ERROR) //
            .setMessage(new SimpleMessage("test")) //
            .build();
    assertSame(Filter.Result.DENY, filter.filter(event));
    filter = RegexFilter.createFilter(null, null, false, null, null);
    assertNull(filter);
}
 
Example #2
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldFilterSensitiveObjectMessage() {
	// given / when
	Result result = log4JFilter.filter(null, null, null, SENSITIVE_COMMAND, new Exception());
	
	// then
	assertThat(result, equalTo(Result.DENY));
}
 
Example #3
Source File: RegexFilterTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoMsg() throws Exception {
    final RegexFilter filter = RegexFilter.createFilter(".* test .*", null, false, null, null);
    filter.start();
    assertTrue(filter.isStarted());
    assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
    assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Message) null, (Throwable) null));
    assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, null, (Object[]) null));
}
 
Example #4
Source File: RegexFilterTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDotAllPattern() throws Exception {
    final String singleLine = "test single line matches";
    final String multiLine = "test multi line matches\nsome more lines";
    final RegexFilter filter = RegexFilter.createFilter(".*line.*", new String[] { "DOTALL", "COMMENTS" }, false,
            Filter.Result.DENY, Filter.Result.ACCEPT);
    final Result singleLineResult = filter.filter(null, null, null, (Object) singleLine, (Throwable) null);
    final Result multiLineResult = filter.filter(null, null, null, (Object) multiLine, (Throwable) null);
    assertThat(singleLineResult, equalTo(Result.DENY));
    assertThat(multiLineResult, equalTo(Result.DENY));
}
 
Example #5
Source File: Log4jLogger_impl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param level a log4j level that's equal or above (ERROR is highest) the level being tested
 * @param marker - the marker that needs to be there to allow this
 * @return - the result of running the logger filter test if there is one, else NEUTRAL
 */
private Result filterTest(org.apache.logging.log4j.Level level, org.apache.logging.log4j.Marker marker) {
  Filter filter = coreLogger.get().getFilter();
  if (null != filter) {
    return filter.filter(coreLogger, level, marker, (String) null, (Object[])null);
  }
  return Result.NEUTRAL;
}
 
Example #6
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterNullMessage() {
	// given / when
	Result result = log4JFilter.filter(null, null, null, null, new Exception());
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #7
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterNonCommandMessage() {
	// given
	Message message = mockMessage(OTHER_COMMAND);
	
	// when
	Result result = log4JFilter.filter(null, null, null, message, new Exception());
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #8
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterNonSensitiveMessage() {
	// given
	Message message = mockMessage(NORMAL_COMMAND);
	
	// when
	Result result = log4JFilter.filter(null, null, null, message, new Exception());
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #9
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldFilterSensitiveMessage() {
	// given
	Message message = mockMessage(SENSITIVE_COMMAND);
	
	// when
	Result result = log4JFilter.filter(null, null, null, message, new Exception());
	
	// then
	assertThat(result, equalTo(Result.DENY));
}
 
Example #10
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterNonSensitiveCommand() {
	// given / when
	Result result = log4JFilter.filter(null, null, null, NORMAL_COMMAND, new Exception());
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #11
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterIrrelevantMessage() {
	// given / when
	Result result = log4JFilter.filter(null, null, null, OTHER_COMMAND, new Exception());
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #12
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterNullObjectParam() {
	// given / when
	Result result = log4JFilter.filter(null, null, null, (Object) null, new Exception());
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #13
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldReturnNeutralForNullMessage() {
	// given / when
	Result result = log4JFilter.filter(null, null, null, null);
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #14
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterNonCommandStringMessage() {
	// given / when
	Result result = log4JFilter.filter(null, null, null, OTHER_COMMAND);
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #15
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterNormalStringMessage() {
	// given / when
	Result result = log4JFilter.filter(null, null, null, NORMAL_COMMAND);
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #16
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldFilterSensitiveStringMessage() {
	// given / when
	Result result = log4JFilter.filter(null, null, null, SENSITIVE_COMMAND);
	
	// then
	assertThat(result, equalTo(Result.DENY));
}
 
Example #17
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterWhenLogEventIsNull() {
	// given / when
	Result result = log4JFilter.filter(null);
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #18
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterLogEventWithNullMessage() {
	// given
	Message message = mockMessage(null);
	LogEvent event = Mockito.mock(LogEvent.class);
	when(event.getMessage()).thenReturn(message);
	
	// when
	Result result = log4JFilter.filter(event);
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #19
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterNonCommandLogEvent() {
	// given
	Message message = mockMessage(OTHER_COMMAND);
	LogEvent event = Mockito.mock(LogEvent.class);
	when(event.getMessage()).thenReturn(message);
	
	// when
	Result result = log4JFilter.filter(event);
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #20
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotFilterIrrelevantLogEvent() {
	// given
	Message message = mockMessage(NORMAL_COMMAND);
	LogEvent event = Mockito.mock(LogEvent.class);
	when(event.getMessage()).thenReturn(message);
	
	// when
	Result result = log4JFilter.filter(event);
	
	// then
	assertThat(result, equalTo(Result.NEUTRAL));
}
 
Example #21
Source File: Log4JFilterTest.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldFilterSensitiveLogEvent() {
	// given
	Message message = mockMessage(SENSITIVE_COMMAND);
	LogEvent event = Mockito.mock(LogEvent.class);
	when(event.getMessage()).thenReturn(message);
	
	// when
	Result result = log4JFilter.filter(event);
	
	// then
	assertThat(result, equalTo(Result.DENY));
}
 
Example #22
Source File: LoggerLoader.java    From Zebra with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public static synchronized void init() {
	final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
	final Configuration config = ctx.getConfiguration();
	Layout layout = PatternLayout.newBuilder().withPattern("%d{yyyy-MM-dd HH:mm:ss}:%p %t %c - %m%n")
	      .withConfiguration(config).withRegexReplacement(null).withCharset(null).withAlwaysWriteExceptions(true)
	      .withNoConsoleNoAnsi(false).withHeader(null).withFooter(null).build();

	// file info
	Filter fileInfoFilter = ThresholdFilter.createFilter(Level.ERROR, Result.DENY, Result.ACCEPT);
	Appender fileInfoAppender = RollingFileAppender.createAppender(LOG_ROOT + "/zebra.log", LOG_ROOT
	      + "/zebra.log.%d{yyyy-MM-dd}.gz", "true", "FileInfo", "true", "4000", "true",
	      TimeBasedTriggeringPolicy.createPolicy("1", "true"),
	      ZebraRolloverStrategy.createStrategy("30", "1", null, Deflater.DEFAULT_COMPRESSION + "", config), layout,
	      fileInfoFilter, "false", null, null, config);
	config.addAppender(fileInfoAppender);
	fileInfoAppender.start();
	AppenderRef fileInfoRef = AppenderRef.createAppenderRef("FileInfo", null, fileInfoFilter);

	// console error
	Appender consoleErrorAppender = ConsoleAppender.createAppender(layout, null, "SYSTEM_ERR", "ConsoleError",
	      "false", "false");
	config.addAppender(consoleErrorAppender);
	consoleErrorAppender.start();

	// console info
	Filter consoleWarnFilter = ThresholdFilter.createFilter(Level.ERROR, Result.DENY, Result.NEUTRAL);
	Appender consoleWarnAppender = ConsoleAppender.createAppender(layout, consoleWarnFilter, "SYSTEM_OUT",
	      "ConsoleWarn", "false", "false");
	config.addAppender(consoleWarnAppender);
	consoleWarnAppender.start();
	AppenderRef consoleWarnAppenderRef = AppenderRef.createAppenderRef("ConsoleWarn", Level.WARN, consoleWarnFilter);
	AppenderRef consoleErrorAppenderRef = AppenderRef.createAppenderRef("ConsoleError", Level.WARN, null);

	AppenderRef[] refs = new AppenderRef[] { consoleErrorAppenderRef, consoleWarnAppenderRef, fileInfoRef };

	LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "com.dianping.zebra", "true", refs,
	      null, config, null);
	loggerConfig.addAppender(consoleErrorAppender, Level.ERROR, null);
	loggerConfig.addAppender(consoleWarnAppender, Level.INFO, null);
	loggerConfig.addAppender(fileInfoAppender, Level.INFO, null);

	config.addLogger("com.dianping.zebra", loggerConfig);

	ctx.updateLoggers();

	context = ctx;
}
 
Example #23
Source File: TestAppender.java    From iaf with Apache License 2.0 4 votes vote down vote up
public B useIbisThreadFilter(String rejectRegex) {
	IbisThreadFilter threadFilter = IbisThreadFilter.createFilter(rejectRegex, Level.WARN, Result.DENY, Result.NEUTRAL);
	return setFilter(threadFilter);
}
 
Example #24
Source File: LoggingConfigurationFactory.java    From luna with MIT License 3 votes vote down vote up
/**
 * Returns the filter for an appender with {@code minLevel} and {@code maxLevel}.
 *
 * @param builder The builder.
 * @param minLevel The minimum logging level.
 * @param maxLevel The maximum logging level.
 * @return The filter.
 */
private FilterComponentBuilder getLevelRangeFilter(ConfigurationBuilder<BuiltConfiguration> builder,
                                                   String minLevel, String maxLevel) {
    return builder.newFilter("LevelRangeFilter", Result.ACCEPT, Result.DENY).
            addAttribute("minLevel", minLevel).
            addAttribute("maxLevel", maxLevel);
}