@Test public void testAppendWithKeyLookup() throws Exception { final Appender appender = ctx.getRequiredAppender("KafkaAppenderWithKeyLookup"); final LogEvent logEvent = createLogEvent(); Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy"); appender.append(logEvent); final List<ProducerRecord<byte[], byte[]>> history = kafka.history(); assertEquals(1, history.size()); final ProducerRecord<byte[], byte[]> item = history.get(0); assertNotNull(item); assertEquals(TOPIC_NAME, item.topic()); byte[] keyValue = format.format(date).getBytes(StandardCharsets.UTF_8); assertArrayEquals(item.key(), keyValue); assertEquals(LOG_MESSAGE, new String(item.value(), StandardCharsets.UTF_8)); }
@Test public void testAppendWithKey() throws Exception { final Appender appender = ctx.getRequiredAppender("KafkaAppenderWithKey"); final LogEvent logEvent = createLogEvent(); appender.append(logEvent); final List<ProducerRecord<byte[], byte[]>> history = kafka.history(); assertEquals(1, history.size()); final ProducerRecord<byte[], byte[]> item = history.get(0); assertNotNull(item); assertEquals(TOPIC_NAME, item.topic()); String msgKey = item.key().toString(); byte[] keyValue = "key".getBytes(StandardCharsets.UTF_8); assertArrayEquals(item.key(), keyValue); assertEquals(LOG_MESSAGE, new String(item.value(), StandardCharsets.UTF_8)); }
@Test public void testAsyncAppend() throws Exception { final Appender appender = ctx.getRequiredAppender("AsyncKafkaAppender"); appender.append(createLogEvent()); final List<ProducerRecord<byte[], byte[]>> history = kafka.history(); assertEquals(1, history.size()); final ProducerRecord<byte[], byte[]> item = history.get(0); assertNotNull(item); assertEquals(TOPIC_NAME, item.topic()); assertNull(item.key()); assertEquals(LOG_MESSAGE, new String(item.value(), StandardCharsets.UTF_8)); }
@Test public void testAppendWithLayout() throws Exception { final Appender appender = ctx.getRequiredAppender("KafkaAppenderWithLayout"); appender.append(createLogEvent()); final List<ProducerRecord<byte[], byte[]>> history = kafka.history(); assertEquals(1, history.size()); final ProducerRecord<byte[], byte[]> item = history.get(0); assertNotNull(item); assertEquals(TOPIC_NAME, item.topic()); assertNull(item.key()); assertEquals("[" + LOG_MESSAGE + "]", new String(item.value(), StandardCharsets.UTF_8)); }
@Test public void testAppendWithSerializedLayout() throws Exception { final Appender appender = ctx.getRequiredAppender("KafkaAppenderWithSerializedLayout"); final LogEvent logEvent = createLogEvent(); appender.append(logEvent); final List<ProducerRecord<byte[], byte[]>> history = kafka.history(); assertEquals(1, history.size()); final ProducerRecord<byte[], byte[]> item = history.get(0); assertNotNull(item); assertEquals(TOPIC_NAME, item.topic()); assertNull(item.key()); assertEquals(LOG_MESSAGE, deserializeLogEvent(item.value()).getMessage().getFormattedMessage()); }
@Test(expected = AppenderLoggingException.class) public void testAppendConnectError() throws Exception { final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .withIgnoreExceptions(false) .setUrl(new URL("http://localhost:"+(wireMockRule.port()+1)+"/test/log4j/")) .build(); appender.append(createLogEvent()); }
@Test(expected = AppenderLoggingException.class) public void testAppendError() throws Exception { wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")) .willReturn(FAILURE_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .withIgnoreExceptions(false) .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/")) .build(); appender.append(createLogEvent()); }
@Test public void testAppend() throws Exception { wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")) .willReturn(SUCCESS_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/")) .build(); appender.append(createLogEvent()); wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/")) .withHeader("Host", containing("localhost")) .withHeader("Content-Type", containing("application/json")) .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\""))); }
@Test public void testAppendMethodPut() throws Exception { wireMockRule.stubFor(put(urlEqualTo("/test/log4j/1234")) .willReturn(SUCCESS_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .setMethod("PUT") .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/1234")) .build(); appender.append(createLogEvent()); wireMockRule.verify(putRequestedFor(urlEqualTo("/test/log4j/1234")) .withHeader("Host", containing("localhost")) .withHeader("Content-Type", containing("application/json")) .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\""))); }
.setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/")) .build(); appender.append(createLogEvent());
@Test public void testAppendHttps() throws Exception { wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")) .willReturn(SUCCESS_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .setUrl(new URL("https://localhost:" + wireMockRule.httpsPort() + "/test/log4j/")) .setSslConfiguration(SslConfiguration.createSSLConfiguration(null, KeyStoreConfiguration.createKeyStoreConfiguration(TestConstants.KEYSTORE_FILE, TestConstants.KEYSTORE_PWD(), TestConstants.KEYSTORE_TYPE, null), TrustStoreConfiguration.createKeyStoreConfiguration(TestConstants.TRUSTSTORE_FILE, TestConstants.TRUSTSTORE_PWD(), TestConstants.TRUSTSTORE_TYPE, null))) .setVerifyHostname(false) .build(); appender.append(createLogEvent()); wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/")) .withHeader("Host", containing("localhost")) .withHeader("Content-Type", containing("application/json")) .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\""))); }
@Test public void testAppendCustomHeader() throws Exception { wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")) .willReturn(SUCCESS_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/")) .setHeaders(new Property[] { Property.createProperty("X-Test", "header value"), Property.createProperty("X-Runtime", "${java:runtime}") }) .build(); appender.append(createLogEvent()); wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/")) .withHeader("Host", containing("localhost")) .withHeader("X-Test", equalTo("header value")) .withHeader("X-Runtime", equalTo(JAVA_LOOKUP.getRuntime())) .withHeader("Content-Type", containing("application/json")) .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\""))); }
private void tryCallAppender(final LogEvent event) { try { appender.append(event); } catch (final RuntimeException ex) { handleAppenderError(ex); } catch (final Exception ex) { handleAppenderError(new AppenderLoggingException(ex)); } }
@Test public void deliversToAppenderRef() { // given Appender appender = mock(Appender.class); when(appender.isStarted()).thenReturn(true); Configuration configuration = mock(Configuration.class); String testAppenderRef = "testAppenderRef"; when(configuration.getAppender(testAppenderRef)).thenReturn(appender); FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration); String failedMessage = "test failed message"; // when failoverPolicy.deliver(failedMessage); // then verify(appender, times(1)).append(any(LogEvent.class)); }
@Test public void resolvesAppenderRefOnlyOnce() { // given Appender appender = mock(Appender.class); when(appender.isStarted()).thenReturn(true); Configuration configuration = mock(Configuration.class); String testAppenderRef = "testAppenderRef"; when(configuration.getAppender(testAppenderRef)).thenReturn(appender); FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration); String failedMessage = "test failed message"; // when failoverPolicy.deliver(failedMessage); failoverPolicy.deliver(failedMessage); // then verify(configuration, times(1)).getAppender(anyString()); verify(appender, times(2)).append(any(LogEvent.class)); }