Skip to content

Commit 4e3b889

Browse files
committed
test(log4j2): Add initial test suite covering plugin registration
This package previously had no tests and no JUnit dependency. Add JUnit Jupiter, the surefire plugin, and an initial test class that exercises the full plugin resolution path end-to-end: a log4j2.xml on the test classpath, a real LogManager-issued logger, and stdout capture (LambdaAppender writes through LambdaRuntime.getLogger() to System.out). The TEXT test routes the root logger through LambdaTextFormat with a deterministic PatternLayout and asserts each level appears. The JSON test adds a second LambdaAppender with format="JSON" backed by JsonTemplateLayout + LambdaLayout.json, attached via additivity=false to a "json-test" logger, and asserts the messages show up JSON-encoded. Add log4j-layout-template-json at test scope so the JSON path can resolve at test time. Users are still expected to bring their own copy at runtime (peer-dependency model, like PatternLayout); the published artifact's dependency surface is unchanged. The tests succeed on Java 8 and fail on Java 25, because annotation processors are not run by default on Java 25, Log4j2Plugins.dat is not generated for our plugins, and both tests surface the resulting CLASS_NOT_FOUND from the log4j status logger.
1 parent 8adb3d7 commit 4e3b889

3 files changed

Lines changed: 135 additions & 1 deletion

File tree

aws-lambda-java-log4j2/pom.xml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<maven.compiler.source>1.8</maven.compiler.source>
3636
<maven.compiler.target>1.8</maven.compiler.target>
3737
<log4j.version>2.25.4</log4j.version>
38+
<junit-jupiter.version>5.12.2</junit-jupiter.version>
3839
</properties>
3940

4041
<distributionManagement>
@@ -60,8 +61,30 @@
6061
<artifactId>log4j-api</artifactId>
6162
<version>${log4j.version}</version>
6263
</dependency>
64+
<dependency>
65+
<groupId>org.apache.logging.log4j</groupId>
66+
<artifactId>log4j-layout-template-json</artifactId>
67+
<version>${log4j.version}</version>
68+
<scope>test</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.junit.jupiter</groupId>
72+
<artifactId>junit-jupiter-engine</artifactId>
73+
<version>${junit-jupiter.version}</version>
74+
<scope>test</scope>
75+
</dependency>
6376
</dependencies>
6477

78+
<build>
79+
<plugins>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-surefire-plugin</artifactId>
83+
<version>3.5.2</version>
84+
</plugin>
85+
</plugins>
86+
</build>
87+
6588
<profiles>
6689
<profile>
6790
<id>dev</id>
@@ -146,4 +169,4 @@
146169
</build>
147170
</profile>
148171
</profiles>
149-
</project>
172+
</project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
2+
3+
package com.amazonaws.services.lambda.runtime.log4j2;
4+
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
7+
import org.junit.jupiter.api.AfterEach;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.io.ByteArrayOutputStream;
12+
import java.io.PrintStream;
13+
import java.io.UnsupportedEncodingException;
14+
import java.nio.charset.StandardCharsets;
15+
16+
import static org.junit.jupiter.api.Assertions.assertFalse;
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
18+
19+
public class LambdaAppenderPluginTest {
20+
21+
private final PrintStream originalOut = System.out;
22+
private ByteArrayOutputStream captured;
23+
24+
@BeforeEach
25+
void redirectStdout() throws UnsupportedEncodingException {
26+
captured = new ByteArrayOutputStream();
27+
System.setOut(new PrintStream(captured, true, StandardCharsets.UTF_8.name()));
28+
}
29+
30+
@AfterEach
31+
void restoreStdout() {
32+
System.setOut(originalOut);
33+
}
34+
35+
@Test
36+
void lambdaAppenderEmitsLogsAtVariousLevels() throws UnsupportedEncodingException {
37+
Logger logger = LogManager.getLogger(LambdaAppenderPluginTest.class);
38+
39+
logger.debug("debug-msg");
40+
logger.info("info-msg");
41+
logger.warn("warn-msg");
42+
logger.error("error-msg");
43+
44+
String output = captured.toString(StandardCharsets.UTF_8.name());
45+
46+
// The PatternLayout in src/test/resources/log4j2.xml is "%-5p %c{1} - %m%n",
47+
// so each event should appear as "<LEVEL> LambdaAppenderPluginTest - <message>".
48+
assertTrue(output.contains("DEBUG LambdaAppenderPluginTest - debug-msg"),
49+
"expected DEBUG line in output but got:\n" + output);
50+
assertTrue(output.contains("INFO LambdaAppenderPluginTest - info-msg"),
51+
"expected INFO line in output but got:\n" + output);
52+
assertTrue(output.contains("WARN LambdaAppenderPluginTest - warn-msg"),
53+
"expected WARN line in output but got:\n" + output);
54+
assertTrue(output.contains("ERROR LambdaAppenderPluginTest - error-msg"),
55+
"expected ERROR line in output but got:\n" + output);
56+
57+
// Sanity check: log4j should not have fallen back to its default
58+
// ConsoleAppender / status logger error message.
59+
assertFalse(output.contains("ERROR StatusLogger"),
60+
"log4j status logger reported an error, output was:\n" + output);
61+
}
62+
63+
@Test
64+
void lambdaAppenderEmitsJsonForJsonFormatLogger() throws UnsupportedEncodingException {
65+
// The "json-test" logger is configured in src/test/resources/log4j2.xml
66+
// with additivity=false to a second LambdaAppender using format="JSON"
67+
// and JsonTemplateLayout backed by LambdaLayout.json.
68+
Logger logger = LogManager.getLogger("json-test");
69+
70+
logger.info("json-info-msg");
71+
logger.error("json-error-msg");
72+
73+
String output = captured.toString(StandardCharsets.UTF_8.name());
74+
75+
assertTrue(output.contains("json-info-msg"),
76+
"expected json-info-msg in output but got:\n" + output);
77+
assertTrue(output.contains("json-error-msg"),
78+
"expected json-error-msg in output but got:\n" + output);
79+
80+
// Output should look like JSON, not the text PatternLayout from the
81+
// root logger — so it must contain JSON field punctuation around the
82+
// message rather than the "INFO json-test - ..." text pattern.
83+
assertTrue(output.contains("\"message\":\"json-info-msg\""),
84+
"expected JSON-encoded message field but got:\n" + output);
85+
}
86+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN">
3+
<Appenders>
4+
<Lambda name="LambdaText" format="TEXT">
5+
<LambdaTextFormat>
6+
<PatternLayout>
7+
<pattern>%-5p %c{1} - %m%n</pattern>
8+
</PatternLayout>
9+
</LambdaTextFormat>
10+
</Lambda>
11+
<Lambda name="LambdaJson" format="JSON">
12+
<LambdaJsonFormat>
13+
<JsonTemplateLayout eventTemplateUri="classpath:LambdaLayout.json" />
14+
</LambdaJsonFormat>
15+
</Lambda>
16+
</Appenders>
17+
<Loggers>
18+
<Logger name="json-test" level="DEBUG" additivity="false">
19+
<AppenderRef ref="LambdaJson" />
20+
</Logger>
21+
<Root level="DEBUG">
22+
<AppenderRef ref="LambdaText" />
23+
</Root>
24+
</Loggers>
25+
</Configuration>

0 commit comments

Comments
 (0)