Skip to content

Commit d662d6d

Browse files
committed
Add JTL profile to GraalVM test
1 parent 596675d commit d662d6d

File tree

8 files changed

+191
-5
lines changed

8 files changed

+191
-5
lines changed

.github/workflows/graalvm-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
strategy:
3131
fail-fast: false
3232
matrix:
33-
backend: [ jul, log4j-core-minimal, log4j-core, logback, simple]
33+
backend: [ jul, log4j-core-minimal, log4j-core-jtl, log4j-core, logback, simple]
3434
log4j-version: [ "2.25.0-SNAPSHOT" ]
3535
uses: apache/logging-log4j-samples/.github/workflows/graalvm-reusable-test.yaml@main
3636
with:

log4j-samples-graalvm/README.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ where `<backend>` can be:
3535
`log4j-core`::
3636
This profile uses Log4j Core as logging backend with its out-of-the-box reachability metadata.
3737
38+
`log4j-core-jtl`::
39+
This profile uses Log4j Core as logging backend with the addition of JSON Template Layout.
40+
3841
`log4j-core-minimal`::
3942
This profile uses Log4j Core a logging backend with an external reachability metadata source optimized for size.
4043
@@ -111,6 +114,9 @@ In a typical application, those classes are already required, so the difference
111114
| With Log4j Core (embedded full reachability metadata)
112115
| 46.80 MiB
113116
117+
| With Log4j Core + JSON Template Layout (embedded full reachability metadata)
118+
| 47.37 MiB
119+
114120
| With Logback
115121
| 30.62 MiB
116122

log4j-samples-graalvm/pom.xml

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
<!-- Dependencies (alphabetical) -->
4444
<assertj.version>3.26.3</assertj.version>
45+
<json-org.version>20240303</json-org.version>
46+
<json-unit.version>3.4.1</json-unit.version>
4547
<junit.version>5.11.1</junit.version>
4648
<log4j.version>2.25.0-SNAPSHOT</log4j.version>
4749
<logback.version>1.5.8</logback.version>
@@ -80,8 +82,10 @@
8082

8183
<dependency>
8284
<groupId>org.slf4j</groupId>
83-
<artifactId>slf4j-api</artifactId>
85+
<artifactId>slf4j-bom</artifactId>
8486
<version>${slf4j.version}</version>
87+
<type>pom</type>
88+
<scope>import</scope>
8589
</dependency>
8690

8791
<dependency>
@@ -106,6 +110,19 @@
106110
<scope>test</scope>
107111
</dependency>
108112

113+
<dependency>
114+
<groupId>org.json</groupId>
115+
<artifactId>json</artifactId>
116+
<version>${json-org.version}</version>
117+
</dependency>
118+
119+
<dependency>
120+
<groupId>net.javacrumbs.json-unit</groupId>
121+
<artifactId>json-unit-assertj</artifactId>
122+
<version>${json-unit.version}</version>
123+
<scope>test</scope>
124+
</dependency>
125+
109126
<dependency>
110127
<groupId>org.junit.jupiter</groupId>
111128
<artifactId>junit-jupiter-api</artifactId>
@@ -351,6 +368,62 @@
351368
</dependencies>
352369
</profile>
353370

371+
<!--
372+
~ Enable this profile to use Log4j Core + JSON Template Layout
373+
-->
374+
<profile>
375+
<id>use-log4j-core-jtl</id>
376+
377+
<properties>
378+
<integration-tests-include>**/JsonTemplateLayoutIT.class</integration-tests-include>
379+
</properties>
380+
381+
<dependencies>
382+
383+
<dependency>
384+
<groupId>org.apache.logging.log4j</groupId>
385+
<artifactId>log4j-core</artifactId>
386+
<scope>runtime</scope>
387+
</dependency>
388+
389+
<dependency>
390+
<groupId>org.apache.logging.log4j</groupId>
391+
<artifactId>log4j-layout-template-json</artifactId>
392+
<scope>runtime</scope>
393+
</dependency>
394+
395+
<dependency>
396+
<groupId>org.slf4j</groupId>
397+
<artifactId>slf4j-nop</artifactId>
398+
<scope>test</scope>
399+
</dependency>
400+
401+
</dependencies>
402+
403+
<build>
404+
<plugins>
405+
<!--
406+
~ The JSON Template Layout configuration is in `log4j2-jtl`.
407+
-->
408+
<plugin>
409+
<groupId>org.codehaus.mojo</groupId>
410+
<artifactId>exec-maven-plugin</artifactId>
411+
<version>3.4.1</version>
412+
<executions>
413+
<execution>
414+
<id>run-executable</id>
415+
<configuration>
416+
<arguments>
417+
<argument>-Dlog4j2.configurationFile=log4j2-jtl.xml</argument>
418+
</arguments>
419+
</configuration>
420+
</execution>
421+
</executions>
422+
</plugin>
423+
</plugins>
424+
</build>
425+
</profile>
426+
354427
<!--
355428
~ Enable this profile to use Logback as logging implementation.
356429
-->
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration xmlns="https://logging.apache.org/xml/ns"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-config-2.xsd">
21+
<Appenders>
22+
<File name="FILE"
23+
fileName="target/logs/file.log">
24+
<JsonTemplateLayout/>
25+
</File>
26+
</Appenders>
27+
<Loggers>
28+
<Root level="TRACE">
29+
<AppenderRef ref="FILE"/>
30+
</Root>
31+
</Loggers>
32+
</Configuration>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.samples.graalvm;
18+
19+
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
import java.io.IOException;
23+
import java.nio.charset.StandardCharsets;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
26+
import java.nio.file.Paths;
27+
import java.util.Map;
28+
import java.util.stream.Stream;
29+
import org.junit.jupiter.api.Test;
30+
31+
/**
32+
* Integration test to run for a JSON Template Layout output.
33+
*/
34+
class JsonTemplateLayoutIT {
35+
36+
static final String[] STANDARD_LEVELS = {"ERROR", "WARN", "INFO", "DEBUG", "TRACE"};
37+
38+
@Test
39+
void verifyStdOut() {
40+
final Path basePath = Paths.get(System.getProperty("basedir"), "target", "logs");
41+
final Path logFile = basePath.resolve("out.log");
42+
43+
assertThat(logFile).exists().isEmptyFile();
44+
}
45+
46+
@Test
47+
void verifyLogFile() throws IOException {
48+
final Path basePath = Paths.get(System.getProperty("basedir"), "target", "logs");
49+
final Path logFile = basePath.resolve("file.log");
50+
51+
assertThat(logFile).exists().isNotEmptyFile();
52+
53+
try (final Stream<String> stream = Files.lines(logFile, StandardCharsets.UTF_8)) {
54+
String[] lines = stream.toArray(String[]::new);
55+
assertThat(lines).hasSize(STANDARD_LEVELS.length);
56+
for (int i = 0; i < STANDARD_LEVELS.length; i++) {
57+
String expectedLevel = STANDARD_LEVELS[i];
58+
assertThatJson(lines[i])
59+
.as("Message nr %d", i + 1)
60+
.whenIgnoringPaths("$['@timestamp']")
61+
.isEqualTo(Map.of(
62+
"ecs.version",
63+
"1.2.0",
64+
"log.level",
65+
expectedLevel,
66+
"message",
67+
String.format("Hello %s!", expectedLevel),
68+
"process.thread.name",
69+
"main",
70+
"log.logger",
71+
Main.class.getName()));
72+
}
73+
}
74+
}
75+
}

log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/JulIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
/**
3333
* Integration test for JUL.
3434
*/
35-
public class JulIT {
35+
class JulIT {
3636

3737
private static final String[] JUL_LEVELS = {"SEVERE", "WARNING", "INFO", "FINE", "FINER"};
3838

log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/SimpleIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/**
3131
* Integration test to run for the SimpleLogger implementation.
3232
*/
33-
public class SimpleIT {
33+
class SimpleIT {
3434

3535
static final String[] STANDARD_LEVELS = {"ERROR", "WARN", "INFO", "DEBUG", "TRACE"};
3636

log4j-samples-graalvm/src/test/java/org/apache/logging/log4j/samples/graalvm/StandardIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/**
3131
* Integration test to run for configurable implementations that can include the thread's name.
3232
*/
33-
public class StandardIT {
33+
class StandardIT {
3434

3535
static final String[] STANDARD_LEVELS = {"ERROR", "WARN", "INFO", "DEBUG", "TRACE"};
3636

0 commit comments

Comments
 (0)