11
11
* the Apache License, Version 2.0 (the "License"); you may
12
12
* not use this file except in compliance with the License.
13
13
* You may obtain a copy of the License at
14
- *
14
+ *
15
15
* http://www.apache.org/licenses/LICENSE-2.0
16
- *
16
+ *
17
17
* Unless required by applicable law or agreed to in writing,
18
18
* software distributed under the License is distributed on an
19
19
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
24
24
*/
25
25
package co .elastic .logging .jul ;
26
26
27
+ import com .fasterxml .jackson .core .JsonProcessingException ;
27
28
import com .fasterxml .jackson .databind .JsonNode ;
28
29
import com .fasterxml .jackson .databind .ObjectMapper ;
29
30
import org .junit .jupiter .api .BeforeEach ;
30
31
import org .junit .jupiter .api .Test ;
31
32
32
33
import java .time .Instant ;
34
+ import java .util .HashMap ;
35
+ import java .util .Map ;
33
36
import java .util .logging .Level ;
34
37
import java .util .logging .LogRecord ;
35
38
@@ -40,7 +43,7 @@ public class EcsFormatterTest {
40
43
private final EcsFormatter formatter = new EcsFormatter ();
41
44
42
45
private final LogRecord record = new LogRecord (Level .INFO , "Example Message" );
43
- private final ObjectMapper objectMapper = new ObjectMapper ();
46
+ private static final ObjectMapper objectMapper = new ObjectMapper ();
44
47
45
48
@ BeforeEach
46
49
void setUp () {
@@ -57,21 +60,21 @@ public void testFormatWithIncludeOriginFlag() throws Exception {
57
60
58
61
final String result = formatter .format (record );
59
62
60
- assertThat (objectMapper . readTree (result ).at ("/log/origin/file/name" ).textValue ()).isEqualTo ("ExampleClass.java" );
61
- assertThat (objectMapper . readTree (result ).at ("/log/origin/function" ).textValue ()).isEqualTo ("exampleMethod" );
63
+ assertThat (parseJson (result ).at ("/log/origin/file/name" ).textValue ()).isEqualTo ("ExampleClass.java" );
64
+ assertThat (parseJson (result ).at ("/log/origin/function" ).textValue ()).isEqualTo ("exampleMethod" );
62
65
}
63
66
64
67
@ Test
65
68
public void testFormatWithoutIncludeOriginFlag () throws Exception {
66
- final JsonNode result = objectMapper . readTree (formatter .format (record ));
69
+ final JsonNode result = parseJson (formatter .format (record ));
67
70
assertThat (result .get ("log.origin" )).isNull ();
68
71
}
69
72
70
73
@ Test
71
74
public void testFormatWithoutLoggerName () throws Exception {
72
75
record .setLoggerName (null );
73
76
74
- final JsonNode result = objectMapper . readTree (formatter .format (record ));
77
+ final JsonNode result = parseJson (formatter .format (record ));
75
78
76
79
assertThat (result .get ("log.logger" )).isNull ();
77
80
}
@@ -80,7 +83,7 @@ public void testFormatWithoutLoggerName() throws Exception {
80
83
public void testFormatWithEmptyLoggerName () throws Exception {
81
84
record .setLoggerName ("" );
82
85
83
- final JsonNode result = objectMapper . readTree (formatter .format (record ));
86
+ final JsonNode result = parseJson (formatter .format (record ));
84
87
85
88
assertThat (result .get ("log.logger" ).textValue ()).isEmpty ();
86
89
}
@@ -90,7 +93,7 @@ public void testFormatWithInnerClassName() throws Exception {
90
93
formatter .setIncludeOrigin (true );
91
94
record .setSourceClassName ("test.ExampleClass$InnerClass" );
92
95
93
- JsonNode result = objectMapper . readTree (formatter .format (record ));
96
+ JsonNode result = parseJson (formatter .format (record ));
94
97
assertThat (result .at ("/log/origin/file/name" ).textValue ()).isEqualTo ("ExampleClass.java" );
95
98
assertThat (result .at ("/log/origin/function" ).textValue ()).isEqualTo ("exampleMethod" );
96
99
}
@@ -100,9 +103,39 @@ public void testFormatWithInvalidClassName() throws Exception {
100
103
formatter .setIncludeOrigin (true );
101
104
record .setSourceClassName ("$test.ExampleClass" );
102
105
103
- JsonNode result = objectMapper . readTree (formatter .format (record ));
106
+ JsonNode result = parseJson (formatter .format (record ));
104
107
assertThat (result .at ("/log/origin/file/name" ).textValue ()).isEqualTo ("<Unknown>" );
105
108
assertThat (result .at ("/log/origin/function" ).textValue ()).isEqualTo ("exampleMethod" );
106
109
}
107
110
111
+ @ Test
112
+ void testMdcSerialization_singleEntry () {
113
+ Map <String ,String > mdc = new HashMap <>();
114
+ TestMdcEcsFormatter mdcFormatter = new TestMdcEcsFormatter (mdc );
115
+ mdc .put ("mdc.key" , "value" );
116
+ JsonNode result = parseJson (mdcFormatter .format (record ));
117
+ assertThat (result .get ("mdc.key" ).textValue ()).isEqualTo ("value" );
118
+ }
119
+
120
+ private static JsonNode parseJson (String formatter ) {
121
+ try {
122
+ return objectMapper .readTree (formatter );
123
+ } catch (JsonProcessingException e ) {
124
+ throw new RuntimeException (e );
125
+ }
126
+ }
127
+
128
+ private static class TestMdcEcsFormatter extends EcsFormatter {
129
+ private final Map <String , String > mdc ;
130
+
131
+ public TestMdcEcsFormatter (Map <String , String > mdc ) {
132
+ this .mdc = mdc ;
133
+ }
134
+
135
+ @ Override
136
+ protected Map <String , String > getMdcEntries () {
137
+ return mdc ;
138
+ }
139
+ }
140
+
108
141
}
0 commit comments