|
| 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 | +} |
0 commit comments