|
| 1 | +package software.amazon.lambda.powertools.logging.log4j; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.contentOf; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.channels.FileChannel; |
| 9 | +import java.nio.file.NoSuchFileException; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.nio.file.StandardOpenOption; |
| 12 | + |
| 13 | +import org.apache.logging.log4j.LogManager; |
| 14 | +import org.apache.logging.log4j.Logger; |
| 15 | +import org.junit.jupiter.api.AfterEach; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junitpioneer.jupiter.ClearEnvironmentVariable; |
| 19 | + |
| 20 | +class BufferingAppenderTest { |
| 21 | + |
| 22 | + private Logger logger; |
| 23 | + |
| 24 | + @BeforeEach |
| 25 | + void setUp() throws IOException { |
| 26 | + logger = LogManager.getLogger(BufferingAppenderTest.class); |
| 27 | + |
| 28 | + try { |
| 29 | + FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close(); |
| 30 | + } catch (NoSuchFileException e) { |
| 31 | + // may not be there in the first run |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @AfterEach |
| 36 | + void cleanUp() throws IOException { |
| 37 | + FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void shouldBufferDebugLogsAndFlushOnError() { |
| 42 | + // When - log debug messages (should be buffered) |
| 43 | + logger.debug("Debug message 1"); |
| 44 | + logger.debug("Debug message 2"); |
| 45 | + |
| 46 | + // Then - no logs written yet |
| 47 | + File logFile = new File("target/logfile.json"); |
| 48 | + assertThat(contentOf(logFile)).isEmpty(); |
| 49 | + |
| 50 | + // When - log error (should flush buffer) |
| 51 | + logger.error("Error message"); |
| 52 | + |
| 53 | + // Then - all logs written |
| 54 | + assertThat(contentOf(logFile)) |
| 55 | + .contains("Debug message 1") |
| 56 | + .contains("Debug message 2") |
| 57 | + .contains("Error message"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + @ClearEnvironmentVariable(key = "_X_AMZN_TRACE_ID") |
| 62 | + void shouldLogDirectlyWhenNoTraceId() { |
| 63 | + // When |
| 64 | + logger.debug("Debug without trace"); |
| 65 | + |
| 66 | + // Then - log written directly |
| 67 | + File logFile = new File("target/logfile.json"); |
| 68 | + assertThat(contentOf(logFile)).contains("Debug without trace"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void shouldNotBufferInfoLogs() { |
| 73 | + // When - log info message (above buffer level) |
| 74 | + logger.info("Info message"); |
| 75 | + |
| 76 | + // Then - log written directly |
| 77 | + File logFile = new File("target/logfile.json"); |
| 78 | + assertThat(contentOf(logFile)).contains("Info message"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void shouldFlushBufferManually() { |
| 83 | + // When - buffer debug logs |
| 84 | + logger.debug("Buffered message"); |
| 85 | + File logFile = new File("target/logfile.json"); |
| 86 | + assertThat(contentOf(logFile)).isEmpty(); |
| 87 | + |
| 88 | + // When - manual flush |
| 89 | + BufferingAppender appender = getBufferingAppender(); |
| 90 | + appender.flushBuffer(); |
| 91 | + |
| 92 | + // Then - logs written |
| 93 | + assertThat(contentOf(logFile)).contains("Buffered message"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void shouldClearBufferManually() { |
| 98 | + // When - buffer debug logs then clear |
| 99 | + logger.debug("Buffered message"); |
| 100 | + BufferingAppender appender = getBufferingAppender(); |
| 101 | + appender.clearBuffer(); |
| 102 | + |
| 103 | + // When - log error (should not flush cleared buffer) |
| 104 | + logger.error("Error after clear"); |
| 105 | + |
| 106 | + // Then - only error logged |
| 107 | + File logFile = new File("target/logfile.json"); |
| 108 | + assertThat(contentOf(logFile)) |
| 109 | + .contains("Error after clear") |
| 110 | + .doesNotContain("Buffered message"); |
| 111 | + } |
| 112 | + |
| 113 | + private BufferingAppender getBufferingAppender() { |
| 114 | + return (BufferingAppender) ((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false)) |
| 115 | + .getConfiguration().getAppender(BufferingAppenderConstants.NAME); |
| 116 | + } |
| 117 | +} |
0 commit comments