StructuredLoggerImpl implements StructuredLogger { public void info(final Supplier<SchemaAndValue> msgSupplier) { if (!inner.isInfoEnabled()) { return; } info(msgSupplier.get()); } StructuredLoggerImpl(final Logger inner); String getName(); void error(final Supplier<SchemaAndValue> msgSupplier); void error(final SchemaAndValue msg); void info(final Supplier<SchemaAndValue> msgSupplier); void info(final SchemaAndValue msg); void debug(final Supplier<SchemaAndValue> msgSupplier); void debug(final SchemaAndValue msg);  }
@Test public void shouldNotGetMessageIfLoggerDisabledForInfo() { when(innerLogger.isInfoEnabled()).thenReturn(false); logger.info(msgSupplier); verify(innerLogger).isInfoEnabled(); verifyNoMoreInteractions(innerLogger, msgSupplier); }
@Test public void shouldSerializeMessageToJsonString() throws IOException { final Schema msgSchema = SchemaBuilder.struct() .field("field1", Schema.STRING_SCHEMA) .field("field2", Schema.INT32_SCHEMA) .build(); final Struct msgStruct = new Struct(msgSchema); msgStruct.put("field1", "foobar"); msgStruct.put("field2", 123); final SchemaAndValue schemaAndValue = new SchemaAndValue(msgSchema, msgStruct); logger.info(schemaAndValue); verify(innerLogger).info(any(), captor.capture()); final String asString = captor.getValue().toString(); final Object deserialized = new ObjectMapper().readValue(asString, Object.class); assertThat(deserialized, instanceOf(Map.class)); assertThat( deserialized, equalTo( ImmutableMap.of( "field1", "foobar", "field2", 123))); }
@Test public void shouldLogInfoMessage() { logger.info(msg); verify(innerLogger, times(1)).info(eq(LOG_MSG), captor.capture()); assertThat(captor.getValue().getMessage(), is(msg)); }
@Test public void shouldLogInfoMessageGivenSupplier() { when(innerLogger.isInfoEnabled()).thenReturn(true); logger.info(msgSupplier); verify(msgSupplier, times(1)).get(); verify(innerLogger, times(1)).info(eq(LOG_MSG), captor.capture()); assertThat(captor.getValue().getMessage(), is(msg)); }