Skip to content

Commit 7433b93

Browse files
committed
Add stack trace printer support for structured logging
Introduce a new `StackTracePrinter` interface (and a standard implementation) that can be used to print stack traces in a custom form. The existing `StructuredLoggingJsonProperties` have been updated with a nested `StackTrace` record that supports common customization options or allows a custom `StackTracePrinter` to be used. Closes gh-43864
1 parent 291e5d8 commit 7433b93

File tree

37 files changed

+1957
-129
lines changed

37 files changed

+1957
-129
lines changed

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/logging.adoc

+51
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,57 @@ You can also declare implementations by listing them in a `META-INF/spring.facto
647647

648648

649649

650+
[[features.logging.structured.customizing-stack-traces]]
651+
=== Customizing Structured Logging Stack Traces
652+
653+
Complete stack traces are included in the JSON output whenever a message is logged with an exception.
654+
This amount of information may be costly to process by your log ingestion system, so you may want to tune the way that stack traces are printed.
655+
656+
To do this, you can use one or more of the following properties:
657+
658+
|===
659+
| Property | Description
660+
661+
| configprop:logging.structured.json.stacktrace.root[]
662+
| Use `last` to print the root item last (same as Java) or `first` to print the root item first.
663+
664+
| configprop:logging.structured.json.stacktrace.max-length[]
665+
| The maximum length that should be printed
666+
667+
| configprop:logging.structured.json.stacktrace.max-throwable-depth[]
668+
| The maximum number of frames to print per stack trace (including common and suppressed frames)
669+
670+
| configprop:logging.structured.json.stacktrace.include-common-frames[]
671+
| If common frames should be included or removed
672+
673+
| configprop:logging.structured.json.stacktrace.include-hashes[]
674+
| If a hash of the stack trace should be included
675+
|===
676+
677+
For example, the following will use root first stack traces, limit their length, and include hashes.
678+
679+
[configprops,yaml]
680+
----
681+
logging:
682+
structured:
683+
json:
684+
stacktrace:
685+
root: first
686+
max-length: 1024
687+
include-common-frames: true
688+
include-hashes: true
689+
----
690+
691+
[TIP]
692+
====
693+
If you need complete control over stack trace printing you can set configprop:logging.structured.json.stacktrace.printer[] to the name of a javadoc:org.springframework.boot.logging.StackTracePrinter[] implementation.
694+
You can also set it to `logging-system` to force regular logging system stack trace output to be used.
695+
696+
Your `StackTracePrinter` implementation can also include a constructor argument that accepts a javadoc:org.springframework.boot.logging.StandardStackTracePrinter[] if it wishes to apply further customization to the stack trace printer created from the properties.
697+
====
698+
699+
700+
650701
[[features.logging.structured.other-formats]]
651702
=== Supporting Other Structured Logging Formats
652703

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.logging;
18+
19+
import java.io.IOException;
20+
import java.io.UncheckedIOException;
21+
22+
/**
23+
* Interface that can be used to print the stack trace of a {@link Throwable}.
24+
*
25+
* @author Phillip Webb
26+
* @since 3.5.0
27+
* @see StandardStackTracePrinter
28+
*/
29+
@FunctionalInterface
30+
public interface StackTracePrinter {
31+
32+
/**
33+
* Return a {@link String} containing the printed stack trace for a given
34+
* {@link Throwable}.
35+
* @param throwable the throwable that should have its stack trace printed
36+
* @return the stack trace string
37+
*/
38+
default String printStackTraceToString(Throwable throwable) {
39+
try {
40+
StringBuilder out = new StringBuilder(4096);
41+
printStackTrace(throwable, out);
42+
return out.toString();
43+
}
44+
catch (IOException ex) {
45+
throw new UncheckedIOException(ex);
46+
}
47+
}
48+
49+
/**
50+
* Prints a stack trace for the given {@link Throwable}.
51+
* @param throwable the throwable that should have its stack trace printed
52+
* @param out the destination to write output
53+
* @throws IOException on IO error
54+
*/
55+
void printStackTrace(Throwable throwable, Appendable out) throws IOException;
56+
57+
}

0 commit comments

Comments
 (0)