Skip to content

Commit

Permalink
Add customized GraalVM configurations (#213)
Browse files Browse the repository at this point in the history
By using the new `log4j-convert-plugin-descriptor` tool introduced in
apache/logging-log4j-transform#139
this PR:

1. Adds a customized `use-log4j-core-jtl-minimal` GraalVM profile that
   only includes the plugins required by JSON Template Layout.
2. Generates the existent `use-log4j-core-minimal` GraalVM profile using
   a custom `Log4j2Plugins.json` file. The previous profile was edited
   manually.

Closes apache/logging-log4j2#2834.
  • Loading branch information
ppkarwasz authored Dec 19, 2024
1 parent baaf80e commit 6a251a6
Show file tree
Hide file tree
Showing 20 changed files with 3,509 additions and 302 deletions.
100 changes: 54 additions & 46 deletions log4j-samples-graalvm/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ limitations under the License.
This project shows how to generate a native image using GraalVM.
In order to use it:
To use it:
. set the `JAVA_HOME` environment variable to the path of a GraalVM installation,
. set the current working directory to the folder containing this file,
. generate the artifact locally with:
. Set the `JAVA_HOME` environment variable to the path of a GraalVM installation,
. Set the current working directory to the folder containing this file,
. Generate the artifact locally with:
+
[source,shell]
----
Expand All @@ -32,24 +32,42 @@ In order to use it:
+
where `<backend>` can be:
`simple`::
This profile uses `SimpleLogger` as logging backend.
`jul`::
This profile uses `java.util.logging` as logging backend.
`logback`::
This profile uses Logback as logging backend.
`log4j-core`::
This profile uses Log4j Core as logging backend with its out-of-the-box reachability metadata.
It uses
https://logging.apache.org/log4j/2.x/manual/pattern-layout.html[Pattern Layout]
to format log events.
`log4j-core-jtl`::
This profile uses Log4j Core as logging backend with the addition of JSON Template Layout.
This profile uses Log4j Core as logging backend with its out-of-the-box reachability metadata.
It uses
https://logging.apache.org/log4j/2.x/manual/json-template-layout.html[JSON Template Layout]
to format log events.
[#log4j-core-minimal]
`log4j-core-minimal`::
This profile uses Log4j Core a logging backend with an external reachability metadata source optimized for size.
It uses
https://logging.apache.org/log4j/2.x/manual/pattern-layout.html[Pattern Layout]
to format log events.
`logback`::
This profile uses Logback as logging backend.
`jul`::
This profile uses `java.util.logging` as logging backend.
[#log4j-core-jtl-minimal]
`log4j-core-jtl-minimal`::
This profile uses Log4j Core a logging backend with an external reachability metadata source optimized for size.
It uses
https://logging.apache.org/log4j/2.x/manual/json-template-layout.html[JSON Template Layout]
to format log events.
`simple`::
This profile uses `SimpleLogger` as logging backend.
. run the application with
. Run the application with
+
[source,shell]
----
Expand All @@ -58,10 +76,9 @@ target/log4j-samples-graalvm
== Reachability metadata
Log4j as many other Java libraries, relies heavily on reflection to convert the configuration file into an instance of a
Log4j, as many other Java libraries, relies heavily on reflection to convert the configuration file into an instance of a
https://logging.apache.org/log4j/2.x/javadoc/log4j-core/org/apache/logging/log4j/core/config/Configuration.html[Configuration]
object.
The classes that are accessed through reflection are called Log4j plugins and GraalVM must be aware of their existence through the usage of reachability metadata.
See
Expand All @@ -70,28 +87,15 @@ and
https://www.graalvm.org/latest/reference-manual/native-image/metadata/[GraalVM reachability metadata]
for more details.
The reachability metadata in this project was:
* generated using the
https://www.graalvm.org/latest/reference-manual/native-image/metadata/AutomaticMetadataCollection/#tracing-agent[GraalVM tracing agent]:
+
[source,shell,subs=+attributes]
----
# Build application classpath
export CLASSPATH=$(\
../mvnw -q \
dependency:build-classpath -DincludeScope=runtime -Dmdep.outputProperty=classpath \
help:evaluate -Dexpression=classpath -DforceStdout \
)
CLASSPATH="target/log4j-samples-graalvm-0.0.0-SNAPSHOT.jar:$CLASSPATH"
# Run application using the GraalVM agent
$JAVA_HOME/bin/java \
-agentlib:native-image-agent=config-output-dir=src/reachability-metadata/minimal/log4j-core/2.24.0 \
org.apache.logging.log4j.samples.graalvm.Main
----
* the generated `reflect-config.json` file was further trimmed down manually, by removing all the Log4j plugins that are not used by this configuration.
These plugins are easy to spot, because they only have a `name` key.
The
https://logging.apache.org/log4j/2.x/manual/plugins.html#plugin-registry[Log4j plugin descriptors]
and
GraalVM reachability metadata used in the customized
<<log4j-core-minimal,`use-log4j-core-minimal`>>
and
<<log4j-core-jtl-minimal,`use-log4j-core-jtl-minimal`>>
profiles were generated using the procedure described in the documentation of the
https://logging.apache.org/log4j/transform/cli.html#log4j-converter-plugin-descriptor[`log4j-converter-plugin-descriptor` tool].
== Native binary size
Expand All @@ -104,25 +108,29 @@ Since this is a trivial Java application, the difference between the binary size
In a typical application, those classes are already required, so the difference should be smaller.
====
[cols="1,1"]
[cols="3,1"]
|===
| Configuration | Size
| With `SimpleLogger`
| 13.90 MiB
| With `java.util.logging`
| 14.10 MiB
| With Logback
| 30.62 MiB
| With Log4j Core (manually optimized reachability metadata)
| 36.19 MiB
| With Log4j Core + JSON Template Layout (manually optimized reachability metadata)
| 37.40 MiB
| With Log4j Core (embedded full reachability metadata)
| 46.80 MiB
| With Log4j Core + JSON Template Layout (embedded full reachability metadata)
| 47.37 MiB
| With Logback
| 30.62 MiB
| With `java.util.logging`
| 14.10 MiB
| With `SimpleLogger`
| 13.90 MiB
|===
Loading

0 comments on commit 6a251a6

Please sign in to comment.