@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); }
public TestDatarouterParentLog4j2Configuration() throws IllegalAccessException{ addFilter(RegexFilter.createFilter(".*password.*", null, true, Result.DENY, Result.NEUTRAL)); FileAppender testFileAppender = Log4j2Configurator.createFileAppender(TEST_APPENDER_NAME, TEST_FILE_NAME, defaultPattern); addAppender(testFileAppender); addLoggerConfig(getClass().getPackage().getName(), Level.ALL, false, testFileAppender); }
@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)); }
@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)); }
@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)); }
@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)); }
@Test public void shouldNotFilterWhenLogEventIsNull() { // given / when Result result = log4JFilter.filter(null); // then assertThat(result, equalTo(Result.NEUTRAL)); }
@Test public void shouldFilterSensitiveStringMessage() { // given / when Result result = log4JFilter.filter(null, null, null, SENSITIVE_COMMAND); // then assertThat(result, equalTo(Result.DENY)); }
@Test public void shouldNotFilterNormalStringMessage() { // given / when Result result = log4JFilter.filter(null, null, null, NORMAL_COMMAND); // then assertThat(result, equalTo(Result.NEUTRAL)); }
@Test public void shouldNotFilterNonCommandStringMessage() { // given / when Result result = log4JFilter.filter(null, null, null, OTHER_COMMAND); // then assertThat(result, equalTo(Result.NEUTRAL)); }
@Test public void shouldReturnNeutralForNullMessage() { // given / when Result result = log4JFilter.filter(null, null, null, null); // then assertThat(result, equalTo(Result.NEUTRAL)); }
@Test public void shouldFilterSensitiveObjectMessage() { // given / when Result result = log4JFilter.filter(null, null, null, SENSITIVE_COMMAND, new Exception()); // then assertThat(result, equalTo(Result.DENY)); }
@Test public void shouldNotFilterNullObjectParam() { // given / when Result result = log4JFilter.filter(null, null, null, (Object) null, new Exception()); // then assertThat(result, equalTo(Result.NEUTRAL)); }
@Test public void shouldNotFilterIrrelevantMessage() { // given / when Result result = log4JFilter.filter(null, null, null, OTHER_COMMAND, new Exception()); // then assertThat(result, equalTo(Result.NEUTRAL)); }
@Test public void shouldNotFilterNonSensitiveCommand() { // given / when Result result = log4JFilter.filter(null, null, null, NORMAL_COMMAND, new Exception()); // then assertThat(result, equalTo(Result.NEUTRAL)); }
@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)); }
@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)); }
@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)); }
@Test public void shouldNotFilterNullMessage() { // given / when Result result = log4JFilter.filter(null, null, null, null, new Exception()); // then assertThat(result, equalTo(Result.NEUTRAL)); }
@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)); }
@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)); }
@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; }