Skip to content

Commit 6084cdd

Browse files
committed
add support for environment variable POWERTOOLS_LOGGER_LOG_EVENT
1 parent 51354b9 commit 6084cdd

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

powertools-logging/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@
8989
<artifactId>junit-jupiter-engine</artifactId>
9090
<scope>test</scope>
9191
</dependency>
92-
<dependency>
93-
<groupId>org.junit-pioneer</groupId>
94-
<artifactId>junit-pioneer</artifactId>
95-
<scope>test</scope>
96-
</dependency>
9792
<dependency>
9893
<groupId>org.apache.commons</groupId>
9994
<artifactId>commons-lang3</artifactId>

powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/internal/LambdaLoggingAspect.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.slf4j.LoggerFactory;
5757
import org.slf4j.MDC;
5858
import org.slf4j.event.Level;
59+
import software.amazon.lambda.powertools.common.internal.SystemWrapper;
5960
import software.amazon.lambda.powertools.logging.Logging;
6061
import software.amazon.lambda.powertools.logging.LoggingUtils;
6162
import software.amazon.lambda.powertools.utilities.JsonConfig;
@@ -186,7 +187,7 @@ public Object around(ProceedingJoinPoint pjp,
186187

187188
getXrayTraceId().ifPresent(xRayTraceId -> appendKey(FUNCTION_TRACE_ID.getName(), xRayTraceId));
188189

189-
if (logging.logEvent()) {
190+
if (logging.logEvent() || "true".equals(SystemWrapper.getenv("POWERTOOLS_LOGGER_LOG_EVENT"))) {
190191
proceedArgs = logEvent(pjp);
191192
}
192193

@@ -236,7 +237,7 @@ private void setLogLevelBasedOnSamplingRate(final ProceedingJoinPoint pjp,
236237
}
237238

238239
private double samplingRate(final Logging logging) {
239-
String sampleRate = System.getenv("POWERTOOLS_LOGGER_SAMPLE_RATE");
240+
String sampleRate = SystemWrapper.getenv("POWERTOOLS_LOGGER_SAMPLE_RATE");
240241
if (null != sampleRate) {
241242
try {
242243
return Double.parseDouble(sampleRate);

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/internal/LambdaLoggingAspectTest.java

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@
4646
import java.lang.reflect.Method;
4747
import java.nio.channels.FileChannel;
4848
import java.nio.charset.StandardCharsets;
49+
import java.nio.file.NoSuchFileException;
4950
import java.nio.file.Paths;
5051
import java.nio.file.StandardOpenOption;
5152
import java.util.Collections;
5253
import org.junit.jupiter.api.AfterEach;
5354
import org.junit.jupiter.api.BeforeEach;
5455
import org.junit.jupiter.api.Test;
5556
import org.junit.jupiter.params.ParameterizedTest;
56-
import org.junitpioneer.jupiter.ClearEnvironmentVariable;
57-
import org.junitpioneer.jupiter.SetEnvironmentVariable;
5857
import org.mockito.Mock;
5958
import org.mockito.MockedStatic;
6059
import org.slf4j.MDC;
@@ -86,15 +85,19 @@ class LambdaLoggingAspectTest {
8685
private Context context;
8786

8887
@BeforeEach
89-
@ClearEnvironmentVariable(key = "POWERTOOLS_LOGGER_SAMPLE_RATE")
90-
void setUp() throws IllegalAccessException, IOException, NoSuchMethodException, InvocationTargetException {
88+
void setUp() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException {
9189
openMocks(this);
9290
MDC.clear();
9391
writeStaticField(LambdaHandlerProcessor.class, "IS_COLD_START", null, true);
9492
setupContext();
9593
requestHandler = new PowertoolsLogEnabled();
9694
requestStreamHandler = new PowertoolsLogEnabledForStream();
9795
resetLogLevel(Level.INFO);
96+
try {
97+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
98+
} catch (NoSuchFileException e) {
99+
// may not be there in the first run
100+
}
98101
}
99102

100103
@AfterEach
@@ -188,29 +191,42 @@ void shouldLogDebugWhenSamplingEqualsOne() {
188191
}
189192

190193
@Test
191-
@SetEnvironmentVariable(key = "POWERTOOLS_LOGGER_SAMPLE_RATE", value = "1")
192194
void shouldLogDebugWhenSamplingEnvVarEqualsOne() {
193-
PowertoolsLogEnabled handler = new PowertoolsLogEnabled();
194-
handler.handleRequest(new Object(), context);
195-
File logFile = new File("target/logfile.json");
196-
assertThat(contentOf(logFile)).contains("Test debug event");
195+
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) {
196+
mocked.when(() -> getenv("POWERTOOLS_LOGGER_SAMPLE_RATE"))
197+
.thenReturn("1");
198+
199+
PowertoolsLogEnabled handler = new PowertoolsLogEnabled();
200+
handler.handleRequest(new Object(), context);
201+
File logFile = new File("target/logfile.json");
202+
assertThat(contentOf(logFile)).contains("Test debug event");
203+
}
197204
}
198205

199206
@Test
200-
@SetEnvironmentVariable(key = "POWERTOOLS_LOGGER_SAMPLE_RATE", value = "42")
201207
void shouldNotLogDebugWhenSamplingEnvVarIsTooBig() {
202-
requestHandler.handleRequest(new Object(), context);
203-
File logFile = new File("target/logfile.json");
204-
assertThat(contentOf(logFile)).doesNotContain("Test debug event");
208+
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) {
209+
mocked.when(() -> getenv("POWERTOOLS_LOGGER_SAMPLE_RATE"))
210+
.thenReturn("42");
211+
212+
requestHandler.handleRequest(new Object(), context);
213+
File logFile = new File("target/logfile.json");
214+
assertThat(contentOf(logFile)).doesNotContain("Test debug event");
215+
}
205216
}
206217

207218
@Test
208-
@SetEnvironmentVariable(key = "POWERTOOLS_LOGGER_SAMPLE_RATE", value = "NotANumber")
209219
void shouldNotLogDebugWhenSamplingEnvVarIsInvalid() {
210-
requestHandler.handleRequest(new Object(), context);
211-
File logFile = new File("target/logfile.json");
212-
assertThat(contentOf(logFile)).doesNotContain("Test debug event");
213-
assertThat(contentOf(logFile)).contains("Skipping sampling rate on environment variable configuration because of invalid value");
220+
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) {
221+
mocked.when(() -> getenv("POWERTOOLS_LOGGER_SAMPLE_RATE"))
222+
.thenReturn("NotANumber");
223+
224+
requestHandler.handleRequest(new Object(), context);
225+
File logFile = new File("target/logfile.json");
226+
assertThat(contentOf(logFile)).doesNotContain("Test debug event");
227+
assertThat(contentOf(logFile)).contains(
228+
"Skipping sampling rate on environment variable configuration because of invalid value");
229+
}
214230
}
215231

216232
@Test
@@ -256,7 +272,7 @@ void shouldLogxRayTraceIdEnvVarSet() {
256272
}
257273

258274
@Test
259-
void shouldLogEventForHandler() throws IOException {
275+
void shouldLogEventForHandlerWithLogEventAnnotation() {
260276
requestHandler = new PowertoolsLogEvent();
261277

262278
requestHandler.handleRequest(Collections.singletonList("ListOfOneElement"), context);
@@ -265,6 +281,21 @@ void shouldLogEventForHandler() throws IOException {
265281
assertThat(contentOf(logFile)).contains("[\"ListOfOneElement\"]");
266282
}
267283

284+
@Test
285+
void shouldLogEventForHandlerWithLogEventEnvVar() {
286+
requestHandler = new PowertoolsLogEnabled();
287+
288+
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) {
289+
mocked.when(() -> getenv("POWERTOOLS_LOGGER_LOG_EVENT"))
290+
.thenReturn("true");
291+
292+
requestHandler.handleRequest(Collections.singletonList("ListOfOneElement"), context);
293+
294+
File logFile = new File("target/logfile.json");
295+
assertThat(contentOf(logFile)).contains("[\"ListOfOneElement\"]");
296+
}
297+
}
298+
268299
@Test
269300
void shouldLogEventForStreamHandler() throws IOException {
270301
requestStreamHandler = new PowertoolsLogEventForStream();

0 commit comments

Comments
 (0)