Skip to content

Commit 466fe5e

Browse files
committed
Rename ShortenedThrowableConverter.SANITIZE to OMIT_THROWABLE_MESSAGE
add unit test
1 parent dda429e commit 466fe5e

File tree

3 files changed

+53
-15
lines changed

3 files changed

+53
-15
lines changed

README.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ The structure of the output, and the data it contains, is fully configurable.
6666
* [Omit Common Frames](#omit-common-frames)
6767
* [Truncate after Regex](#truncate-after-regex)
6868
* [Exclude Frames per Regex](#exclude-frames-per-regex)
69-
* [Exclude Messages From Stacktrace](#exclude-messages-from-stacktrace)
69+
* [Omit Throwable Messages](#omit-throwable-messages)
7070
* [Maximum Depth per Throwable](#maximum-depth-per-throwable)
71-
* [Maximum Trace Size (bytes)](#maximum-trace-size)
71+
* [Maximum Trace Size (bytes)](#maximum-trace-size-bytes)
7272
* [Classname Shortening](#classname-shortening)
7373
* [Custom Line Separator](#custom-line-separator)
7474
* [Root Cause First](#root-cause-first)
@@ -2125,11 +2125,13 @@ Alternatively, multiple exclusion patterns can be specified at once using the `<
21252125
Using the `<exclusions>` configuration option can be useful when using an environment variable to specify the actual patterns at deployment time.
21262126

21272127

2128-
### Exclude Messages From Stacktrace
2129-
In the event you wish to exclude all messages from the stackrace, then use ShortenedThrowableConverter.SANITIZE in you log statement.
2130-
This feature will exclude all messages in the entire stacktrace from appearing.
2128+
### Omit Throwable Messages
2129+
2130+
To omit throwable messages from stacktraces, add the `ShortenedThrowableConverter.OMIT_THROWABLE_MESSAGE` marker
2131+
to log statements.
2132+
2133+
Consider the following stacktrace (without omitting messages):
21312134

2132-
**Example**
21332135
```
21342136
Exception in thread "main" com.myproject.module.MyProjectFooBarException: Customer ssn of 12345678 was not registered
21352137
at com.myproject.module.MyProject.anotherMethod(MyProject.java:19)
@@ -2141,11 +2143,17 @@ Caused by: java.lang.ArithmeticException: Could not generate userId for Customer
21412143
... 2 more
21422144
```
21432145

2144-
If the above is logged utilizing the ShortenedThrowableConverter.SANITIZE marker, then all messages will be suppressed in the logging output.
2146+
If the `ShortenedThrowableConverter.OMIT_THROWABLE_MESSAGE` marker is used when logging the above throwable,
2147+
then the `ShortenedThrowableConverter` will omit all messages in the stacktrace.
2148+
2149+
For example, the following code:
2150+
21452151
```java
2146-
log.error(SANITIZE, "An exception was thrown but I want to make sure no customer data is shown in stacktrace", e);
2152+
logger.error(OMIT_THROWABLE_MESSAGE, "An exception was thrown but I want to make sure no customer data is shown in stacktrace", e);
21472153
```
2148-
Will produce:
2154+
2155+
will produce:
2156+
21492157
```
21502158
Exception in thread "main" com.myproject.module.MyProjectFooBarException:
21512159
at com.myproject.module.MyProject.anotherMethod(MyProject.java:19)
@@ -2156,6 +2164,7 @@ Caused by: java.lang.ArithmeticException:
21562164
at com.myproject.module.MyProject.anotherMethod(MyProject.java:17)
21572165
... 2 more
21582166
```
2167+
21592168
This enables devs to still see the type of exception thrown and where it occurred, **without** exposing sensitive data.
21602169

21612170
### Maximum Depth per Throwable
@@ -2876,7 +2885,7 @@ The provider name is the xml element name to use when configuring. Each provider
28762885

28772886

28782887

2879-
### Providers for AccessEvents
2888+
### Providers for AccessEvents
28802889

28812890
The [common providers mentioned above](#providers-common-to-loggingevents-and-accessevents), and the providers listed in the table below, are available for _AccessEvents_.
28822891
The provider name is the xml element name to use when configuring. Each provider's configuration properties are shown, with default configuration values in parenthesis.

src/main/java/net/logstash/logback/stacktrace/ShortenedThrowableConverter.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public class ShortenedThrowableConverter extends ThrowableHandlingConverter {
118118
public static final int SHORT_CLASS_NAME_LENGTH = 10;
119119
public static final int DEFAULT_CLASS_NAME_LENGTH = FULL_CLASS_NAME_LENGTH;
120120

121+
public static final Marker OMIT_THROWABLE_MESSAGE = MarkerFactory.getMarker("OMIT_THROWABLE_MESSAGE");
122+
121123
private static final String ELLIPSIS = "...";
122124
private static final int BUFFER_INITIAL_CAPACITY = 4096;
123125

@@ -133,8 +135,6 @@ public class ShortenedThrowableConverter extends ThrowableHandlingConverter {
133135
private static final int OPTION_INDEX_SHORTENED_CLASS_NAME = 1;
134136
private static final int OPTION_INDEX_MAX_LENGTH = 2;
135137

136-
public static final Marker SANITIZE = MarkerFactory.getMarker("SANITIZE");
137-
138138
/**
139139
* String sequence to use to delimit lines instead of {@link CoreConstants#LINE_SEPARATOR}
140140
* when inline is active
@@ -422,7 +422,8 @@ private void appendRootCauseLast(
422422
String prefix,
423423
int indent,
424424
IThrowableProxy throwableProxy,
425-
Deque<String> stackHashes, ILoggingEvent event) {
425+
Deque<String> stackHashes,
426+
ILoggingEvent event) {
426427

427428
if (throwableProxy == null || builder.length() > this.maxLength) {
428429
return;
@@ -451,7 +452,8 @@ private void appendRootCauseFirst(
451452
String prefix,
452453
int indent,
453454
IThrowableProxy throwableProxy,
454-
Deque<String> stackHashes, ILoggingEvent event) {
455+
Deque<String> stackHashes,
456+
ILoggingEvent event) {
455457

456458
if (throwableProxy == null || builder.length() > this.maxLength) {
457459
return;
@@ -699,7 +701,7 @@ private void indent(StringBuilder builder, int indent) {
699701
}
700702

701703
protected String getMessage(IThrowableProxy throwableProxy, ILoggingEvent event) {
702-
if (event.getMarkerList()!= null && event.getMarkerList().contains(SANITIZE)){
704+
if (event.getMarkerList() != null && event.getMarkerList().contains(OMIT_THROWABLE_MESSAGE)) {
703705
return "";
704706
}
705707
return throwableProxy.getMessage();

src/test/java/net/logstash/logback/stacktrace/ShortenedThrowableConverterTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,33 @@ public void test_inline_hash_with_suppressed() {
813813
}
814814
}
815815

816+
@Test
817+
public void testOmitThrowableMessage() {
818+
819+
try {
820+
StackTraceElementGenerator.generateSingle();
821+
fail("Exception must have been thrown");
822+
} catch (RuntimeException e) {
823+
824+
/*
825+
* First get the stacktrace with the message
826+
*/
827+
ShortenedThrowableConverter converter = new ShortenedThrowableConverter();
828+
converter.start();
829+
830+
String stacktraceWithMessage = convert(converter, e);
831+
assertThat(stacktraceWithMessage).contains("message");
832+
833+
/*
834+
* Now omit the message
835+
*/
836+
ILoggingEvent event = createEvent(e);
837+
when(event.getMarkerList()).thenReturn(List.of(ShortenedThrowableConverter.OMIT_THROWABLE_MESSAGE));
838+
String stacktraceWithoutMessage = convert(converter, event);
839+
assertThat(stacktraceWithoutMessage).doesNotContain("message");
840+
}
841+
}
842+
816843

817844
// --------------------------------------------------------------------------------------------
818845

0 commit comments

Comments
 (0)