Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-3143

ThrowableProxyRenderer cannot handle options with depth and "short"

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.14.1
    • None
    • Darwin 18.7.0 Darwin Kernel Version 18.7.0

      java version "11.0.11" 2021-04-20 LTS
      Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194)
      Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode)

       

    • Important

    Description

      The Document said that %xEx{short} which will only output the first line of the Throwable or %xEx{n} where the first n lines of the stacktrace will be printed. But it actually not work.

      I followed the code in ThrowableProxyRenderer.java . It turns out that the method here not consider about these options at all.

      static void formatExtendedStackTraceTo(final ThrowableProxy src, final StringBuilder sb, final List<String> ignorePackages, final TextRenderer textRenderer, final String suffix, final String lineSeparator) {
          textRenderer.render(src.getName(), sb, "Name");
          textRenderer.render(": ", sb, "NameMessageSeparator");
          textRenderer.render(src.getMessage(), sb, "Message");
          renderSuffix(suffix, sb, textRenderer);
          textRenderer.render(lineSeparator, sb, "Text");
          final StackTraceElement[] causedTrace = src.getThrowable() != null ? src.getThrowable().getStackTrace() : null;
          formatElements(sb, Strings.EMPTY, 0, causedTrace, src.getExtendedStackTrace(), ignorePackages, textRenderer, suffix, lineSeparator);
          formatSuppressed(sb, TAB, src.getSuppressedProxies(), ignorePackages, textRenderer, suffix, lineSeparator);
          formatCause(sb, Strings.EMPTY, src.getCauseProxy(), ignorePackages, textRenderer, suffix, lineSeparator);
      }
      

      Here are my test case

      import org.apache.logging.log4j.LogManager;
      import org.apache.logging.log4j.Logger;
      
      public class Test {
          private static final Logger logger = LogManager.getLogger(Test.class);
      
          private static void makeADeepErr(int i, int n) throws Exception {
              if (i < n) {
                  makeADeepErr(i + 1, n);
              }
              throw new Exception("err");
          }
      
          public static void main(String[] args) {
              try {
                  makeADeepErr(0, 20);
              } catch (Exception e) {
                  logger.error("err occur", e);
              }
          }
      }
      
      

      And here are my log4j2.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <Configuration status="WARN">
          <Appenders>
              <Console name="Console" target="SYSTEM_OUT" follow="true">
                  <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n %xEx{short}"/>
              </Console>
      
          </Appenders>
          <Loggers>
              <Root level="info">
                  <AppenderRef ref="Console"/>
              </Root>
          </Loggers>
      </Configuration>
      
      

      And here are my output

      01:03:26.877 [main] ERROR Test - err occur
       java.lang.Exception: err
      	at Test.makeADeepErr(Test.java:11) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.makeADeepErr(Test.java:9) ~[classes/:?]
      	at Test.main(Test.java:16) [classes/:?]
      

       But the %xEx{none} or %xEx{0} can work fine.

      Attachments

        Activity

          People

            Unassigned Unassigned
            alset alset
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - 2h
                2h
                Remaining:
                Remaining Estimate - 2h
                2h
                Logged:
                Time Spent - Not Specified
                Not Specified