-
Lambda Handlerpackage event;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import software.amazon.lambda.powertools.logging.Logging;
public class HandlerApiGateway implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private static final Logger LOG = LogManager.getLogger();
@Logging
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) {
LOG.info("[INFO]" + event);
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
response.setIsBase64Encoded(false);
response.setStatusCode(200);
return response;
}
} Testpackage event;
import com.amazonaws.services.lambda.runtime.ClientContext;
import com.amazonaws.services.lambda.runtime.CognitoIdentity;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class TestHandlerApiGateway {
@Test
void test() {
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
HandlerApiGateway handler = new HandlerApiGateway();
APIGatewayProxyResponseEvent response = handler.handleRequest(event, new LambdaContext());
assertEquals(200, response.getStatusCode());
}
static class LambdaContext implements Context {
public String getAwsRequestId() {
return "test-test";
}
public String getLogGroupName() {
return "/aws/lambda/event";
}
public String getLogStreamName() {
return "test";
}
public String getFunctionName() {
return "event";
}
public String getFunctionVersion() {
return "$LATEST";
}
public String getInvokedFunctionArn() {
return "arn:aws:lambda:REGION:ACCOUNT-ID:function:event";
}
public CognitoIdentity getIdentity() {
return null;
}
public ClientContext getClientContext() {
return null;
}
public int getRemainingTimeInMillis() {
return 300000;
}
public int getMemoryLimitInMB() {
return 512;
}
public LambdaLogger getLogger() {
return new LambdaLogger() {
public void log(String message) {
}
public void log(byte[] message) {
}
};
}
}
} log4j2.xml<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="JsonAppender" target="SYSTEM_OUT">
<JsonTemplateLayout eventTemplateUri="classpath:LambdaJsonLayout.json" />
</Console>
</Appenders>
<Loggers>
<Logger name="JsonLogger" level="INFO" additivity="false">
<AppenderRef ref="JsonAppender"/>
</Logger>
<Root level="all">
<AppenderRef ref="JsonAppender"/>
</Root>
</Loggers>
</Configuration> Running environment
Actual
Running on AWS
Expectedcontains
etc. Am I missing something ? |
Beta Was this translation helpful? Give feedback.
Answered by
jeromevdl
Jun 28, 2023
Replies: 2 comments 1 reply
-
Hi, thanks for your message. I've just tried with the example and I have the function information: {
"timestamp": "2023-06-28T07:34:21.824+0000UTC",
"instant": {
"epochSecond": 1687937661,
"nanoOfSecond": 824917000
},
"thread": "main",
"level": "INFO",
"loggerName": "helloworld.App",
"message": "13.43.90.247",
"endOfBatch": false,
"loggerFqcn": "org.apache.logging.log4j.spi.AbstractLogger",
"threadId": 1,
"threadPriority": 5,
"coldStart": "true",
"functionArn": "arn:aws:lambda:eu-west-2:705456458224:function:powertools-core-HelloWorldFunction-JkgcEOm8TW1M",
"functionMemorySize": "512",
"functionName": "powertools-core-HelloWorldFunction-JkgcEOm8TW1M",
"functionVersion": "$LATEST",
"function_request_id": "32169fd6-8783-4b34-9d66-73243e051d48",
"samplingRate": "0.1",
"service": "hello",
"test": "willBeLogged",
"xray_trace_id": "1-649be274-65c779e10520769b4a021182"
} Nothing jumps to my eyes with what you're sharing, I suspect you don't have the proper aspectJ configuration in your gradle: plugins{
id 'java'
id 'io.freefair.aspectj.post-compile-weaving' version '6.3.0'
}
repositories {
mavenCentral()
}
dependencies {
aspect 'software.amazon.lambda:powertools-logging:1.15.0'
}
sourceCompatibility = 11
targetCompatibility = 11 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
H1Gdev
-
Thanks, good to see it worked. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks for your message. I've just tried with the example and I have the function information: