1616 */
1717package org .apache .logging .log4j .core .impl ;
1818
19- import static org .junit . Assert . assertEquals ;
19+ import static org .assertj . core . api . Assertions . assertThat ;
2020
21- import java .util .List ;
22- import org .apache .logging .log4j .LogManager ;
2321import org .apache .logging .log4j .Logger ;
22+ import org .apache .logging .log4j .core .LoggerContext ;
2423import 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 ;
2929
3030/**
3131 * There are two logger.info() calls here.
4242 * @author lwest
4343 * @since 2016-09-14 in recursion
4444 */
45+ @ LoggerContextSource (value = "impl/NestedLoggingFromToStringTest.xml" , reconfigure = ReconfigurationPolicy .BEFORE_EACH )
46+ @ UsingStatusListener // Record status logger messages and dump in case of a failure
4547public class NestedLoggingFromToStringTest {
4648
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-
5949 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+ }
6256
6357 public int getX () {
6458 innerLogger .debug ("getX: values x={} y={} z={}" , x , y , z );
@@ -72,10 +66,16 @@ public String toString() {
7266 }
7367
7468 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+ }
7676
7777 public int getX () {
78- innerLogger .trace (new ObjectLoggingThing2 ());
78+ innerLogger .trace (new ObjectLoggingThing2 (loggerContext ));
7979 return 999 ;
8080 }
8181
@@ -86,10 +86,16 @@ public String toString() {
8686 }
8787
8888 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+ }
9096
9197 public int getX () {
92- innerLogger .trace (new ParameterizedLoggingThing ());
98+ innerLogger .trace (new ParameterizedLoggingThing (loggerContext ));
9399 return 123 ;
94100 }
95101
@@ -100,49 +106,37 @@ public String toString() {
100106 }
101107
102108 @ 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]" );
114117 }
115118
116119 @ 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" );
128129 }
129130
130131 @ 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]" );
147141 }
148142}
0 commit comments