You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2024/10/04 13:59:55 UTC

(logging-log4j-samples) branch main updated: Add JTL profile to GraalVM test

This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j-samples.git


The following commit(s) were added to refs/heads/main by this push:
     new d662d6d  Add JTL profile to GraalVM test
d662d6d is described below

commit d662d6dc3565706e4d121e06f9a283307ad239cd
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Fri Oct 4 15:59:49 2024 +0200

    Add JTL profile to GraalVM test
---
 .github/workflows/graalvm-test.yaml                |  2 +-
 log4j-samples-graalvm/README.adoc                  |  6 ++
 log4j-samples-graalvm/pom.xml                      | 75 +++++++++++++++++++++-
 .../src/main/resources/log4j2-jtl.xml              | 32 +++++++++
 .../{StandardIT.java => JsonTemplateLayoutIT.java} | 31 ++++++---
 .../logging/log4j/samples/graalvm/JulIT.java       |  2 +-
 .../logging/log4j/samples/graalvm/SimpleIT.java    |  2 +-
 .../logging/log4j/samples/graalvm/StandardIT.java  |  2 +-
 8 files changed, 139 insertions(+), 13 deletions(-)

diff --git a/.github/workflows/graalvm-test.yaml b/.github/workflows/graalvm-test.yaml
index 21edda9..08b0246 100644
--- a/.github/workflows/graalvm-test.yaml
+++ b/.github/workflows/graalvm-test.yaml
@@ -30,7 +30,7 @@ jobs:
     strategy:
       fail-fast: false
       matrix:
-        backend: [ jul, log4j-core-minimal, log4j-core, logback, simple]
+        backend: [ jul, log4j-core-minimal, log4j-core-jtl, log4j-core, logback, simple]
         log4j-version: [ "2.25.0-SNAPSHOT" ]
     uses: apache/logging-log4j-samples/.github/workflows/graalvm-reusable-test.yaml@main
     with:
diff --git a/log4j-samples-graalvm/README.adoc b/log4j-samples-graalvm/README.adoc
index 9a922fc..59c44f2 100644
--- a/log4j-samples-graalvm/README.adoc
+++ b/log4j-samples-graalvm/README.adoc
@@ -35,6 +35,9 @@ where `<backend>` can be:
 `log4j-core`::
 This profile uses Log4j Core as logging backend with its out-of-the-box reachability metadata.
 
+`log4j-core-jtl`::
+This profile uses Log4j Core as logging backend with the addition of JSON Template Layout.
+
 `log4j-core-minimal`::
 This profile uses Log4j Core a logging backend with an external reachability metadata source optimized for size.
 
@@ -111,6 +114,9 @@ In a typical application, those classes are already required, so the difference
 | 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
 
diff --git a/log4j-samples-graalvm/pom.xml b/log4j-samples-graalvm/pom.xml
index 02c90c7..a28d955 100644
--- a/log4j-samples-graalvm/pom.xml
+++ b/log4j-samples-graalvm/pom.xml
@@ -42,6 +42,8 @@
 
     <!-- Dependencies (alphabetical) -->
     <assertj.version>3.26.3</assertj.version>
+    <json-org.version>20240303</json-org.version>
+    <json-unit.version>3.4.1</json-unit.version>
     <junit.version>5.11.1</junit.version>
     <log4j.version>2.25.0-SNAPSHOT</log4j.version>
     <logback.version>1.5.8</logback.version>
@@ -80,8 +82,10 @@
 
       <dependency>
         <groupId>org.slf4j</groupId>
-        <artifactId>slf4j-api</artifactId>
+        <artifactId>slf4j-bom</artifactId>
         <version>${slf4j.version}</version>
+        <type>pom</type>
+        <scope>import</scope>
       </dependency>
 
       <dependency>
@@ -106,6 +110,19 @@
       <scope>test</scope>
     </dependency>
 
+    <dependency>
+      <groupId>org.json</groupId>
+      <artifactId>json</artifactId>
+      <version>${json-org.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>net.javacrumbs.json-unit</groupId>
+      <artifactId>json-unit-assertj</artifactId>
+      <version>${json-unit.version}</version>
+      <scope>test</scope>
+    </dependency>
+
     <dependency>
       <groupId>org.junit.jupiter</groupId>
       <artifactId>junit-jupiter-api</artifactId>
@@ -351,6 +368,62 @@
       </dependencies>
     </profile>
 
+    <!--
+  ~ Enable this profile to use Log4j Core + JSON Template Layout
+  -->
+    <profile>
+      <id>use-log4j-core-jtl</id>
+
+      <properties>
+        <integration-tests-include>**/JsonTemplateLayoutIT.class</integration-tests-include>
+      </properties>
+
+      <dependencies>
+
+        <dependency>
+          <groupId>org.apache.logging.log4j</groupId>
+          <artifactId>log4j-core</artifactId>
+          <scope>runtime</scope>
+        </dependency>
+
+        <dependency>
+          <groupId>org.apache.logging.log4j</groupId>
+          <artifactId>log4j-layout-template-json</artifactId>
+          <scope>runtime</scope>
+        </dependency>
+
+        <dependency>
+          <groupId>org.slf4j</groupId>
+          <artifactId>slf4j-nop</artifactId>
+          <scope>test</scope>
+        </dependency>
+
+      </dependencies>
+
+      <build>
+        <plugins>
+          <!--
+            ~ The JSON Template Layout configuration is in `log4j2-jtl`.
+            -->
+          <plugin>
+            <groupId>org.codehaus.mojo</groupId>
+            <artifactId>exec-maven-plugin</artifactId>
+            <version>3.4.1</version>
+            <executions>
+              <execution>
+                <id>run-executable</id>
+                <configuration>
+                  <arguments>
+                    <argument>-Dlog4j2.configurationFile=log4j2-jtl.xml</argument>
+                  </arguments>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+        </plugins>
+      </build>
+    </profile>
+
     <!--
       ~ Enable this profile to use Logback as logging implementation.
       -->
diff --git a/log4j-samples-graalvm/src/main/resources/log4j2-jtl.xml b/log4j-samples-graalvm/src/main/resources/log4j2-jtl.xml
new file mode 100644
index 0000000..b313231
--- /dev/null
+++ b/log4j-samples-graalvm/src/main/resources/log4j2-jtl.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one or more
+  ~ contributor license agreements.  See the NOTICE file distributed with
+  ~ this work for additional information regarding copyright ownership.
+  ~ The ASF licenses this file to you under the Apache License, Version 2.0
+  ~ (the "License"); you may not use this file except in compliance with
+  ~ the License.  You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<Configuration xmlns="https://logging.apache.org/xml/ns"
+               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-config-2.xsd">
+  <Appenders>
+    <File name="FILE"
+          fileName="target/logs/file.log">
+      <JsonTemplateLayout/>
+    </File>
+  </Appenders>
+  <Loggers>
+    <Root level="TRACE">
+      <AppenderRef ref="FILE"/>
+    </Root>
+  </Loggers>
+</Configuration>
diff --git a/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/StandardIT.java b/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/JsonTemplateLayoutIT.java
similarity index 61%
copy from log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/StandardIT.java
copy to log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/JsonTemplateLayoutIT.java
index f797413..48a05c1 100644
--- a/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/StandardIT.java
+++ b/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/JsonTemplateLayoutIT.java
@@ -16,6 +16,7 @@
  */
 package org.apache.logging.log4j.samples.graalvm;
 
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.IOException;
@@ -23,14 +24,14 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.List;
+import java.util.Map;
 import java.util.stream.Stream;
 import org.junit.jupiter.api.Test;
 
 /**
- * Integration test to run for configurable implementations that can include the thread's name.
+ * Integration test to run for a JSON Template Layout output.
  */
-public class StandardIT {
+class JsonTemplateLayoutIT {
 
     static final String[] STANDARD_LEVELS = {"ERROR", "WARN", "INFO", "DEBUG", "TRACE"};
 
@@ -49,12 +50,26 @@ public class StandardIT {
 
         assertThat(logFile).exists().isNotEmptyFile();
 
-        final List<String> expected = Stream.of(STANDARD_LEVELS)
-                .map(l -> String.format("[main] %s %s - Hello %s!", l, Main.class.getName(), l))
-                .toList();
-
         try (final Stream<String> stream = Files.lines(logFile, StandardCharsets.UTF_8)) {
-            assertThat(stream).containsExactlyElementsOf(expected);
+            String[] lines = stream.toArray(String[]::new);
+            assertThat(lines).hasSize(STANDARD_LEVELS.length);
+            for (int i = 0; i < STANDARD_LEVELS.length; i++) {
+                String expectedLevel = STANDARD_LEVELS[i];
+                assertThatJson(lines[i])
+                        .as("Message nr %d", i + 1)
+                        .whenIgnoringPaths("$['@timestamp']")
+                        .isEqualTo(Map.of(
+                                "ecs.version",
+                                "1.2.0",
+                                "log.level",
+                                expectedLevel,
+                                "message",
+                                String.format("Hello %s!", expectedLevel),
+                                "process.thread.name",
+                                "main",
+                                "log.logger",
+                                Main.class.getName()));
+            }
         }
     }
 }
diff --git a/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/JulIT.java b/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/JulIT.java
index 129573e..f7e72f3 100644
--- a/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/JulIT.java
+++ b/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/JulIT.java
@@ -32,7 +32,7 @@ import org.junit.jupiter.api.Test;
 /**
  * Integration test for JUL.
  */
-public class JulIT {
+class JulIT {
 
     private static final String[] JUL_LEVELS = {"SEVERE", "WARNING", "INFO", "FINE", "FINER"};
 
diff --git a/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/SimpleIT.java b/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/SimpleIT.java
index 5a5ee65..16d6258 100644
--- a/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/SimpleIT.java
+++ b/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/SimpleIT.java
@@ -30,7 +30,7 @@ import org.junit.jupiter.api.Test;
 /**
  * Integration test to run for the SimpleLogger implementation.
  */
-public class SimpleIT {
+class SimpleIT {
 
     static final String[] STANDARD_LEVELS = {"ERROR", "WARN", "INFO", "DEBUG", "TRACE"};
 
diff --git a/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/StandardIT.java b/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/StandardIT.java
index f797413..ccb5744 100644
--- a/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/StandardIT.java
+++ b/log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/StandardIT.java
@@ -30,7 +30,7 @@ import org.junit.jupiter.api.Test;
 /**
  * Integration test to run for configurable implementations that can include the thread's name.
  */
-public class StandardIT {
+class StandardIT {
 
     static final String[] STANDARD_LEVELS = {"ERROR", "WARN", "INFO", "DEBUG", "TRACE"};