Skip to content

Commit 328c65d

Browse files
committed
Merge pull request #50303 from mheath
Closes gh-50303 * pr/50303: Allow Environment to be passed to custom StackTracePrinter instances
2 parents 9b00740 + 686db1e commit 328c65d

3 files changed

Lines changed: 26 additions & 19 deletions

File tree

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLogFormatterFactory.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public StructuredLogFormatterFactory(Class<E> logEventType, Environment environm
8989
new JsonMembersCustomizerBuilder(properties).build());
9090
allAvailableParameters.add(StructuredLoggingJsonMembersCustomizer.Builder.class,
9191
new JsonMembersCustomizerBuilder(properties));
92-
allAvailableParameters.add(StackTracePrinter.class, (type) -> getStackTracePrinter(properties));
92+
allAvailableParameters.add(StackTracePrinter.class,
93+
(type) -> getStackTracePrinter(properties, environment));
9394
allAvailableParameters.add(ContextPairs.class, (type) -> getContextPairs(properties));
9495
if (availableParameters != null) {
9596
availableParameters.accept(allAvailableParameters);
@@ -99,8 +100,10 @@ public StructuredLogFormatterFactory(Class<E> logEventType, Environment environm
99100
commonFormatters.accept(this.commonFormatters);
100101
}
101102

102-
private StackTracePrinter getStackTracePrinter(StructuredLoggingJsonProperties properties) {
103-
return (properties != null && properties.stackTrace() != null) ? properties.stackTrace().createPrinter() : null;
103+
private StackTracePrinter getStackTracePrinter(StructuredLoggingJsonProperties properties,
104+
Environment environment) {
105+
return (properties != null && properties.stackTrace() != null)
106+
? properties.stackTrace().createPrinter(environment) : null;
104107
}
105108

106109
private ContextPairs getContextPairs(StructuredLoggingJsonProperties properties) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonProperties.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static StructuredLoggingJsonProperties get(Environment environment) {
9090
record StackTrace(String printer, Root root, Integer maxLength, Integer maxThrowableDepth,
9191
Boolean includeCommonFrames, Boolean includeHashes) {
9292

93-
StackTracePrinter createPrinter() {
93+
StackTracePrinter createPrinter(Environment environment) {
9494
String name = sanitizePrinter();
9595
if ("loggingsystem".equals(name) || (name.isEmpty() && !hasAnyOtherProperty())) {
9696
return null;
@@ -99,9 +99,10 @@ StackTracePrinter createPrinter() {
9999
if ("standard".equals(name) || name.isEmpty()) {
100100
return standardPrinter;
101101
}
102-
return (StackTracePrinter) new Instantiator<>(StackTracePrinter.class,
103-
(parameters) -> parameters.add(StandardStackTracePrinter.class, standardPrinter))
104-
.instantiate(printer());
102+
return (StackTracePrinter) new Instantiator<>(StackTracePrinter.class, (parameters) -> {
103+
parameters.add(StandardStackTracePrinter.class, standardPrinter);
104+
parameters.add(Environment.class, environment);
105+
}).instantiate(printer());
105106
}
106107

107108
boolean hasCustomPrinter() {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesTests.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.boot.logging.structured.StructuredLoggingJsonProperties.StackTrace;
3636
import org.springframework.boot.logging.structured.StructuredLoggingJsonProperties.StackTrace.Root;
3737
import org.springframework.boot.logging.structured.StructuredLoggingJsonProperties.StructuredLoggingJsonPropertiesRuntimeHints;
38+
import org.springframework.core.env.Environment;
3839
import org.springframework.mock.env.MockEnvironment;
3940
import org.springframework.util.ClassUtils;
4041

@@ -115,44 +116,44 @@ class StackTraceTests {
115116
@Test
116117
void createPrinterWhenEmptyReturnsNull() {
117118
StackTrace properties = new StackTrace(null, null, null, null, null, null);
118-
assertThat(properties.createPrinter()).isNull();
119+
assertThat(properties.createPrinter(new MockEnvironment())).isNull();
119120
}
120121

121122
@Test
122123
void createPrinterWhenNoPrinterAndNotEmptyReturnsStandard() {
123124
StackTrace properties = new StackTrace(null, Root.LAST, null, null, null, null);
124-
assertThat(properties.createPrinter()).isInstanceOf(StandardStackTracePrinter.class);
125+
assertThat(properties.createPrinter(new MockEnvironment())).isInstanceOf(StandardStackTracePrinter.class);
125126
}
126127

127128
@Test
128129
void createPrinterWhenLoggingSystemReturnsNull() {
129130
StackTrace properties = new StackTrace("logging-system", null, null, null, null, null);
130-
assertThat(properties.createPrinter()).isNull();
131+
assertThat(properties.createPrinter(new MockEnvironment())).isNull();
131132
}
132133

133134
@Test
134135
void createPrinterWhenLoggingSystemRelaxedReturnsNull() {
135136
StackTrace properties = new StackTrace("LoggingSystem", null, null, null, null, null);
136-
assertThat(properties.createPrinter()).isNull();
137+
assertThat(properties.createPrinter(new MockEnvironment())).isNull();
137138
}
138139

139140
@Test
140141
void createPrinterWhenStandardReturnsStandardPrinter() {
141142
StackTrace properties = new StackTrace("standard", null, null, null, null, null);
142-
assertThat(properties.createPrinter()).isInstanceOf(StandardStackTracePrinter.class);
143+
assertThat(properties.createPrinter(new MockEnvironment())).isInstanceOf(StandardStackTracePrinter.class);
143144
}
144145

145146
@Test
146147
void createPrinterWhenStandardRelaxedReturnsStandardPrinter() {
147148
StackTrace properties = new StackTrace("STANDARD", null, null, null, null, null);
148-
assertThat(properties.createPrinter()).isInstanceOf(StandardStackTracePrinter.class);
149+
assertThat(properties.createPrinter(new MockEnvironment())).isInstanceOf(StandardStackTracePrinter.class);
149150
}
150151

151152
@Test
152153
void createPrinterWhenStandardAppliesCustomizations() {
153154
Exception exception = TestException.create();
154155
StackTrace properties = new StackTrace(null, Root.FIRST, 300, 2, true, false);
155-
StackTracePrinter printer = ((StandardStackTracePrinter) properties.createPrinter())
156+
StackTracePrinter printer = ((StandardStackTracePrinter) properties.createPrinter(new MockEnvironment()))
156157
.withLineSeparator("\n");
157158
String actual = TestException.withoutLineNumbers(printer.printStackTraceToString(exception));
158159
assertThat(actual).isEqualToNormalizingNewlines("""
@@ -167,7 +168,7 @@ void createPrinterWhenStandardAppliesCustomizations() {
167168
void createPrinterWhenStandardWithHashesPrintsHash() {
168169
Exception exception = TestException.create();
169170
StackTrace properties = new StackTrace(null, null, null, null, null, true);
170-
StackTracePrinter printer = properties.createPrinter();
171+
StackTracePrinter printer = properties.createPrinter(new MockEnvironment());
171172
String actual = printer.printStackTraceToString(exception);
172173
assertThat(actual).containsPattern("<#[0-9a-z]{8}>");
173174
}
@@ -176,16 +177,18 @@ void createPrinterWhenStandardWithHashesPrintsHash() {
176177
void createPrinterWhenClassNameCreatesPrinter() {
177178
Exception exception = TestException.create();
178179
StackTrace properties = new StackTrace(TestStackTracePrinter.class.getName(), null, null, null, true, null);
179-
StackTracePrinter printer = properties.createPrinter();
180+
StackTracePrinter printer = properties.createPrinter(new MockEnvironment());
180181
assertThat(printer.printStackTraceToString(exception)).isEqualTo("java.lang.RuntimeException: exception");
181182
}
182183

183184
@Test
184185
void createPrinterWhenClassNameInjectsConfiguredPrinter() {
185186
Exception exception = TestException.create();
187+
MockEnvironment environment = new MockEnvironment();
188+
environment.setProperty("stracktracelineseparator", "!");
186189
StackTrace properties = new StackTrace(TestStackTracePrinterCustomized.class.getName(), Root.FIRST, 300, 2,
187190
true, null);
188-
StackTracePrinter printer = properties.createPrinter();
191+
StackTracePrinter printer = properties.createPrinter(environment);
189192
String actual = TestException.withoutLineNumbers(printer.printStackTraceToString(exception));
190193
assertThat(actual).isEqualTo("RuntimeExceptionroot! at org.springfr...");
191194
}
@@ -237,9 +240,9 @@ static class TestStackTracePrinterCustomized implements StackTracePrinter {
237240

238241
private final StandardStackTracePrinter printer;
239242

240-
TestStackTracePrinterCustomized(StandardStackTracePrinter printer) {
243+
TestStackTracePrinterCustomized(StandardStackTracePrinter printer, Environment environment) {
241244
this.printer = printer.withMaximumLength(40)
242-
.withLineSeparator("!")
245+
.withLineSeparator(environment.getProperty("stracktracelineseparator", "\n"))
243246
.withFormatter((throwable) -> ClassUtils.getShortName(throwable.getClass()) + throwable.getMessage());
244247
}
245248

0 commit comments

Comments
 (0)