EcsFormatter extends ExtFormatter { @Override public String format(ExtLogRecord record) { StringBuilder builder = new StringBuilder(); EcsJsonSerializer.serializeObjectStart(builder, record.getMillis()); EcsJsonSerializer.serializeLogLevel(builder, record.getLevel().getName()); EcsJsonSerializer.serializeFormattedMessage(builder, record.getFormattedMessage()); EcsJsonSerializer.serializeServiceName(builder, serviceName); EcsJsonSerializer.serializeEventDataset(builder, eventDataset); EcsJsonSerializer.serializeThreadName(builder, record.getThreadName()); EcsJsonSerializer.serializeLoggerName(builder, record.getLoggerName()); EcsJsonSerializer.serializeMDC(builder, record.getMdcCopy()); String ndc = record.getNdc(); if (ndc != null && !ndc.isEmpty()) { EcsJsonSerializer.serializeTagStart(builder); for (String tag : ndc.split("\\.")) { EcsJsonSerializer.serializeSingleTag(builder, tag); } EcsJsonSerializer.serializeTagEnd(builder); } if (includeOrigin && record.getSourceFileName() != null && record.getSourceMethodName() != null) { EcsJsonSerializer.serializeOrigin(builder, record.getSourceFileName(), record.getSourceMethodName(), record.getSourceLineNumber()); } Throwable throwable = record.getThrown(); if (throwable != null) { EcsJsonSerializer.serializeException(builder, throwable, stackTraceAsArray); } EcsJsonSerializer.serializeObjectEnd(builder); return builder.toString(); } EcsFormatter(); @Override String format(ExtLogRecord record); void setIncludeOrigin(final boolean includeOrigin); void setServiceName(final String serviceName); void setStackTraceAsArray(final boolean stackTraceAsArray); void setEventDataset(String eventDataset);  }
@Test void testSingleNDCInformationLogging() throws Exception { record.setNdc("exampleTag"); JsonNode result = objectMapper.readTree(formatter.format(record)); assertThat(result.get("tags")).hasSize(1); assertThat(result.get("tags").get(0).textValue()).isEqualTo("exampleTag"); }
@Test void testMultipleNDCInformationLogging() throws Exception { record.setNdc("exampleTag1.exampleTag2"); JsonNode result = objectMapper.readTree(formatter.format(record)); assertThat(result.get("tags")).hasSize(2); assertThat(result.get("tags").get(0).textValue()).isEqualTo("exampleTag1"); assertThat(result.get("tags").get(1).textValue()).isEqualTo("exampleTag2"); }
@Test void testExceptionLogging() throws Exception { record.setThrown(new RuntimeException("Example Exception Message") { @Override public void printStackTrace(PrintWriter pw) { pw.println("co.elastic.logging.jboss.logmanager.EcsFormatterTest$1: Example Exception Message"); pw.println("\tat co.elastic.logging.jboss.logmanager.EcsFormatterTest.testExceptionLogging(EcsFormatterTest.java:125)"); } }); JsonNode result = objectMapper.readTree(formatter.format(record)); assertThat(result.get("error.type").textValue()).isEqualTo("co.elastic.logging.jboss.logmanager.EcsFormatterTest$1"); assertThat(result.get("error.message").textValue()).isEqualTo("Example Exception Message"); assertThat(result.get("error.stack_trace").textValue()) .isEqualTo("co.elastic.logging.jboss.logmanager.EcsFormatterTest$1: Example Exception Message\n" + "\tat co.elastic.logging.jboss.logmanager.EcsFormatterTest.testExceptionLogging(EcsFormatterTest.java:125)\n"); }