[
https://issues.apache.org/jira/browse/LOG4J2-3627?focusedWorklogId=924192&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-924192
]
ASF GitHub Bot logged work on LOG4J2-3627:
------------------------------------------
Author: ASF GitHub Bot
Created on: 02/Jul/24 16:59
Start Date: 02/Jul/24 16:59
Worklog Time Spent: 10m
Work Description: vy commented on code in PR #2691:
URL: https://github.com/apache/logging-log4j2/pull/2691#discussion_r1654740592
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java:
##########
@@ -295,12 +295,35 @@ public String getCauseStackTraceAsString(
final List<String> ignorePackages,
final TextRenderer textRenderer,
final String suffix,
- final String lineSeparator) {
+ final String lineSeparator,
+ final Integer linesToKeep) {
final StringBuilder sb = new StringBuilder();
- ThrowableProxyRenderer.formatCauseStackTrace(this, sb, ignorePackages,
textRenderer, suffix, lineSeparator);
+ ThrowableProxyRenderer.formatCauseStackTraceTo(
+ this, sb, ignorePackages, textRenderer, suffix, lineSeparator,
linesToKeep);
return sb.toString();
}
+ /**
+ * Formats the stack trace with cause exception.
+ *
+ * @param sb Destination.
+ * @param ignorePackages List of packages to be ignored in the trace.
+ * @param textRenderer The message renderer.
+ * @param suffix Append this to the end of each stack frame.
+ * @param lineSeparator The end-of-line separator.
+ * @param linesToKeep The total line count of final result
+ */
+ public void formatCauseStackTraceTo(
Review Comment:
@alan0428a, as @ppkarwasz indicated, first you need to remove your changes
breaking the backward compatibility of existing methods – this is why
`bnd-baseline` is requiring a major version bump, you're breaking backward
compatibility. Instead,
1. Add new methods with new arguments
2. Redirect old methods to these new methods (pass the new arguments as
`null`, etc.)
3. Migrate existing code using old methods to new methods
4. Mark old methods as `@Deprecated`
5. Bump the minor version in the `package-info.java`
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxyRenderer.java:
##########
@@ -309,4 +311,24 @@ private static void renderOn(
textRenderer.render(msg, output, "Message");
}
}
+
+ private static void truncateLines(
+ StringBuilder sb, String lineSeparator, Integer linesToKeep,
TextRenderer textRenderer) {
+ if (linesToKeep == null) {
+ return;
+ }
+
+ String content = sb.toString();
+ String[] lines = content.split(lineSeparator);
+
+ if (lines.length <= linesToKeep) {
+ return;
+ }
+
+ sb.setLength(0);
+ for (int i = 0; i < linesToKeep; i++) {
+ sb.append(lines[i]);
+ textRenderer.render(lineSeparator, sb, "Text");
+ }
Review Comment:
No, we don't need to limit `lineSeparator` to a single character and we
don't need to implement KMP. You can use `StringBuilder#indexOf(String)` method.
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverter.java:
##########
@@ -235,4 +235,69 @@ protected String getSuffix(final LogEvent event) {
public ThrowableFormatOptions getOptions() {
return options;
}
+
+ private boolean requireAdditionalFormatting(String suffix) {
+ return !options.allLines() || nonStandardLineSeparator ||
Strings.isNotBlank(suffix) || options.hasPackages();
+ }
+
+ private boolean ignoreElement(final StackTraceElement element, final
List<String> ignorePackages) {
+ if (ignorePackages != null) {
+ final String className = element.getClassName();
+ for (final String pkg : ignorePackages) {
+ if (className.startsWith(pkg)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ private void appendSuppressedCount(
+ final StringBuilder sb, final int count, final String suffix,
final String lineSeparator) {
+ if (count == 1) {
+ sb.append("\t... ");
+ } else {
+ sb.append("\t... suppressed ");
+ sb.append(count);
+ sb.append(" lines");
+ }
+ appendSuffix(sb, suffix);
+ sb.append(lineSeparator);
+ }
+
+ private void appendEntry(
+ final StackTraceElement stackTraceElement,
+ final StringBuilder sb,
+ final String suffix,
+ final String lineSeparator) {
+ sb.append(stackTraceElement.toString());
+ appendSuffix(sb, suffix);
+ sb.append(lineSeparator);
+ }
+
+ private void appendSuffix(StringBuilder buffer, String suffix) {
+ if (Strings.isNotBlank(suffix)) {
+ buffer.append(' ');
+ buffer.append(suffix);
+ }
+ }
+
+ private void truncateLines(StringBuilder sb, String lineSeparator, Integer
linesToKeep) {
Review Comment:
No, please don't touch to the API. You need to create a new class in
`log4j-core`. Thinking about it, better we make it explicit that it is intended
for internal usage: Place the class to
`org.apache.logging.log4j.core.util.internal` package in `log4j-core`.
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxyRenderer.java:
##########
@@ -309,4 +311,24 @@ private static void renderOn(
textRenderer.render(msg, output, "Message");
}
}
+
+ private static void truncateLines(
+ StringBuilder sb, String lineSeparator, Integer linesToKeep,
TextRenderer textRenderer) {
Review Comment:
I still see it in several other places: in `truncateLines()` methods,
variables in changes to `ThrowablePatternConverter`, etc.
Issue Time Tracking
-------------------
Worklog Id: (was: 924192)
Time Spent: 1h (was: 50m)
> PatternLayout: %xEx{ [ "short" | depth]} not working
> ----------------------------------------------------
>
> Key: LOG4J2-3627
> URL: https://issues.apache.org/jira/browse/LOG4J2-3627
> Project: Log4j 2
> Issue Type: Bug
> Components: Layouts
> Affects Versions: 2.11.0, 2.11.1, 2.11.2, 2.12.0, 2.12.1, 2.13.0, 2.13.1,
> 2.13.2, 2.14.0, 2.13.3, 2.14.1, 2.15.0, 2.16.0, 2.17.1, 2.17.0, 2.12.3,
> 2.12.2, 2.18.0, 2.12.4, 2.17.2, 2.19.0
> Reporter: Thorsten Heit
> Assignee: Volkan Yazici
> Priority: Minor
> Time Spent: 1h
> Remaining Estimate: 0h
>
> According to the documentation the patterns {{{}%xEx{short{}}}} or
> {{{}%xEx{<num>{}}}} should limit the number of lines of a stack trace that is
> logged. This doesn't work; instead, the complete stack trace is logged
> (always!). This is in contrary to the patterns {{%ex}} or {{%rEx}} where this
> works.
> In commit 9ff63b2e50be754ae394feda2c33d9e64fd0ab3a (2018-01-25) a change was
> implemented because of LOG4J2-2195 (according to the commit message) that
> removed the option of limiting the number of lines to output.
> Therefore all versions since 2.11.0 should be affected.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)