1111 * the Apache License, Version 2.0 (the "License"); you may
1212 * not use this file except in compliance with the License.
1313 * You may obtain a copy of the License at
14- *
14+ *
1515 * http://www.apache.org/licenses/LICENSE-2.0
16- *
16+ *
1717 * Unless required by applicable law or agreed to in writing,
1818 * software distributed under the License is distributed on an
1919 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
2424 */
2525package co .elastic .logging .jul ;
2626
27+ import com .fasterxml .jackson .core .JsonProcessingException ;
2728import com .fasterxml .jackson .databind .JsonNode ;
2829import com .fasterxml .jackson .databind .ObjectMapper ;
2930import org .junit .jupiter .api .BeforeEach ;
3031import org .junit .jupiter .api .Test ;
3132
3233import java .time .Instant ;
34+ import java .util .HashMap ;
35+ import java .util .Map ;
3336import java .util .logging .Level ;
3437import java .util .logging .LogRecord ;
3538
@@ -40,7 +43,7 @@ public class EcsFormatterTest {
4043 private final EcsFormatter formatter = new EcsFormatter ();
4144
4245 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 ();
4447
4548 @ BeforeEach
4649 void setUp () {
@@ -57,21 +60,21 @@ public void testFormatWithIncludeOriginFlag() throws Exception {
5760
5861 final String result = formatter .format (record );
5962
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" );
6265 }
6366
6467 @ Test
6568 public void testFormatWithoutIncludeOriginFlag () throws Exception {
66- final JsonNode result = objectMapper . readTree (formatter .format (record ));
69+ final JsonNode result = parseJson (formatter .format (record ));
6770 assertThat (result .get ("log.origin" )).isNull ();
6871 }
6972
7073 @ Test
7174 public void testFormatWithoutLoggerName () throws Exception {
7275 record .setLoggerName (null );
7376
74- final JsonNode result = objectMapper . readTree (formatter .format (record ));
77+ final JsonNode result = parseJson (formatter .format (record ));
7578
7679 assertThat (result .get ("log.logger" )).isNull ();
7780 }
@@ -80,7 +83,7 @@ public void testFormatWithoutLoggerName() throws Exception {
8083 public void testFormatWithEmptyLoggerName () throws Exception {
8184 record .setLoggerName ("" );
8285
83- final JsonNode result = objectMapper . readTree (formatter .format (record ));
86+ final JsonNode result = parseJson (formatter .format (record ));
8487
8588 assertThat (result .get ("log.logger" ).textValue ()).isEmpty ();
8689 }
@@ -90,7 +93,7 @@ public void testFormatWithInnerClassName() throws Exception {
9093 formatter .setIncludeOrigin (true );
9194 record .setSourceClassName ("test.ExampleClass$InnerClass" );
9295
93- JsonNode result = objectMapper . readTree (formatter .format (record ));
96+ JsonNode result = parseJson (formatter .format (record ));
9497 assertThat (result .at ("/log/origin/file/name" ).textValue ()).isEqualTo ("ExampleClass.java" );
9598 assertThat (result .at ("/log/origin/function" ).textValue ()).isEqualTo ("exampleMethod" );
9699 }
@@ -100,9 +103,39 @@ public void testFormatWithInvalidClassName() throws Exception {
100103 formatter .setIncludeOrigin (true );
101104 record .setSourceClassName ("$test.ExampleClass" );
102105
103- JsonNode result = objectMapper . readTree (formatter .format (record ));
106+ JsonNode result = parseJson (formatter .format (record ));
104107 assertThat (result .at ("/log/origin/file/name" ).textValue ()).isEqualTo ("<Unknown>" );
105108 assertThat (result .at ("/log/origin/function" ).textValue ()).isEqualTo ("exampleMethod" );
106109 }
107110
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+
108141}
0 commit comments