Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add option to omit/keep common frames
  • Loading branch information
brenuart committed Oct 11, 2022
commit 3ecb453e71ee6e1a339b4adb845da8bd4f0c5200
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,17 @@
* <li>maxLength = "full" or "short" or an integer value</li>
* </ol>
*
* If any other remaining options are "rootFirst",
* then the converter awill be configured as root-cause-first.
* If any other remaining options equal to an evaluator name,
* then the evaluator will be used to determine if the stacktrace should be printed.
* Other options will be interpreted as exclusion regexes.
* The other options can be listed in any order and are interpreted as follows:
* <ul>
* <li>"rootFirst" - indicating that stacks should be printed root-cause first
* <li>"inlineHash" - indicating that hexadecimal error hashes should be computed and inlined
* <li>"inline" - indicating that the whole stack trace should be inlined, using "\\n" as separator
* <li>"omitCommonFrames" - omit common frames
* <li>"keepCommonFrames" - keep common frames
* <li>evaluator name - name of evaluators that will determine if the stacktrace is ignored
* <li>exclusion pattern - pattern for stack trace elements to exclude
* </ul>
*
* <p>
* For example,
* <pre>
Expand All @@ -91,7 +97,7 @@
*
* <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
* <encoder>
* <pattern>[%thread] - %msg%n%stack{5,1024,10,rootFirst,regex1,regex2,evaluatorName}</pattern>
* <pattern>[%thread] - %msg%n%stack{5,1024,10,rootFirst,omitCommonFrames,regex1,regex2,evaluatorName}</pattern>
* </encoder>
* </appender>
* }
Expand All @@ -118,7 +124,8 @@ public class ShortenedThrowableConverter extends ThrowableHandlingConverter {
private static final String OPTION_VALUE_SHORT = "short";
private static final String OPTION_VALUE_ROOT_FIRST = "rootFirst";
private static final String OPTION_VALUE_INLINE_HASH = "inlineHash";

private static final String OPTION_VALUE_OMITCOMMONFRAMES = "omitCommonFrames";
private static final String OPTION_VALUE_KEEPCOMMONFRAMES = "keepCommonFrames";
private static final String OPTION_VALUE_INLINE_STACK = "inline";

private static final int OPTION_INDEX_MAX_DEPTH = 0;
Expand Down Expand Up @@ -180,6 +187,11 @@ public class ShortenedThrowableConverter extends ThrowableHandlingConverter {
*/
private boolean inlineHash;

/**
* True to omit common frames
*/
private boolean omitCommonFrames = true;

/** line delimiter */
private String lineSeparator = CoreConstants.LINE_SEPARATOR;

Expand Down Expand Up @@ -244,30 +256,46 @@ private void parseOptions() {
* - "rootFirst" - indicating that stacks should be printed root-cause first
* - "inlineHash" - indicating that hexadecimal error hashes should be computed and inlined
* - "inline" - indicating that the whole stack trace should be inlined, using "\\n" as separator
* - "omitCommonFrames" - omit common frames
* - "keepCommonFrames" - keep common frames
* - evaluator name - name of evaluators that will determine if the stacktrace is ignored
* - exclusion pattern - pattern for stack trace elements to exclude
*/
if (OPTION_VALUE_ROOT_FIRST.equals(option)) {
setRootCauseFirst(true);
} else if (OPTION_VALUE_INLINE_HASH.equals(option)) {
setInlineHash(true);
} else if (OPTION_VALUE_INLINE_STACK.equals(option)) {
setLineSeparator(DEFAULT_INLINE_SEPARATOR);
} else {
@SuppressWarnings("rawtypes")
Map evaluatorMap = (Map) getContext().getObject(CoreConstants.EVALUATOR_MAP);
@SuppressWarnings("unchecked")
EventEvaluator<ILoggingEvent> evaluator = (evaluatorMap != null)
? (EventEvaluator<ILoggingEvent>) evaluatorMap.get(option)
: null;

if (evaluator != null) {
addEvaluator(evaluator);
} else {
addExclude(option);
}
switch (option) {
case OPTION_VALUE_ROOT_FIRST:
setRootCauseFirst(true);
break;

case OPTION_VALUE_INLINE_HASH:
setInlineHash(true);
break;

case OPTION_VALUE_INLINE_STACK:
setLineSeparator(DEFAULT_INLINE_SEPARATOR);
break;

case OPTION_VALUE_OMITCOMMONFRAMES:
setOmitCommonFrames(true);
break;

case OPTION_VALUE_KEEPCOMMONFRAMES:
setOmitCommonFrames(false);
break;

default:
@SuppressWarnings("rawtypes")
Map evaluatorMap = (Map) getContext().getObject(CoreConstants.EVALUATOR_MAP);
@SuppressWarnings("unchecked")
EventEvaluator<ILoggingEvent> evaluator = (evaluatorMap != null)
? (EventEvaluator<ILoggingEvent>) evaluatorMap.get(option)
: null;

if (evaluator != null) {
addEvaluator(evaluator);
} else {
addExclude(option);
}
}
break;
}
}
}
Expand All @@ -281,7 +309,7 @@ private int parseIntegerOptionValue(String option, int valueIfFull, int valueIfS
try {
return Integer.parseInt(option);
} catch (NumberFormatException nfe) {
addError("Could not parse [" + option + "] as an integer");
addError("Could not parse [" + option + "] as an integer, default to " + valueIfNonParsable);
return valueIfNonParsable;
}
}
Expand Down Expand Up @@ -317,9 +345,6 @@ public String convert(ILoggingEvent event) {
return builder.toString();
}

public String getLineSeparator() {
return lineSeparator;
}

/**
* Sets which lineSeparator to use between events.
Expand All @@ -341,6 +366,11 @@ public void setLineSeparator(String lineSeparator) {
this.lineSeparator = SeparatorParser.parseSeparator(lineSeparator);
}

public String getLineSeparator() {
return lineSeparator;
}


/**
* Return true if any evaluator returns true, indicating that
* the stack trace should not be logged.
Expand Down Expand Up @@ -441,8 +471,8 @@ private void appendStackTraceElements(StringBuilder builder, int indent, IThrowa
return;
}
StackTraceElementProxy[] stackTraceElements = throwableProxy.getStackTraceElementProxyArray();
int commonFrames = throwableProxy.getCommonFrames();

int commonFrames = isOmitCommonFrames() ? throwableProxy.getCommonFrames() : 0;
boolean appendingExcluded = false;
int consecutiveExcluded = 0;
int appended = 0;
Expand Down Expand Up @@ -743,6 +773,20 @@ public int getMaxLength() {
return maxLength;
}


/**
* Control whether common frames should be omitted for nested throwables or not.
*
* @param omitCommonFrames {@code true} to omit common frames
*/
public void setOmitCommonFrames(boolean omitCommonFrames) {
this.omitCommonFrames = omitCommonFrames;
}

public boolean isOmitCommonFrames() {
return this.omitCommonFrames;
}

public boolean isRootCauseFirst() {
return rootCauseFirst;
}
Expand Down
Loading