|
25 | 25 | import java.nio.file.NoSuchFileException; |
26 | 26 | import java.nio.file.Paths; |
27 | 27 | import java.nio.file.StandardOpenOption; |
| 28 | +import java.util.List; |
28 | 29 | import java.util.concurrent.atomic.AtomicBoolean; |
29 | 30 |
|
30 | 31 | import org.junit.jupiter.api.AfterEach; |
|
40 | 41 | import ch.qos.logback.classic.Logger; |
41 | 42 | import ch.qos.logback.classic.pattern.RootCauseFirstThrowableProxyConverter; |
42 | 43 | import ch.qos.logback.classic.spi.LoggingEvent; |
| 44 | +import org.slf4j.event.KeyValuePair; |
43 | 45 | import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor; |
44 | 46 | import software.amazon.lambda.powertools.common.stubs.TestLambdaContext; |
45 | 47 | import software.amazon.lambda.powertools.logging.PowertoolsLogging; |
| 48 | +import software.amazon.lambda.powertools.logging.argument.StructuredArguments; |
46 | 49 | import software.amazon.lambda.powertools.logging.internal.handler.PowertoolsLogEnabled; |
47 | 50 | import software.amazon.lambda.powertools.logging.logback.LambdaEcsEncoder; |
| 51 | +import software.amazon.lambda.powertools.logging.logback.LambdaJsonEncoder; |
48 | 52 |
|
49 | 53 | @Order(3) |
50 | 54 | class LambdaEcsEncoderTest { |
@@ -179,4 +183,61 @@ private void setMDC() { |
179 | 183 | MDC.put(PowertoolsLoggedFields.CORRELATION_ID.getName(), "test-correlation-id"); |
180 | 184 | } |
181 | 185 |
|
| 186 | + @Test |
| 187 | + void shouldLogKeyValuePairs() { |
| 188 | + // GIVEN |
| 189 | + LambdaEcsEncoder encoder = new LambdaEcsEncoder(); |
| 190 | + encoder.start(); |
| 191 | + |
| 192 | + Object[] arguments = { |
| 193 | + "argument_01", |
| 194 | + StructuredArguments.entry("structured_argument_01_retain", "retained"), |
| 195 | + StructuredArguments.entry("structured_argument_02_overwrite", "to_be_overwritten") |
| 196 | + }; |
| 197 | + LoggingEvent keyValuePairsLoggingEvent = new LoggingEvent("fqcn", logger, Level.INFO, "Key Value Pairs Test with argument: {}", |
| 198 | + null, arguments); |
| 199 | + |
| 200 | + MDC.put("mdc_01_retain", "retained"); |
| 201 | + MDC.put("mdc_02_overwrite", "to_be_overwritten"); |
| 202 | + |
| 203 | + keyValuePairsLoggingEvent.setKeyValuePairs(List.of( |
| 204 | + new KeyValuePair("key_01_string", "value_01"), |
| 205 | + new KeyValuePair("key_02_numeric", 2), |
| 206 | + new KeyValuePair("key_03_decimal", 2.333), |
| 207 | + new KeyValuePair("key_04_null", null), |
| 208 | + new KeyValuePair("", "value_05_empty_key"), |
| 209 | + new KeyValuePair(null, "value_06_null_key"), |
| 210 | + new KeyValuePair("key_07_boolean_true", true), |
| 211 | + new KeyValuePair("key_08_boolean_false", false), |
| 212 | + new KeyValuePair("mdc_02_overwrite", "overwritten_by_kvp"), |
| 213 | + new KeyValuePair("structured_argument_02_overwrite", "overwritten_by_kvp") |
| 214 | + )); |
| 215 | + |
| 216 | + // WHEN |
| 217 | + byte[] encoded = encoder.encode(keyValuePairsLoggingEvent); |
| 218 | + String result = new String(encoded, StandardCharsets.UTF_8); |
| 219 | + |
| 220 | + // THEN |
| 221 | + assertThat(result) |
| 222 | + // Arguments |
| 223 | + .contains("Key Value Pairs Test with argument: argument_01") |
| 224 | + .contains("\"structured_argument_01_retain\":\"retained\"") |
| 225 | + // .doesNotContain("\"structured_argument_02_overwrite\":\"to_be_overwritten\"") TODO: Deduplication not implemented vor Arguments |
| 226 | + // MDC |
| 227 | + .contains("\"mdc_01_retain\":\"retained\"") |
| 228 | + // .doesNotContain("\"mdc_02_overwrite\":\"to_be_overwritten\"") TODO: Deduplication not implemented vor Arguments |
| 229 | + // Key Value Pairs |
| 230 | + .contains("\"key_01_string\":\"value_01\"") |
| 231 | + .contains("\"key_02_numeric\":2") |
| 232 | + .contains("\"key_03_decimal\":2.333") |
| 233 | + .contains("\"key_04_null\":null") |
| 234 | + .contains("\"\":\"value_05_empty_key\"") |
| 235 | + .contains("\"null\":\"value_06_null_key\"") |
| 236 | + .contains("\"key_07_boolean_true\":true") |
| 237 | + .contains("\"key_08_boolean_false\":false") |
| 238 | + .contains("\"mdc_02_overwrite\":\"overwritten_by_kvp\"") |
| 239 | + .contains("\"structured_argument_02_overwrite\":\"overwritten_by_kvp\"") |
| 240 | + ; |
| 241 | + } |
| 242 | + |
182 | 243 | } |
0 commit comments