StructuredLayout extends AbstractLayout<byte[]> { public byte[] toByteArray(final LogEvent event) { if (event.getMessage().getParameters().length != 1 || !(event.getMessage().getParameters()[0] instanceof StructuredLogMessage)) { throw new IllegalArgumentException( "LogEvent must contain a single parameter of type StructuredLogMessage"); } final StructuredLogMessage schemaAndValue = (StructuredLogMessage) event.getMessage().getParameters()[0]; final Struct logRecord = logRecordStructBuilderFactory.get() .withLoggerName(event.getLoggerName()) .withLevel(event.getLevel().name()) .withTimeMs(event.getTimeMillis()) .withMessageSchemaAndValue(schemaAndValue.getMessage()) .build(); return serializer.apply(logRecord); } StructuredLayout(final Function<Struct, byte[]> serializer);   StructuredLayout(
      final Function<Struct, byte[]> serializer,
      final Supplier<LogRecordBuilder<Struct>> logRecordBuilderFactory); byte[] toByteArray(final LogEvent event); String getContentType(); byte[] toSerializable(final LogEvent event);  }
@Test public void shouldSerializeMessageCorrectly() { when(log4jMessage.getParameters()).thenReturn(new Object[]{logMessage}); final Schema logSchema = mock(Schema.class); final Struct logRecord = mock(Struct.class); when(logRecord.schema()).thenReturn(logSchema); when(builder.build()).thenReturn(logRecord); final byte[] serialized = layout.toByteArray(logEvent); verifyBeforeBuild(builder, io -> io.verify(builder).withLoggerName(LOGGER_NAME)); verifyBeforeBuild(builder, io -> io.verify(builder).withTimeMs(LOG_TIME_MS)); verifyBeforeBuild(builder, io -> io.verify(builder).withLevel(LOG_LEVEL.name())); verifyBeforeBuild( builder, io -> io.verify(builder).withMessageSchemaAndValue(schemaAndValue)); verify(converter, times(1)) .apply(logRecord); assertThat(serialized, equalTo(SERIALIZED_MSG)); }
@Test public void shouldThrowIfInvalidNumberParameters() { expectInvalidParameters(); layout.toByteArray(logEvent); }
@Test public void shouldThrowIfInvalidParameterType() { expectInvalidParameters(123); layout.toByteArray(logEvent); }