Skip to content

Commit 4488716

Browse files
committed
complete tests for log4j
1 parent d600fd8 commit 4488716

File tree

12 files changed

+226
-64
lines changed

12 files changed

+226
-64
lines changed

powertools-logging/powertools-logging-log4j/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@
156156
</dependency>
157157
</dependencies>
158158
</plugin>
159+
<plugin>
160+
<artifactId>maven-surefire-plugin</artifactId>
161+
<configuration>
162+
<systemPropertyVariables>
163+
<slf4j.binding>org.apache.logging.slf4j.SLF4JServiceProvider</slf4j.binding>
164+
</systemPropertyVariables>
165+
</configuration>
166+
</plugin>
159167
<plugin>
160168
<groupId>org.apache.maven.plugins</groupId>
161169
<artifactId>maven-checkstyle-plugin</artifactId>

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,4 @@ public org.slf4j.event.Level getLogLevel(Logger logger) {
3535
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
3636
return org.slf4j.event.Level.valueOf(ctx.getLogger(logger.getName()).getLevel().toString());
3737
}
38-
39-
4038
}

powertools-logging/powertools-logging-log4j/src/main/resources/LambdaEcsLayout.json

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@
1515
"$resolver": "message",
1616
"stringified": true
1717
},
18-
"service.name": {
19-
"$resolver": "powertools",
20-
"field": "service"
21-
},
22-
"service.version": {
23-
"$resolver": "powertools",
24-
"field": "service_version"
25-
},
26-
"process.thread.name": {
27-
"$resolver": "thread",
28-
"field": "name"
29-
},
30-
"log.logger": {
31-
"$resolver": "logger",
32-
"field": "name"
33-
},
3418
"error.type": {
3519
"$resolver": "exception",
3620
"field": "className"
@@ -46,6 +30,22 @@
4630
"stringified": true
4731
}
4832
},
33+
"service.name": {
34+
"$resolver": "powertools",
35+
"field": "service"
36+
},
37+
"service.version": {
38+
"$resolver": "powertools",
39+
"field": "service_version"
40+
},
41+
"log.logger": {
42+
"$resolver": "logger",
43+
"field": "name"
44+
},
45+
"process.thread.name": {
46+
"$resolver": "thread",
47+
"field": "name"
48+
},
4949
"cloud.provider" : "aws",
5050
"cloud.service.name" : "lambda",
5151
"cloud.region" : {
@@ -56,35 +56,35 @@
5656
"$resolver": "powertools",
5757
"field": "account_id"
5858
},
59-
"faas.coldstart": {
60-
"$resolver": "powertools",
61-
"field": "cold_start"
62-
},
6359
"faas.id": {
6460
"$resolver": "powertools",
6561
"field": "function_arn"
6662
},
67-
"faas.memory": {
68-
"$resolver": "powertools",
69-
"field": "function_memory_size"
70-
},
7163
"faas.name": {
7264
"$resolver": "powertools",
7365
"field": "function_name"
7466
},
67+
"faas.version": {
68+
"$resolver": "powertools",
69+
"field": "function_version"
70+
},
71+
"faas.memory": {
72+
"$resolver": "powertools",
73+
"field": "function_memory_size"
74+
},
7575
"faas.execution": {
7676
"$resolver": "powertools",
7777
"field": "function_request_id"
7878
},
79-
"faas.version": {
79+
"faas.coldstart": {
8080
"$resolver": "powertools",
81-
"field": "function_version"
82-
},
83-
"": {
84-
"$resolver": "powertools"
81+
"field": "cold_start"
8582
},
8683
"trace.id": {
8784
"$resolver": "powertools",
8885
"field": "xray_trace_id"
86+
},
87+
"": {
88+
"$resolver": "powertools"
8989
}
9090
}

powertools-logging/powertools-logging-log4j/src/main/resources/LambdaJsonLayout.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
2-
"cold_start": {
3-
"$resolver": "powertools",
4-
"field": "cold_start"
2+
"level": {
3+
"$resolver": "level",
4+
"field": "name"
5+
},
6+
"message": {
7+
"$resolver": "message",
8+
"stringified": true
59
},
610
"error": {
711
"message": {
@@ -20,6 +24,10 @@
2024
}
2125
}
2226
},
27+
"cold_start": {
28+
"$resolver": "powertools",
29+
"field": "cold_start"
30+
},
2331
"function_arn": {
2432
"$resolver": "powertools",
2533
"field": "function_arn"
@@ -40,14 +48,6 @@
4048
"$resolver": "powertools",
4149
"field": "function_version"
4250
},
43-
"level": {
44-
"$resolver": "level",
45-
"field": "name"
46-
},
47-
"message": {
48-
"$resolver": "message",
49-
"stringified": true
50-
},
5151
"sampling_rate": {
5252
"$resolver": "powertools",
5353
"field": "sampling_rate"

powertools-logging/powertools-logging-log4j/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/PowerToolsResolverFactoryTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@
2727
import java.nio.file.NoSuchFileException;
2828
import java.nio.file.Paths;
2929
import java.nio.file.StandardOpenOption;
30+
import org.junit.jupiter.api.AfterEach;
3031
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Order;
3133
import org.junit.jupiter.api.Test;
3234
import org.junitpioneer.jupiter.SetEnvironmentVariable;
3335
import org.mockito.Mock;
3436
import org.slf4j.MDC;
3537
import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor;
3638
import software.amazon.lambda.powertools.logging.internal.handler.PowertoolsLogEnabled;
3739

40+
@Order(2)
3841
class PowerToolsResolverFactoryTest {
3942

4043
@Mock
@@ -55,32 +58,35 @@ void setUp() throws IllegalAccessException, IOException {
5558
}
5659
}
5760

61+
@AfterEach
62+
void cleanUp() throws IOException{
63+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
64+
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
65+
}
66+
5867
@Test
59-
@SetEnvironmentVariable(key = "POWERTOOLS_SERVICE_NAME", value = "testLog4j")
6068
@SetEnvironmentVariable(key = "POWERTOOLS_LOGGER_SAMPLE_RATE", value = "0.000000001")
6169
@SetEnvironmentVariable(key = "_X_AMZN_TRACE_ID", value = "Root=1-63441c4a-abcdef012345678912345678")
6270
void shouldLogInJsonFormat() {
6371
PowertoolsLogEnabled handler = new PowertoolsLogEnabled();
6472
handler.handleRequest("Input", context);
6573

6674
File logFile = new File("target/logfile.json");
67-
assertThat(contentOf(logFile)).startsWith(
68-
"{\"cold_start\":true,\"function_arn\":\"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1\",\"function_memory_size\":1024,\"function_name\":\"testFunction\",\"function_request_id\":\"RequestId\",\"function_version\":\"1\",\"level\":\"INFO\",\"message\":\"Test event\",\"sampling_rate\":1.0E-9,\"service\":\"testLog4j\",\"timestamp\":")
69-
.endsWith("\"xray_trace_id\":\"1-63441c4a-abcdef012345678912345678\",\"myKey\":\"myValue\"}\n");
75+
assertThat(contentOf(logFile)).contains(
76+
"{\"level\":\"DEBUG\",\"message\":\"Test debug event\",\"cold_start\":true,\"function_arn\":\"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1\",\"function_memory_size\":1024,\"function_name\":\"testFunction\",\"function_request_id\":\"RequestId\",\"function_version\":\"1\",\"sampling_rate\":1.0E-9,\"service\":\"testLog4j\",\"timestamp\":")
77+
.contains("\"xray_trace_id\":\"1-63441c4a-abcdef012345678912345678\",\"myKey\":\"myValue\"}\n");
7078
}
7179

7280
@Test
7381
@SetEnvironmentVariable(key = "AWS_REGION", value = "eu-central-1")
74-
@SetEnvironmentVariable(key = "POWERTOOLS_SERVICE_NAME", value = "testLog4jEcs")
7582
@SetEnvironmentVariable(key = "_X_AMZN_TRACE_ID", value = "Root=1-63441c4a-abcdef012345678912345678")
7683
void shouldLogInEcsFormat() {
7784
PowertoolsLogEnabled handler = new PowertoolsLogEnabled();
7885
handler.handleRequest("Input", context);
7986

8087
File logFile = new File("target/ecslogfile.json");
81-
assertThat(contentOf(logFile)).endsWith(
82-
"\"ecs.version\":\"1.2.0\",\"log.level\":\"INFO\",\"message\":\"Test event\",\"service.name\":\"testLog4j\",\"service.version\":\"1\",\"process.thread.name\":\"main\",\"log.logger\":\"software.amazon.lambda.powertools.logging.internal.handler.PowertoolsLogEnabled\",\"cloud.provider\":\"aws\",\"cloud.service.name\":\"lambda\",\"cloud.region\":\"eu-central-1\",\"cloud.account.id\":\"012345678910\",\"faas.coldstart\":true,\"faas.id\":\"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1\",\"faas.memory\":1024,\"faas.name\":\"testFunction\",\"faas.execution\":\"RequestId\",\"faas.version\":\"1\",\"myKey\":\"myValue\",\"trace.id\":\"1-63441c4a-abcdef012345678912345678\"}\n")
83-
.startsWith("{\"@timestamp\":\"");
88+
assertThat(contentOf(logFile)).contains(
89+
"\"ecs.version\":\"1.2.0\",\"log.level\":\"DEBUG\",\"message\":\"Test debug event\",\"service.name\":\"testLog4j\",\"service.version\":\"1\",\"log.logger\":\"software.amazon.lambda.powertools.logging.internal.handler.PowertoolsLogEnabled\",\"process.thread.name\":\"main\",\"cloud.provider\":\"aws\",\"cloud.service.name\":\"lambda\",\"cloud.region\":\"eu-central-1\",\"cloud.account.id\":\"012345678910\",\"faas.id\":\"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1\",\"faas.name\":\"testFunction\",\"faas.version\":\"1\",\"faas.memory\":1024,\"faas.execution\":\"RequestId\",\"faas.coldstart\":true,\"trace.id\":\"1-63441c4a-abcdef012345678912345678\",\"myKey\":\"myValue\"}\n");
8490
}
8591

8692
private void setupContext() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
15+
package software.amazon.lambda.powertools.logging.internal;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.assertj.core.api.Assertions.contentOf;
19+
import static org.mockito.Mockito.when;
20+
import static org.mockito.MockitoAnnotations.openMocks;
21+
22+
import com.amazonaws.services.lambda.runtime.Context;
23+
import java.io.File;
24+
import java.io.IOException;
25+
import java.nio.channels.FileChannel;
26+
import java.nio.file.NoSuchFileException;
27+
import java.nio.file.Paths;
28+
import java.nio.file.StandardOpenOption;
29+
import org.junit.jupiter.api.AfterEach;
30+
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.Order;
32+
import org.junit.jupiter.api.Test;
33+
import org.junitpioneer.jupiter.SetEnvironmentVariable;
34+
import org.mockito.Mock;
35+
import org.slf4j.MDC;
36+
import software.amazon.lambda.powertools.logging.internal.handler.PowertoolsLogEnabled;
37+
38+
@Order(1)
39+
class LambdaLoggingAspectTest {
40+
41+
@Mock
42+
private Context context;
43+
44+
@BeforeEach
45+
void setUp() throws IOException {
46+
openMocks(this);
47+
MDC.clear();
48+
setupContext();
49+
//Make sure file is cleaned up before running full stack logging regression
50+
try {
51+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
52+
} catch (NoSuchFileException e) {
53+
// file might not be there for the first run
54+
}
55+
}
56+
57+
@AfterEach
58+
void cleanUp() throws IOException {
59+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
60+
}
61+
62+
@Test
63+
@SetEnvironmentVariable(key = "POWERTOOLS_SERVICE_NAME", value = "testLog4j")
64+
void testSlf4jBinding() {
65+
PowertoolsLogEnabled handler = new PowertoolsLogEnabled();
66+
handler.handleRequest("Input", context);
67+
68+
File logFile = new File("target/logfile.json");
69+
assertThat(contentOf(logFile))
70+
.contains("slf4j.binding is set to org.apache.logging.slf4j.SLF4JServiceProvider")
71+
.contains("Loading software.amazon.lambda.powertools.logging.internal.Log4jLoggingManager");
72+
}
73+
74+
private void setupContext() {
75+
when(context.getFunctionName()).thenReturn("testFunction");
76+
when(context.getInvokedFunctionArn()).thenReturn(
77+
"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1");
78+
when(context.getFunctionVersion()).thenReturn("1");
79+
when(context.getMemoryLimitInMB()).thenReturn(1024);
80+
when(context.getAwsRequestId()).thenReturn("RequestId");
81+
}
82+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package software.amazon.lambda.powertools.logging.internal;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.slf4j.event.Level.DEBUG;
45
import static org.slf4j.event.Level.ERROR;
5-
import static org.slf4j.event.Level.INFO;
66
import static org.slf4j.event.Level.WARN;
77

88
import org.junit.jupiter.api.Order;
@@ -27,7 +27,7 @@ void getLogLevel_shouldReturnConfiguredLogLevel() {
2727
Level rootLevel = manager.getLogLevel(ROOT);
2828

2929
// Then
30-
assertThat(logLevel).isEqualTo(INFO);
30+
assertThat(logLevel).isEqualTo(DEBUG);
3131
assertThat(rootLevel).isEqualTo(WARN);
3232
}
3333

powertools-logging/powertools-logging-log4j/src/test/java/software/amazon/lambda/powertools/logging/internal/handler/PowertoolsLogEnabled.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ public class PowertoolsLogEnabled implements RequestHandler<Object, Object> {
2525
private final Logger LOG = LoggerFactory.getLogger(PowertoolsLogEnabled.class);
2626

2727
@Override
28-
@Logging
28+
@Logging(clearState = true)
2929
public Object handleRequest(Object input, Context context) {
3030
LoggingUtils.appendKey("myKey", "myValue");
31-
LOG.info("Test event");
3231
LOG.debug("Test debug event");
3332
return null;
3433
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
# Licensed under the Apache License, Version 2.0 (the
4+
# "License"); you may not use this file except in compliance
5+
# with the License. You may obtain a copy of the License at
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
#
13+
#
14+
15+
# because of LambdaLoggingAspect static initialization of the LoggingManager, we need to
16+
# set an order in the unit tests, especially LambdaLoggingAspectTest needs to be first
17+
junit.jupiter.testclass.order.default=org.junit.jupiter.api.ClassOrderer$OrderAnnotation

powertools-logging/powertools-logging-log4j/src/test/resources/log4j2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</File>
1313
</Appenders>
1414
<Loggers>q
15-
<Logger name="software.amazon.lambda.powertools" level="INFO" additivity="false">
15+
<Logger name="software.amazon.lambda.powertools" level="DEBUG" additivity="false">
1616
<AppenderRef ref="logFileWithEcs"/>
1717
<AppenderRef ref="logFile"/>
1818
</Logger>

0 commit comments

Comments
 (0)