30
30
import java .util .logging .Level ;
31
31
import java .util .logging .LogRecord ;
32
32
33
+ import com .fasterxml .jackson .databind .JsonNode ;
34
+ import com .fasterxml .jackson .databind .ObjectMapper ;
33
35
import org .junit .jupiter .api .BeforeEach ;
34
36
import org .junit .jupiter .api .Test ;
35
37
@@ -38,76 +40,69 @@ public class EcsFormatterTest {
38
40
private final EcsFormatter formatter = new EcsFormatter ();
39
41
40
42
private final LogRecord record = new LogRecord (Level .INFO , "Example Meesage" );
43
+ private final ObjectMapper objectMapper = new ObjectMapper ();
41
44
42
- @ Test
43
- public void testFormatWithIncludeOriginFlag () {
45
+ @ BeforeEach
46
+ void setUp () {
47
+ record .setInstant (Instant .ofEpochMilli (5 ));
48
+ record .setSourceClassName ("ExampleClass" );
49
+ record .setSourceMethodName ("exampleMethod" );
50
+ record .setThreadID (7 );
51
+ record .setLoggerName ("ExampleLogger" );
52
+ }
44
53
54
+ @ Test
55
+ public void testFormatWithIncludeOriginFlag () throws Exception {
45
56
formatter .setIncludeOrigin (true );
46
57
47
58
final String result = formatter .format (record );
48
59
49
- assertThat (result ).isEqualTo (
50
- "{ \" @timestamp \" : \" 1970-01-01T00:00:00.005Z \" , \" log.level \" : \" INFO \" , \" message \" : \" Example Meesage \" , \" process.thread.id \" :7, \" log.logger \" : \" ExampleLogger \" , \" log. origin\" :{ \" file.name \" : \" ExampleClass.java \" , \" function \" : \" exampleMethod\" }} \n " );
60
+ assertThat (objectMapper . readTree ( result ).get ( "log.origin" ). get ( "file.name" ). textValue ()). isEqualTo ("ExampleClass.java" );
61
+ assertThat ( objectMapper . readTree ( result ). get ( " log.origin" ). get ( "function" ). textValue ()). isEqualTo ( " exampleMethod" );
51
62
}
52
63
53
64
@ Test
54
- public void testFormatWithoutIncludeOriginFlag () {
55
-
56
- final String result = formatter .format (record );
57
-
58
- assertThat (result ).isEqualTo (
59
- "{\" @timestamp\" :\" 1970-01-01T00:00:00.005Z\" , \" log.level\" : \" INFO\" , \" message\" :\" Example Meesage\" , \" process.thread.id\" :7,\" log.logger\" :\" ExampleLogger\" }\n " );
65
+ public void testFormatWithoutIncludeOriginFlag () throws Exception {
66
+ final JsonNode result = objectMapper .readTree (formatter .format (record ));
67
+ assertThat (result .get ("log.origin" )).isNull ();
60
68
}
61
69
62
70
@ Test
63
- public void testFormatWithoutLoggerName () {
71
+ public void testFormatWithoutLoggerName () throws Exception {
64
72
record .setLoggerName (null );
65
73
66
- final String result = formatter .format (record );
74
+ final JsonNode result = objectMapper . readTree ( formatter .format (record ) );
67
75
68
- assertThat (result ).isEqualTo (
69
- "{\" @timestamp\" :\" 1970-01-01T00:00:00.005Z\" , \" log.level\" : \" INFO\" , \" message\" :\" Example Meesage\" , \" process.thread.id\" :7}\n " );
76
+ assertThat (result .get ("log.logger" )).isNull ();
70
77
}
71
78
72
79
@ Test
73
- public void testFormatWithEmptyLoggerName () {
80
+ public void testFormatWithEmptyLoggerName () throws Exception {
74
81
record .setLoggerName ("" );
75
82
76
- final String result = formatter .format (record );
83
+ final JsonNode result = objectMapper . readTree ( formatter .format (record ) );
77
84
78
- assertThat (result ).isEqualTo (
79
- "{\" @timestamp\" :\" 1970-01-01T00:00:00.005Z\" , \" log.level\" : \" INFO\" , \" message\" :\" Example Meesage\" , \" process.thread.id\" :7,\" log.logger\" :\" \" }\n " );
85
+ assertThat (result .get ("log.logger" ).textValue ()).isEmpty ();
80
86
}
81
87
82
88
@ Test
83
- public void testFormatWithInnerClassName () {
89
+ public void testFormatWithInnerClassName () throws Exception {
84
90
formatter .setIncludeOrigin (true );
85
91
record .setSourceClassName ("test.ExampleClass$InnerClass" );
86
92
87
- final String result = formatter .format (record );
88
-
89
- assertThat (result ).isEqualTo (
90
- "{\" @timestamp\" :\" 1970-01-01T00:00:00.005Z\" , \" log.level\" : \" INFO\" , \" message\" :\" Example Meesage\" , \" process.thread.id\" :7,\" log.logger\" :\" ExampleLogger\" ,\" log.origin\" :{\" file.name\" :\" ExampleClass.java\" ,\" function\" :\" exampleMethod\" }}\n " );
93
+ JsonNode result = objectMapper .readTree (formatter .format (record ));
94
+ assertThat (result .get ("log.origin" ).get ("file.name" ).textValue ()).isEqualTo ("ExampleClass.java" );
95
+ assertThat (result .get ("log.origin" ).get ("function" ).textValue ()).isEqualTo ("exampleMethod" );
91
96
}
92
97
93
98
@ Test
94
- public void testFormatWithInvalidClassName () {
99
+ public void testFormatWithInvalidClassName () throws Exception {
95
100
formatter .setIncludeOrigin (true );
96
101
record .setSourceClassName ("$test.ExampleClass" );
97
102
98
- final String result = formatter .format (record );
99
-
100
- assertThat (result ).isEqualTo (
101
- "{\" @timestamp\" :\" 1970-01-01T00:00:00.005Z\" , \" log.level\" : \" INFO\" , \" message\" :\" Example Meesage\" , \" process.thread.id\" :7,\" log.logger\" :\" ExampleLogger\" ,\" log.origin\" :{\" file.name\" :\" <Unknown>\" ,\" function\" :\" exampleMethod\" }}\n " );
102
- }
103
-
104
- @ BeforeEach
105
- void setUp () {
106
- record .setInstant (Instant .ofEpochMilli (5 ));
107
- record .setSourceClassName ("ExampleClass" );
108
- record .setSourceMethodName ("exampleMethod" );
109
- record .setThreadID (7 );
110
- record .setLoggerName ("ExampleLogger" );
103
+ JsonNode result = objectMapper .readTree (formatter .format (record ));
104
+ assertThat (result .get ("log.origin" ).get ("file.name" ).textValue ()).isEqualTo ("<Unknown>" );
105
+ assertThat (result .get ("log.origin" ).get ("function" ).textValue ()).isEqualTo ("exampleMethod" );
111
106
}
112
107
113
108
}
0 commit comments