@Test public void testIncludeNullDelimiterTrue() throws Exception { final AbstractJacksonLayout layout = JsonLayout.newBuilder() .setCompact(true) .setIncludeNullDelimiter(true) .build(); final String str = layout.toSerializable(LogEventFixtures.createLogEvent()); assertTrue(str.endsWith("\0")); }
@Test public void testContentType() { final AbstractJacksonLayout layout = JsonLayout.createDefaultLayout(); assertEquals("application/json; charset=UTF-8", layout.getContentType()); }
@BeforeClass public static void setUpStatic() { appender = OutputStreamAppender .newBuilder() .setName("test stream") .setTarget(baos) .setLayout(JsonLayout.createLayout(false, true, false, true, true, StandardCharsets.UTF_8)) .build(); final Logger logger = (Logger) LogManager.getLogger(LoggingRequestLogger.class); appender.start(); logger.addAppender(appender); }
/** * Returns appropriate JSON header. * * @return a byte array containing the header, opening the JSON array. */ @Override public byte[] getHeader() { if (!this.complete) { return null; } final StringBuilder buf = new StringBuilder(); final String str = serializeToString(getHeaderSerializer()); if (str != null) { buf.append(str); } buf.append(this.eol); return getBytes(buf.toString()); }
/** * Returns appropriate JSON footer. * * @return a byte array containing the footer, closing the JSON array. */ @Override public byte[] getFooter() { if (!this.complete) { return null; } final StringBuilder buf = new StringBuilder(); buf.append(this.eol); final String str = serializeToString(getFooterSerializer()); if (str != null) { buf.append(str); } buf.append(this.eol); return getBytes(buf.toString()); }
final boolean includeStacktrace) { final boolean encodeThreadContextAsList = properties && propertiesAsList; return new JsonLayout(config, locationInfo, properties, encodeThreadContextAsList, complete, compact, eventEol, headerPattern, footerPattern, charset, includeStacktrace, false, false);
/** * @return The content type. */ @Override public String getContentType() { return CONTENT_TYPE + "; charset=" + this.getCharset(); }
/** * Creates a JSON Layout using the default settings. Useful for testing. * * @return A JSON Layout. */ public static JsonLayout createDefaultLayout() { return new JsonLayout(new DefaultConfiguration(), false, false, false, false, false, false, DEFAULT_HEADER, DEFAULT_FOOTER, StandardCharsets.UTF_8, true, false, false); }
@Test public void testIncludeNullDelimiterFalse() throws Exception { final AbstractJacksonLayout layout = JsonLayout.newBuilder() .setCompact(true) .setIncludeNullDelimiter(false) .build(); final String str = layout.toSerializable(LogEventFixtures.createLogEvent()); assertFalse(str.endsWith("\0")); }
@Test public void testDefaultCharset() { final AbstractJacksonLayout layout = JsonLayout.createDefaultLayout(); assertEquals(StandardCharsets.UTF_8, layout.getCharset()); }
@Override public JsonLayout build() { final boolean encodeThreadContextAsList = isProperties() && propertiesAsList; final String headerPattern = toStringOrNull(getHeader()); final String footerPattern = toStringOrNull(getFooter()); return new JsonLayout(getConfiguration(), isLocationInfo(), isProperties(), encodeThreadContextAsList, isComplete(), isCompact(), getEventEol(), headerPattern, footerPattern, getCharset(), isIncludeStacktrace(), isStacktraceAsString(), isIncludeNullDelimiter()); }
private String prepareJSONForStacktraceTests(final boolean stacktraceAsString) { final Log4jLogEvent expected = LogEventFixtures.createLogEvent(); // @formatter:off final AbstractJacksonLayout layout = JsonLayout.newBuilder() .setCompact(true) .setIncludeStacktrace(true) .setStacktraceAsString(stacktraceAsString) .build(); // @formatter:off return layout.toSerializable(expected); }
@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 public void testAdditionalFields() throws Exception { final AbstractJacksonLayout layout = JsonLayout.newBuilder() .setLocationInfo(false) .setProperties(false) .setComplete(false) .setCompact(true) .setEventEol(false) .setIncludeStacktrace(false) .setAdditionalFields(new KeyValuePair[] { new KeyValuePair("KEY1", "VALUE1"), new KeyValuePair("KEY2", "${java:runtime}"), }) .setCharset(StandardCharsets.UTF_8) .setConfiguration(ctx.getConfiguration()) .build(); final String str = layout.toSerializable(LogEventFixtures.createLogEvent()); assertTrue(str, str.contains("\"KEY1\":\"VALUE1\"")); assertTrue(str, str.contains("\"KEY2\":\"" + new JavaLookup().getRuntime() + "\"")); }
@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 testUdpAppender() throws Exception { try { udpServer.latch.await(); } catch (final InterruptedException ex) { ex.printStackTrace(); } // @formatter:off final SocketAppender appender = SocketAppender.newBuilder() .withProtocol(Protocol.UDP) .withPort(tcpServer.getLocalPort()) .withReconnectDelayMillis(-1) .withName("test") .withImmediateFail(false) .withLayout(JsonLayout.newBuilder().setProperties(true).build()) .build(); // @formatter:on appender.start(); // set appender on root and set level to debug logger.addAppender(appender); logger.setAdditive(false); logger.setLevel(Level.DEBUG); logger.debug("This is a udp message"); final LogEvent event = udpServer.getQueue().poll(3, TimeUnit.SECONDS); assertNotNull("No event retrieved", event); assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This is a udp message")); assertTrue("Message not delivered via UDP", udpServer.getCount() > 0); }
@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 + "\""))); }
final AbstractJacksonLayout layout = JsonLayout.newBuilder() .setConfiguration(configuration) .setLocationInfo(true)
@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 + "\""))); }
@Test public void testDefaultProtocol() throws Exception { // @formatter:off final SocketAppender appender = SocketAppender.newBuilder() .withPort(tcpServer.getLocalPort()) .withReconnectDelayMillis(-1) .withName("test") .withImmediateFail(false) .withLayout(JsonLayout.newBuilder().setProperties(true).build()) .build(); // @formatter:on assertNotNull(appender); appender.stop(); }