Skip to content

Commit 259e303

Browse files
committed
Fix NestedLoggingFromToStringTest
1 parent c791f0c commit 259e303

File tree

3 files changed

+91
-64
lines changed

3 files changed

+91
-64
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/NestedLoggingFromToStringTest.java

Lines changed: 57 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
*/
1717
package 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;
2321
import org.apache.logging.log4j.Logger;
22+
import org.apache.logging.log4j.core.LoggerContext;
2423
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;
2929

3030
/**
3131
* There are two logger.info() calls here.
@@ -42,23 +42,17 @@
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
4547
public 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
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration xmlns="https://logging.apache.org/xml/ns"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="
21+
https://logging.apache.org/xml/ns
22+
https://logging.apache.org/xml/ns/log4j-config-3.xsd">
23+
<Appenders>
24+
<List name="LIST">
25+
<PatternLayout pattern="%level %logger %m"/>
26+
</List>
27+
</Appenders>
28+
<Loggers>
29+
<Root level="TRACE">
30+
<AppenderRef ref="LIST"/>
31+
</Root>
32+
</Loggers>
33+
</Configuration>

log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public void start() {
371371
}
372372
LOGGER.info("Starting configuration {}...", this);
373373
this.setStarting();
374-
if (watchManager.getIntervalSeconds() >= 0) {
374+
if (watchManager.getIntervalSeconds() > 0) {
375375
LOGGER.info(
376376
"Start watching for changes to {} every {} seconds",
377377
getConfigurationSource(),

0 commit comments

Comments
 (0)