16
16
*/
17
17
package org .apache .logging .log4j .core .impl ;
18
18
19
- import static org .junit . Assert . assertEquals ;
19
+ import static org .assertj . core . api . Assertions . assertThat ;
20
20
21
- import java .util .List ;
22
- import org .apache .logging .log4j .LogManager ;
23
21
import org .apache .logging .log4j .Logger ;
22
+ import org .apache .logging .log4j .core .LoggerContext ;
24
23
import org .apache .logging .log4j .core .test .appender .ListAppender ;
25
- import org .apache .logging .log4j .core .test .junit .LoggerContextRule ;
26
- import org .junit .Before ;
27
- import org .junit .Rule ;
28
- import org .junit .Test ;
24
+ import org .apache .logging .log4j .core .test .junit .LoggerContextSource ;
25
+ import org .apache .logging .log4j .core .test .junit .ReconfigurationPolicy ;
26
+ import org .apache .logging .log4j .plugins .Named ;
27
+ import org .apache .logging .log4j .test .junit .UsingStatusListener ;
28
+ import org .junit .jupiter .api .Test ;
29
29
30
30
/**
31
31
* There are two logger.info() calls here.
42
42
* @author lwest
43
43
* @since 2016-09-14 in recursion
44
44
*/
45
+ @ LoggerContextSource (value = "impl/NestedLoggingFromToStringTest.xml" , reconfigure = ReconfigurationPolicy .BEFORE_EACH )
46
+ @ UsingStatusListener // Record status logger messages and dump in case of a failure
45
47
public class NestedLoggingFromToStringTest {
46
48
47
- @ Rule
48
- public LoggerContextRule context = new LoggerContextRule ("log4j-sync-to-list.xml" );
49
-
50
- private ListAppender listAppender ;
51
- private Logger logger ;
52
-
53
- @ Before
54
- public void before () {
55
- listAppender = context .getListAppender ("List" );
56
- logger = LogManager .getLogger (NestedLoggingFromToStringTest .class );
57
- }
58
-
59
49
static class ParameterizedLoggingThing {
60
- final Logger innerLogger = LogManager .getLogger (ParameterizedLoggingThing .class );
61
- private final int x = 3 , y = 4 , z = 5 ;
50
+ final Logger innerLogger ;
51
+ private static final int x = 3 , y = 4 , z = 5 ;
52
+
53
+ ParameterizedLoggingThing (LoggerContext loggerContext ) {
54
+ innerLogger = loggerContext .getLogger (ParameterizedLoggingThing .class );
55
+ }
62
56
63
57
public int getX () {
64
58
innerLogger .debug ("getX: values x={} y={} z={}" , x , y , z );
@@ -72,10 +66,16 @@ public String toString() {
72
66
}
73
67
74
68
static class ObjectLoggingThing1 {
75
- final Logger innerLogger = LogManager .getLogger (ObjectLoggingThing1 .class );
69
+ final LoggerContext loggerContext ;
70
+ final Logger innerLogger ;
71
+
72
+ ObjectLoggingThing1 (LoggerContext loggerContext ) {
73
+ this .loggerContext = loggerContext ;
74
+ this .innerLogger = loggerContext .getLogger (ObjectLoggingThing1 .class );
75
+ }
76
76
77
77
public int getX () {
78
- innerLogger .trace (new ObjectLoggingThing2 ());
78
+ innerLogger .trace (new ObjectLoggingThing2 (loggerContext ));
79
79
return 999 ;
80
80
}
81
81
@@ -86,10 +86,16 @@ public String toString() {
86
86
}
87
87
88
88
static class ObjectLoggingThing2 {
89
- final Logger innerLogger = LogManager .getLogger (ObjectLoggingThing2 .class );
89
+ final LoggerContext loggerContext ;
90
+ final Logger innerLogger ;
91
+
92
+ ObjectLoggingThing2 (LoggerContext loggerContext ) {
93
+ this .loggerContext = loggerContext ;
94
+ this .innerLogger = loggerContext .getLogger (ObjectLoggingThing2 .class );
95
+ }
90
96
91
97
public int getX () {
92
- innerLogger .trace (new ParameterizedLoggingThing ());
98
+ innerLogger .trace (new ParameterizedLoggingThing (loggerContext ));
93
99
return 123 ;
94
100
}
95
101
@@ -100,49 +106,37 @@ public String toString() {
100
106
}
101
107
102
108
@ Test
103
- public void testNestedLoggingInLastArgument () {
104
- final ParameterizedLoggingThing it = new ParameterizedLoggingThing ();
105
- logger .info ("main: argCount={} it={}" , "2" , it );
106
- final List <String > list = listAppender .getMessages ();
107
-
108
- final String expect1 =
109
- "DEBUG org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest.ParameterizedLoggingThing getX: values x=3 y=4 z=5" ;
110
- final String expect2 =
111
- "INFO org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest main: argCount=2 it=[ParameterizedLoggingThing x=3 y=4 z=5]" ;
112
- assertEquals (expect1 , list .get (0 ));
113
- assertEquals (expect2 , list .get (1 ));
109
+ public void testNestedLoggingInLastArgument (LoggerContext loggerContext , @ Named ("LIST" ) ListAppender listAppender ) {
110
+ final ParameterizedLoggingThing it = new ParameterizedLoggingThing (loggerContext );
111
+ loggerContext .getLogger (NestedLoggingFromToStringTest .class ).info ("main: argCount={} it={}" , "2" , it );
112
+
113
+ assertThat (listAppender .getMessages ())
114
+ .containsExactly (
115
+ "DEBUG org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest.ParameterizedLoggingThing getX: values x=3 y=4 z=5" ,
116
+ "INFO org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest main: argCount=2 it=[ParameterizedLoggingThing x=3 y=4 z=5]" );
114
117
}
115
118
116
119
@ Test
117
- public void testNestedLoggingInFirstArgument () {
118
- final ParameterizedLoggingThing it = new ParameterizedLoggingThing ();
119
- logger .info ("next: it={} some{} other{}" , it , "AA" , "BB" );
120
- final List <String > list = listAppender .getMessages ();
121
-
122
- final String expect1 =
123
- "DEBUG org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest.ParameterizedLoggingThing getX: values x=3 y=4 z=5" ;
124
- final String expect2 =
125
- "INFO org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest next: it=[ParameterizedLoggingThing x=3 y=4 z=5] someAA otherBB" ;
126
- assertEquals (expect1 , list .get (0 ));
127
- assertEquals (expect2 , list .get (1 ));
120
+ public void testNestedLoggingInFirstArgument (
121
+ LoggerContext loggerContext , @ Named ("LIST" ) ListAppender listAppender ) {
122
+ final ParameterizedLoggingThing it = new ParameterizedLoggingThing (loggerContext );
123
+ loggerContext .getLogger (NestedLoggingFromToStringTest .class ).info ("next: it={} some{} other{}" , it , "AA" , "BB" );
124
+
125
+ assertThat (listAppender .getMessages ())
126
+ .containsExactly (
127
+ "DEBUG org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest.ParameterizedLoggingThing getX: values x=3 y=4 z=5" ,
128
+ "INFO org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest next: it=[ParameterizedLoggingThing x=3 y=4 z=5] someAA otherBB" );
128
129
}
129
130
130
131
@ Test
131
- public void testDoublyNestedLogging () {
132
- logger .info (new ObjectLoggingThing1 ());
133
- final List <String > list = listAppender .getMessages ();
134
-
135
- final String expect1 =
136
- "DEBUG org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest.ParameterizedLoggingThing getX: values x=3 y=4 z=5" ;
137
- final String expect2 =
138
- "TRACE org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest.ObjectLoggingThing2 [ParameterizedLoggingThing x=3 y=4 z=5]" ;
139
- final String expect3 =
140
- "TRACE org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest.ObjectLoggingThing1 [ObjectLoggingThing2 x=123]" ;
141
- final String expect4 =
142
- "INFO org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest [ObjectLoggingThing1 y=999]" ;
143
- assertEquals (expect1 , list .get (0 ));
144
- assertEquals (expect2 , list .get (1 ));
145
- assertEquals (expect3 , list .get (2 ));
146
- assertEquals (expect4 , list .get (3 ));
132
+ public void testDoublyNestedLogging (LoggerContext loggerContext , @ Named ("LIST" ) ListAppender listAppender ) {
133
+ loggerContext .getLogger (NestedLoggingFromToStringTest .class ).info (new ObjectLoggingThing1 (loggerContext ));
134
+
135
+ assertThat (listAppender .getMessages ())
136
+ .containsExactly (
137
+ "DEBUG org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest.ParameterizedLoggingThing getX: values x=3 y=4 z=5" ,
138
+ "TRACE org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest.ObjectLoggingThing2 [ParameterizedLoggingThing x=3 y=4 z=5]" ,
139
+ "TRACE org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest.ObjectLoggingThing1 [ObjectLoggingThing2 x=123]" ,
140
+ "INFO org.apache.logging.log4j.core.impl.NestedLoggingFromToStringTest [ObjectLoggingThing1 y=999]" );
147
141
}
148
142
}
0 commit comments