LogFormatter { public <Message extends PMessage<Message, Field>, Field extends PField> String format(Message message) { ByteArrayOutputStream out = new ByteArrayOutputStream(); formatTo(out, message); return new String(out.toByteArray(), StandardCharsets.UTF_8); } LogFormatter(FieldHandler... fieldHandlers);  LogFormatter(boolean pretty, FieldHandler... fieldHandlers);  LogFormatter(boolean pretty, Collection<FieldHandler> fieldHandlers); void formatTo(OutputStream out, Message message); String format(Message message);  }
@Test public void testFormat_null() { LogFormatter formatter = new LogFormatter(true); assertThat(formatter.format(null), is("null")); }
@Test public void testFormat_EverythingDefault() throws IOException { Containers containers = MessageStreams.resource("/compat/binary.data", new BinarySerializer(), Containers.kDescriptor) .findFirst() .orElseThrow(() -> new AssertionError("resource")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); new PrettySerializer().config().serialize(baos, containers); assertThat(new LogFormatter(true).format(containers), is(equalToLines(new String(baos.toByteArray(), UTF_8)))); }
@Test public void testFormat() { OptionalFields credentials = OptionalFields .builder() .setStringValue("username") .setBinaryValue(Binary.fromBase64("password")) .build(); LogFormatter formatter = new LogFormatter((writer, field, value) -> { if (field.getName() .contains("binary")) { writer.append("\"********\""); return true; } return false; }); assertThat(formatter.format(credentials), is("providence.OptionalFields{stringValue=\"username\",binaryValue=\"********\"}")); }
@Test public void testFormat_pretty() { OptionalFields credentials = OptionalFields .builder() .setStringValue("username") .setBinaryValue(Binary.fromBase64("password")) .build(); LogFormatter formatter = new LogFormatter(true, (writer, field, value) -> { if (field.getName() .contains("binary")) { writer.append("\"********\""); return true; } return false; }); assertThat(formatter.format(credentials), is("providence.OptionalFields {\n" + " stringValue = \"username\"\n" + " binaryValue = \"********\"\n" + "}")); }