blob: 6d40b15d4093ec4f337430d035bcc59d6f0876b0 [file] [log] [blame]
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +02001package eu.mulk.quarkus.googlecloud.jsonlogging;
2
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +02003import static org.junit.jupiter.api.Assertions.assertLinesMatch;
4
Matthias Andreas Benkardd2280ad2024-06-23 17:08:58 +02005import jakarta.json.spi.JsonProvider;
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +02006import java.util.Collection;
7import java.util.List;
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +02008import org.jboss.logmanager.ExtLogRecord;
9import org.jboss.logmanager.Level;
10import org.junit.jupiter.api.Test;
11
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +020012class FormatterTest {
13
Matthias Andreas Benkardd2280ad2024-06-23 17:08:58 +020014 private static final JsonProvider JSON = JsonProvider.provider();
15
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +020016 @Test
17 void simpleRecord() {
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +020018 var logRecord = makeSimpleRecord();
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +020019
20 var formatter = new Formatter(List.of(), List.of());
21 var formattingResult = formatter.format(logRecord);
22
23 assertLinesMatch(
24 List.of(
25 "\\{"
26 + "\"message\":\"Hello, world!\","
27 + "\"severity\":\"INFO\","
28 + "\"timestamp\":\\{\"seconds\":\\d+,\"nanos\":\\d+\\},"
29 + "\"logging.googleapis.com/sourceLocation\":"
30 + "\\{\"file\":\"ReflectionUtils.java\","
31 + "\"line\":\"\\d+\","
32 + "\"function\":\"org.junit.platform.commons.util.ReflectionUtils.invokeMethod\""
33 + "\\}"
34 + "\\}\n"),
35 List.of(formattingResult));
36 }
37
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +020038 static ExtLogRecord makeSimpleRecord() {
39 return new ExtLogRecord(Level.INFO, "Hello, world!", FormatterTest.class.getName());
40 }
41
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +020042 @Test
43 void structuredRecord() {
44 var parameterProvider =
45 new StructuredParameterProvider() {
46 @Override
47 public StructuredParameter getParameter() {
Matthias Andreas Benkardd2280ad2024-06-23 17:08:58 +020048 var b = JSON.createObjectBuilder();
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +020049 b.add("traceId", "39f9a49a9567a8bd7087b708f8932550");
50 b.add("spanId", "c7431b14630b633d");
51 return () -> b;
52 }
53 };
54
55 var labelProvider =
56 new LabelProvider() {
57 @Override
58 public Collection<Label> getLabels() {
59 return List.of(Label.of("requestId", "123"));
60 }
61 };
62
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +020063 var logRecord = makeStructuredRecord();
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +020064
65 var formatter = new Formatter(List.of(parameterProvider), List.of(labelProvider));
66 var formattingResult = formatter.format(logRecord);
67 assertLinesMatch(
68 List.of(
69 "\\{"
70 + "\"logging.googleapis.com/labels\":\\{\"a\":\"b\",\"requestId\":\"123\"\\},"
71 + "\"message\":\"Hello, world!\","
72 + "\"severity\":\"INFO\","
73 + "\"timestamp\":\\{\"seconds\":\\d+,\"nanos\":\\d+\\},"
74 + "\"logging.googleapis.com/sourceLocation\":"
75 + "\\{\"file\":\"ReflectionUtils.java\","
76 + "\"line\":\"\\d+\","
77 + "\"function\":\"org.junit.platform.commons.util.ReflectionUtils.invokeMethod\""
78 + "\\},"
79 + "\"traceId\":\"39f9a49a9567a8bd7087b708f8932550\","
80 + "\"spanId\":\"c7431b14630b633d\","
81 + "\"one\":1,"
82 + "\"two\":2.0,"
83 + "\"yes\":true"
84 + "\\}\n"),
85 List.of(formattingResult));
86 }
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +020087
88 static ExtLogRecord makeStructuredRecord() {
89 var logRecord = makeSimpleRecord();
90 logRecord.setParameters(
91 new Object[] {
92 (StructuredParameter)
Matthias Andreas Benkardd2280ad2024-06-23 17:08:58 +020093 () -> JSON.createObjectBuilder().add("one", 1).add("two", 2.0).add("yes", true),
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +020094 Label.of("a", "b")
95 });
96 return logRecord;
97 }
Matthias Andreas Benkardf90824b2024-06-23 16:42:11 +020098
99 @Test
100 void massivelyStructuredRecord() {
101 var logRecord = makeMassivelyStructuredRecord();
102
103 var formatter = new Formatter(List.of(), List.of());
104 var formattingResult = formatter.format(logRecord);
105 assertLinesMatch(
106 List.of(
107 "\\{"
108 + "\"message\":\"Hello, world!\","
109 + "\"severity\":\"INFO\","
110 + "\"timestamp\":\\{\"seconds\":\\d+,\"nanos\":\\d+\\},"
111 + "\"logging.googleapis.com/sourceLocation\":"
112 + "\\{\"file\":\"ReflectionUtils.java\","
113 + "\"line\":\"\\d+\","
114 + "\"function\":\"org.junit.platform.commons.util.ReflectionUtils.invokeMethod\""
115 + "\\},"
116 + "\"int-0\":0,\"int-1\":1,\"int-2\":2,\"int-3\":3,\"int-4\":4,\"int-5\":5,\"int-6\":6,\"int-7\":7,\"int-8\":8,\"int-9\":9,"
117 + "\"double-10\":10.0,\"double-11\":11.0,\"double-12\":12.0,\"double-13\":13.0,\"double-14\":14.0,"
118 + "\"double-15\":15.0,\"double-16\":16.0,\"double-17\":17.0,\"double-18\":18.0,\"double-19\":19.0,"
119 + "\"boolean-20\":true,\"boolean-21\":false,\"boolean-22\":true,\"boolean-23\":false,\"boolean-24\":true,"
120 + "\"boolean-25\":false,\"boolean-26\":true,\"boolean-27\":false,\"boolean-28\":true,\"boolean-29\":false,"
121 + "\"string-30\":\"30\",\"string-31\":\"31\",\"string-32\":\"32\",\"string-33\":\"33\",\"string-34\":\"34\","
122 + "\"string-35\":\"35\",\"string-36\":\"36\",\"string-37\":\"37\",\"string-38\":\"38\",\"string-39\":\"39\""
123 + "\\}\n"),
124 List.of(formattingResult));
125 }
126
127 static ExtLogRecord makeMassivelyStructuredRecord() {
128 var logRecord = FormatterTest.makeSimpleRecord();
129 logRecord.setParameters(
130 new Object[] {
131 (StructuredParameter)
132 () -> {
Matthias Andreas Benkardd2280ad2024-06-23 17:08:58 +0200133 var b = JSON.createObjectBuilder();
Matthias Andreas Benkardf90824b2024-06-23 16:42:11 +0200134 for (int i = 0; i < 10; i++) {
135 b.add("int-" + i, i);
136 }
137 for (int i = 10; i < 20; i++) {
138 b.add("double-" + i, (double) i);
139 }
140 for (int i = 20; i < 30; i++) {
141 b.add("boolean-" + i, i % 2 == 0);
142 }
143 for (int i = 30; i < 40; i++) {
144 b.add("string-" + i, String.valueOf(i));
145 }
146 return b;
147 }
148 });
149 return logRecord;
150 }
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +0200151}