Skip to content

Commit 20d3255

Browse files
authored
Merge pull request #2 from surajkumar/add-logger
Added logger and throw errors where needed
2 parents 3de9af7 + 9aa25d3 commit 20d3255

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ subprojects {
2222
implementation 'com.amazonaws:aws-lambda-java-core:1.2.3'
2323
implementation 'com.amazonaws:aws-lambda-java-events:3.13.0'
2424
implementation 'com.amazonaws:aws-lambda-java-runtime-interface-client:2.6.0'
25+
26+
implementation 'org.slf4j:log4j-over-slf4j:2.0.16'
27+
implementation 'ch.qos.logback:logback-classic:1.5.7'
2528
}
2629
}

lib/src/main/java/org/example/aws/CustomLogger.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package org.example.aws;
22

3+
import com.amazonaws.services.lambda.runtime.Context;
34
import com.amazonaws.services.lambda.runtime.LambdaLogger;
5+
import org.apache.log4j.LogManager;
6+
import org.apache.log4j.Logger;
47

58
public class CustomLogger implements LambdaLogger {
9+
private static final Logger LOGGER = LogManager.getLogger(Context.class);
610

711
@Override
812
public void log(String s) {
9-
System.out.println(s);
13+
LOGGER.info(s);
1014
}
1115

1216
@Override

lib/src/main/java/org/example/aws/LambdaRuntimeLoop.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
import com.fasterxml.jackson.core.JsonProcessingException;
77
import com.fasterxml.jackson.databind.DeserializationFeature;
88
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import org.apache.log4j.LogManager;
10+
import org.apache.log4j.Logger;
911

1012
import java.io.IOException;
1113
import java.net.http.HttpResponse;
1214

1315
public class LambdaRuntimeLoop {
16+
private static final Logger LOGGER = LogManager.getLogger(LambdaRuntimeLoop.class);
17+
1418
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
1519
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
1620

@@ -33,15 +37,13 @@ public void run() throws IOException, InterruptedException {
3337
HttpResponse<String> event = lambdaRuntimeHttpClient.nextInvocation();
3438
String requestId = event.headers().firstValue(AWS_LAMBDA_REQUEST_ID).orElseThrow();
3539
String functionArn = event.headers().firstValue(AWS_LAMBDA_INVOKED_FUNCTION_ARN).orElseThrow();
36-
37-
3840
try {
3941
APIGatewayProxyRequestEvent request = OBJECT_MAPPER.readValue(event.body(), APIGatewayProxyRequestEvent.class);
4042
APIGatewayProxyResponseEvent output
4143
= handler.handleRequest(request, new CustomContext(requestId, functionArn, 0));
4244
lambdaRuntimeHttpClient.invocationResponse(requestId, writeValue(output));
4345
} catch (Exception e) {
44-
System.err.println(e.getMessage());
46+
LOGGER.error(e.getMessage());
4547
try {
4648
lambdaRuntimeHttpClient.invocationError(requestId);
4749
} catch (IOException | InterruptedException ex) {

lib/src/main/java/org/example/aws/Main.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,40 @@
33
import com.amazonaws.services.lambda.runtime.RequestHandler;
44
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
55
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
6+
import org.apache.log4j.LogManager;
7+
import org.apache.log4j.Logger;
68

79
import java.lang.reflect.Constructor;
810

911
public class Main {
12+
private static final Logger LOGGER = LogManager.getLogger(Main.class);
13+
1014
public static void main(String[] args) throws Exception {
1115
LambdaRuntimeHttpClient lambdaRuntimeHttpClient = new LambdaRuntimeHttpClient();
1216

1317
String handlerName = System.getenv("_HANDLER");
14-
System.out.println("Invoking handler " + handlerName);
18+
LOGGER.info("Invoking handler " + handlerName);
1519

1620
RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> handler = getHandlerInstance(handlerName);
17-
if (handler == null) {
18-
System.out.println("Handler is null");
19-
return;
20-
}
21-
2221
LambdaRuntimeLoop lambdaRuntimeLoop = new LambdaRuntimeLoop(lambdaRuntimeHttpClient, handler);
2322
lambdaRuntimeLoop.run();
2423
}
2524

2625
private static RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> getHandlerInstance(String handlerName) {
2726
Class<?> handlerClass = loadHandlerClass(handlerName);
28-
if (handlerClass == null) {
29-
return null;
30-
}
3127
Constructor<?> constructor = findZeroArgConstructor(handlerClass);
3228
if (constructor == null) {
33-
System.out.println("No zero-arg constructor found for " + handlerName);
34-
return null;
29+
throw new RuntimeException("No zero-arg constructor");
3530
}
36-
3731
return instantiateHandler(constructor);
3832
}
3933

4034
private static Class<?> loadHandlerClass(String handlerName) {
4135
try {
4236
return Class.forName(handlerName);
4337
} catch (ClassNotFoundException e) {
44-
System.out.println("Handler not found: " + e.getMessage());
45-
return null;
38+
LOGGER.error("Handler not found on classpath: " + handlerName);
39+
throw new RuntimeException(e);
4640
}
4741
}
4842

@@ -52,6 +46,7 @@ private static Constructor<?> findZeroArgConstructor(Class<?> clazz) {
5246
return constructor;
5347
}
5448
}
49+
LOGGER.warn("No zero-arg constructor found for " + clazz.getName());
5550
return null;
5651
}
5752

@@ -60,8 +55,8 @@ private static RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyRespon
6055
try {
6156
return (RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent>) constructor.newInstance();
6257
} catch (Exception e) {
63-
System.out.println("Failed to construct handler: " + e.getMessage());
64-
return null;
58+
LOGGER.error("Failed to construct instance of handler: ", e);
59+
throw new RuntimeException(e);
6560
}
6661
}
6762
}

0 commit comments

Comments
 (0)