Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt ExtendedStackTraceElement to Java 9+ #1641

Merged
merged 1 commit into from
Aug 2, 2023

Conversation

ppkarwasz
Copy link
Contributor

Closes #1640.

This PR provides a JDK 9+ version of StackTraceElement and ExtendedStackTraceElement formatting and serialization. The formatting of these elements resembles the one used natively by the JDK:

(<classloader>/)?(<moduleName>(@<moduleVersion>)?/)?<formatting as in JDK 8>

The names of the builtin classloaders ("app", "boot", "platform") are omitted, as well as the versions of the built-in modules (unless reflective access to jdk.internal.module is forbidden) .

To test this PR, run ./mvnw verify -Pjava9-tests. Some unrelated test failures are to be expected.

@ppkarwasz
Copy link
Contributor Author

After this PR a sample stacktrace changes from:

java.lang.RuntimeException: null
	at eu.copernik.log4j.test.XmlTest.logError(XmlTest.java:18) ~[classes/:?]
	at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1736) [?:?]
	at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1728) [?:?]
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) [?:?]
	at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020) [?:?]
	at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656) [?:?]
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594) [?:?]
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183) [?:?]

to

java.lang.RuntimeException: null
	at eu.copernik.log4j.test@1.0.0/eu.copernik.log4j.test.XmlTest.logError(XmlTest.java:18) ~[classes/:?]
	at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1736) [?:?]
	at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1728) [?:?]
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) [?:?]
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020) [?:?]
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656) [?:?]
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594) [?:?]
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183) [?:?]

@ppkarwasz
Copy link
Contributor Author

A sample XML event passes from:

  <Event xmlns="http://logging.apache.org/log4j/2.0/events" timeMillis="1690800041924" thread="ForkJoinPool.commonPool-worker-19" level="INFO" loggerName="eu.copernik.log4j.test.XmlTest" endOfBatch="false" loggerFqcn="org.apache.logging.log4j.spi.AbstractLogger" threadId="15" threadPriority="5">
    <Instant epochSecond="1690800041" nanoOfSecond="924573100"/>
    <Message>Hello world!</Message>
    <Source class="eu.copernik.log4j.test.XmlTest" method="logError" file="XmlTest.java" line="19">
      <classLoaderName xmlns="">app</classLoaderName>
      <moduleName xmlns="">eu.copernik.log4j.test</moduleName>
      <moduleVersion xmlns="">1.0.0</moduleVersion>
    </Source>
    <Thrown commonElementCount="0" name="java.lang.RuntimeException">
      <ExtendedStackTrace>
        <ExtendedStackTraceItem class="eu.copernik.log4j.test.XmlTest" method="logError" file="XmlTest.java" line="18" exact="false" location="classes/" version="?"/>
        <ExtendedStackTraceItem class="java.util.concurrent.CompletableFuture$AsyncRun" method="run" file="CompletableFuture.java" line="1736" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem class="java.util.concurrent.CompletableFuture$AsyncRun" method="exec" file="CompletableFuture.java" line="1728" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem class="java.util.concurrent.ForkJoinTask" method="doExec" file="ForkJoinTask.java" line="290" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem class="java.util.concurrent.ForkJoinPool$WorkQueue" method="topLevelExec" file="ForkJoinPool.java" line="1020" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem class="java.util.concurrent.ForkJoinPool" method="scan" file="ForkJoinPool.java" line="1656" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem class="java.util.concurrent.ForkJoinPool" method="runWorker" file="ForkJoinPool.java" line="1594" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem class="java.util.concurrent.ForkJoinWorkerThread" method="run" file="ForkJoinWorkerThread.java" line="183" exact="true" location="?" version="?"/>
      </ExtendedStackTrace>
    </Thrown>
  </Event>

to

  <Event xmlns="http://logging.apache.org/log4j/2.0/events" timeMillis="1690800122073" thread="ForkJoinPool.commonPool-worker-19" level="INFO" loggerName="eu.copernik.log4j.test.XmlTest" endOfBatch="false" loggerFqcn="org.apache.logging.log4j.spi.AbstractLogger" threadId="17" threadPriority="5">
    <Instant epochSecond="1690800122" nanoOfSecond="73385600"/>
    <Message>Hello world!</Message>
    <Source classLoaderName="app" moduleName="eu.copernik.log4j.test" moduleVersion="1.0.0" class="eu.copernik.log4j.test.XmlTest" method="logError" file="XmlTest.java" line="19"/>
    <Thrown commonElementCount="0" name="java.lang.RuntimeException">
      <ExtendedStackTrace>
        <ExtendedStackTraceItem classLoaderName="app" moduleName="eu.copernik.log4j.test" moduleVersion="1.0.0" class="eu.copernik.log4j.test.XmlTest" method="logError" file="XmlTest.java" line="18" exact="false" location="classes/" version="?"/>
        <ExtendedStackTraceItem moduleName="java.base" moduleVersion="11.0.14.1" class="java.util.concurrent.CompletableFuture$AsyncRun" method="run" file="CompletableFuture.java" line="1736" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem moduleName="java.base" moduleVersion="11.0.14.1" class="java.util.concurrent.CompletableFuture$AsyncRun" method="exec" file="CompletableFuture.java" line="1728" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem moduleName="java.base" moduleVersion="11.0.14.1" class="java.util.concurrent.ForkJoinTask" method="doExec" file="ForkJoinTask.java" line="290" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem moduleName="java.base" moduleVersion="11.0.14.1" class="java.util.concurrent.ForkJoinPool$WorkQueue" method="topLevelExec" file="ForkJoinPool.java" line="1020" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem moduleName="java.base" moduleVersion="11.0.14.1" class="java.util.concurrent.ForkJoinPool" method="scan" file="ForkJoinPool.java" line="1656" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem moduleName="java.base" moduleVersion="11.0.14.1" class="java.util.concurrent.ForkJoinPool" method="runWorker" file="ForkJoinPool.java" line="1594" exact="true" location="?" version="?"/>
        <ExtendedStackTraceItem moduleName="java.base" moduleVersion="11.0.14.1" class="java.util.concurrent.ForkJoinWorkerThread" method="run" file="ForkJoinWorkerThread.java" line="183" exact="true" location="?" version="?"/>
      </ExtendedStackTrace>
    </Thrown>
  </Event>

The JSON and YAML layouts undergo similar changes.

@ppkarwasz ppkarwasz force-pushed the stack-trace-element9 branch 5 times, most recently from cedda1b to beb1770 Compare August 2, 2023 12:13
Closes apache#1640.

Co-authored-by: Ralph Goers <rgoers@apache.org>
@ppkarwasz ppkarwasz merged commit 381f508 into apache:2.x Aug 2, 2023
10 of 11 checks passed
@ppkarwasz ppkarwasz deleted the stack-trace-element9 branch August 2, 2023 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle additional StackTraceElement fields in JDK 9
1 participant