Skip to content

Commit aa264f7

Browse files
committed
test default variable values in appender-ref ref attribute
Signed-off-by: ceki <ceki@qos.ch>
1 parent 8fb403a commit aa264f7

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
<!--
3+
~ Logback: the reliable, generic, fast and flexible logging framework.
4+
~ Copyright (C) 1999-2026, QOS.ch. All rights reserved.
5+
~
6+
~ This program and the accompanying materials are dual-licensed under
7+
~ either the terms of the Eclipse Public License v1.0 as published by
8+
~ the Eclipse Foundation
9+
~
10+
~ or (per the licensee's choosing)
11+
~
12+
~ under the terms of the GNU Lesser General Public License version 2.1
13+
~ as published by the Free Software Foundation.
14+
-->
15+
16+
<!DOCTYPE configuration>
17+
18+
<configuration debug="false">
19+
20+
<appender name="A" class="ch.qos.logback.core.read.ListAppender"/>
21+
<appender name="NOP" class="ch.qos.logback.core.helpers.NOPAppender"/>
22+
23+
<root level="DEBUG">
24+
<appender-ref ref="${NONEXISTENT:-NOP}"/>
25+
<appender-ref ref="A"/>
26+
27+
</root>
28+
29+
</configuration>

logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import ch.qos.logback.core.ConsoleAppender;
2626
import ch.qos.logback.core.CoreConstants;
2727
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
28+
import ch.qos.logback.core.helpers.NOPAppender;
2829
import ch.qos.logback.core.joran.action.ParamAction;
2930
import ch.qos.logback.core.joran.spi.ActionException;
3031
import ch.qos.logback.core.joran.spi.JoranException;
@@ -205,7 +206,7 @@ public void appenderRefSettingBySystemPropertyDefault() throws JoranException {
205206
public void refToUndefinedAppender() throws JoranException {
206207

207208
configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "refToUndefinedAppender.xml");
208-
StatusPrinter.print(loggerContext);
209+
//StatusPrinter.print(loggerContext);
209210
final Logger logger = loggerContext.getLogger("ch.qos.logback.classic.joran");
210211
final ListAppender<ILoggingEvent> listAppender = (ListAppender<ILoggingEvent>) root.getAppender("A");
211212
assertNotNull(listAppender);
@@ -216,7 +217,27 @@ public void refToUndefinedAppender() throws JoranException {
216217
assertEquals(1, listAppender.list.size());
217218

218219
checker.assertContainsMatch(Status.WARN, "Appender named \\[NON_EXISTENT_APPENDER\\] could not be found. Skipping attachment to Logger\\[ROOT\\]");
220+
}
221+
222+
@Test
223+
public void refViaDefaultSubstitution() throws JoranException {
224+
225+
configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "refViaDefaultSubstitution.xml");
226+
StatusPrinter.print(loggerContext);
227+
final Logger logger = loggerContext.getLogger("ch.qos.logback.classic.joran");
228+
final ListAppender<ILoggingEvent> listAppender = (ListAppender<ILoggingEvent>) root.getAppender("A");
229+
final NOPAppender<ILoggingEvent> nopAppender = (NOPAppender) root.getAppender("NOP");
219230

231+
assertNotNull(listAppender);
232+
assertNotNull(nopAppender);
233+
234+
assertEquals(0, listAppender.list.size());
235+
final String msg = "hello world";
236+
logger.info(msg);
237+
238+
assertEquals(1, listAppender.list.size());
239+
240+
checker.assertIsWarningOrErrorFree();
220241
}
221242

222243

logback-core/src/main/java/ch/qos/logback/core/model/processor/AppenderDeclarationAnalyser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ protected Class<AppenderModel> getSupportedModelClass() {
4747
@Override
4848
public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
4949
AppenderModel appenderModel = (AppenderModel) model;
50+
String appenderName = mic.subst(appenderModel.getName());
5051

51-
String appenderName = appenderModel.getName();
5252
addAppenderDeclaration(mic, appenderName);
5353
}
5454

logback-core/src/main/java/ch/qos/logback/core/model/processor/ModelInterpretationContext.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ public BeanDescriptionCache getBeanDescriptionCache() {
159159
return beanDescriptionCache;
160160
}
161161

162+
/**
163+
* Performs variable substitution on the provided {@code ref} string.
164+
*
165+
* <p>Value substitution will follow the order</p>
166+
* <ol>
167+
* <li>properties defined in this {@link ModelInterpretationContext}</li>
168+
* <li>properties defined in the {@link Context context} of this {@link ModelInterpretationContext}</li>
169+
* <li>System properties</li>
170+
* <li>Environment variables</li>
171+
* </ol>
172+
*
173+
* <p>If value substitution occurs it will be output as a status message, unless marked confidential, that is,
174+
* if {@code ref} contains the case-insensitive strings PASSWORD, SECRET or CONFIDENTIAL.</p>
175+
*
176+
* @param ref the string that may contain variables to be substituted; can be {@code null}
177+
* @return the string with substitutions applied if applicable; may return {@code null} if {@code ref} is {@code null}
178+
*/
162179
public String subst(String ref) {
163180

164181
String substituted = variableSubstitutionsHelper.subst(ref);

logback-core/src/main/java/ch/qos/logback/core/util/OptionHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ public static String substVars(String input, PropertyContainer pc0, PropertyCont
115115

116116
/**
117117
* Try to lookup the property in the following order:
118-
* <ul>
119-
* <li>pc1 (usually the local property container)</li>
118+
* <ol>
119+
* <li>pc1 (the local property container, usually the {@link ch.qos.logback.core.model.processor.ModelInterpretationContext ModelInterpretationContext})</li>
120120
* <li>pc2 (usually the {@link Context context})</li>
121121
* <li>System properties</li>
122122
* <li>Environment variables</li>
123-
* </ul>
123+
* </ol>
124124
*
125125
* @param key the property key
126126
* @param pc1 the first property container to search

0 commit comments

Comments
 (0)