Matthias Andreas Benkard | b69b301 | 2024-06-23 15:48:49 +0200 | [diff] [blame^] | 1 | package eu.mulk.quarkus.googlecloud.jsonlogging; |
| 2 | |
| 3 | import jakarta.json.Json; |
| 4 | import org.jboss.logmanager.ExtLogRecord; |
| 5 | import org.jboss.logmanager.Level; |
| 6 | import org.junit.jupiter.api.Test; |
| 7 | |
| 8 | import java.util.Collection; |
| 9 | import java.util.List; |
| 10 | |
| 11 | import static org.junit.jupiter.api.Assertions.assertLinesMatch; |
| 12 | |
| 13 | class FormatterTest { |
| 14 | |
| 15 | @Test |
| 16 | void simpleRecord() { |
| 17 | var logRecord = new ExtLogRecord(Level.INFO, "Hello, world!", FormatterTest.class.getName()); |
| 18 | |
| 19 | var formatter = new Formatter(List.of(), List.of()); |
| 20 | var formattingResult = formatter.format(logRecord); |
| 21 | |
| 22 | assertLinesMatch( |
| 23 | List.of( |
| 24 | "\\{" |
| 25 | + "\"message\":\"Hello, world!\"," |
| 26 | + "\"severity\":\"INFO\"," |
| 27 | + "\"timestamp\":\\{\"seconds\":\\d+,\"nanos\":\\d+\\}," |
| 28 | + "\"logging.googleapis.com/sourceLocation\":" |
| 29 | + "\\{\"file\":\"ReflectionUtils.java\"," |
| 30 | + "\"line\":\"\\d+\"," |
| 31 | + "\"function\":\"org.junit.platform.commons.util.ReflectionUtils.invokeMethod\"" |
| 32 | + "\\}" |
| 33 | + "\\}\n"), |
| 34 | List.of(formattingResult)); |
| 35 | } |
| 36 | |
| 37 | @Test |
| 38 | void structuredRecord() { |
| 39 | var parameterProvider = |
| 40 | new StructuredParameterProvider() { |
| 41 | @Override |
| 42 | public StructuredParameter getParameter() { |
| 43 | var b = Json.createObjectBuilder(); |
| 44 | b.add("traceId", "39f9a49a9567a8bd7087b708f8932550"); |
| 45 | b.add("spanId", "c7431b14630b633d"); |
| 46 | return () -> b; |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | var labelProvider = |
| 51 | new LabelProvider() { |
| 52 | @Override |
| 53 | public Collection<Label> getLabels() { |
| 54 | return List.of(Label.of("requestId", "123")); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | var logRecord = new ExtLogRecord(Level.INFO, "Hello, world!", FormatterTest.class.getName()); |
| 59 | logRecord.setParameters( |
| 60 | new Object[] { |
| 61 | (StructuredParameter) |
| 62 | () -> Json.createObjectBuilder().add("one", 1).add("two", 2.0).add("yes", true), |
| 63 | Label.of("a", "b") |
| 64 | }); |
| 65 | |
| 66 | var formatter = new Formatter(List.of(parameterProvider), List.of(labelProvider)); |
| 67 | var formattingResult = formatter.format(logRecord); |
| 68 | assertLinesMatch( |
| 69 | List.of( |
| 70 | "\\{" |
| 71 | + "\"logging.googleapis.com/labels\":\\{\"a\":\"b\",\"requestId\":\"123\"\\}," |
| 72 | + "\"message\":\"Hello, world!\"," |
| 73 | + "\"severity\":\"INFO\"," |
| 74 | + "\"timestamp\":\\{\"seconds\":\\d+,\"nanos\":\\d+\\}," |
| 75 | + "\"logging.googleapis.com/sourceLocation\":" |
| 76 | + "\\{\"file\":\"ReflectionUtils.java\"," |
| 77 | + "\"line\":\"\\d+\"," |
| 78 | + "\"function\":\"org.junit.platform.commons.util.ReflectionUtils.invokeMethod\"" |
| 79 | + "\\}," |
| 80 | + "\"traceId\":\"39f9a49a9567a8bd7087b708f8932550\"," |
| 81 | + "\"spanId\":\"c7431b14630b633d\"," |
| 82 | + "\"one\":1," |
| 83 | + "\"two\":2.0," |
| 84 | + "\"yes\":true" |
| 85 | + "\\}\n"), |
| 86 | List.of(formattingResult)); |
| 87 | } |
| 88 | } |