Skip to content

Commit

Permalink
Bringing back the logging that's actually readable in AWS
Browse files Browse the repository at this point in the history
  • Loading branch information
gj0dcsa committed Oct 26, 2024
1 parent 1549022 commit 819f264
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 117 deletions.
78 changes: 3 additions & 75 deletions lambda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
<name>dcsa-conformance-lambda</name>
<description>DCSA Conformance Lambda</description>

<properties>
<aspectj.version>1.9.21.2</aspectj.version>
</properties>

<dependencies>
<dependency>
<groupId>org.dcsa.conformance</groupId>
Expand All @@ -39,23 +35,10 @@
<version>1.12.773</version>
</dependency>

<!-- AWS Lambda Powertools, for logging, tracing -->
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging</artifactId>
<version>1.18.0</version>
</dependency>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-metrics</artifactId>
<version>1.18.0</version>
</dependency>
<!-- Use specific Java 21+ version -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
<scope>runtime</scope>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.16</version>
</dependency>

<dependency>
Expand All @@ -66,61 +49,13 @@

<build>
<plugins>
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.14</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<complianceLevel>${java.version}</complianceLevel>
<forceAjcCompile>true</forceAjcCompile><!-- Enable Lombok in Lambda -->
<sources/>
<weaveDirectories>
<weaveDirectory>${project.build.directory}/classes</weaveDirectory>
</weaveDirectories><!-- end Enable Lombok -->
<aspectLibraries>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging</artifactId>
</aspectLibrary>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-metrics</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<finalName>conformance-lambda</finalName>
<transformers>
<transformer implementation="org.apache.logging.log4j.maven.plugins.shade.transformer.Log4j2PluginCacheFileTransformer"/>
</transformers>
<relocations><!-- Fixing 'WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance. -->
<relocation>
<pattern>META-INF/versions/9/org/apache/logging/log4j/</pattern>
<shadedPattern>org/apache/logging/log4j/</shadedPattern>
</relocation>
</relocations><!-- end fixing WARNING -->
</configuration>
<executions>
<execution>
Expand All @@ -130,13 +65,6 @@
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-transform-maven-shade-plugin-extensions</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Expand Down
12 changes: 5 additions & 7 deletions lambda/src/main/java/org/dcsa/conformance/lambda/ApiLambda.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.dcsa.conformance.sandbox.ConformanceSandbox;
import org.dcsa.conformance.sandbox.ConformanceWebRequest;
import org.dcsa.conformance.sandbox.ConformanceWebResponse;
import org.dcsa.conformance.sandbox.state.ConformancePersistenceProvider;
import software.amazon.lambda.powertools.logging.Logging;

@Log4j2
@Slf4j
public class ApiLambda
implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

@Logging
public APIGatewayProxyResponseEvent handleRequest(
final APIGatewayProxyRequestEvent event, final Context context) {
try {
log.info("event = {}", event);
System.out.println("event = " + event + ", context = " + context);
JsonNode jsonEvent = OBJECT_MAPPER.valueToTree(event);
log.debug("jsonEvent = {}", jsonEvent);
log.info("jsonEvent = {}", jsonEvent.toPrettyString());

ConformancePersistenceProvider persistenceProvider =
LambdaToolkit.createPersistenceProvider();
Expand All @@ -53,7 +51,7 @@ public APIGatewayProxyResponseEvent handleRequest(
.withStatusCode(conformanceWebResponse.statusCode())
.withBody(conformanceWebResponse.body());
} catch (RuntimeException | Error e) {
log.error("Unhandled exception: ", e);
log.error("Unhandled exception: {}", e, e);
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.*;
import java.nio.charset.StandardCharsets;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.dcsa.conformance.core.toolkit.JsonToolkit;
import org.dcsa.conformance.sandbox.ConformanceSandbox;
import org.dcsa.conformance.sandbox.state.ConformancePersistenceProvider;
import software.amazon.lambda.powertools.logging.Logging;

@Log4j2

@Slf4j
public class SandboxTaskLambda implements RequestStreamHandler {

@Logging
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {
try {
JsonNode jsonInput = JsonToolkit.inputStreamToJsonNode(inputStream);
log.info("jsonInput = {}", jsonInput);
log.info("jsonInput = {}", jsonInput.toPrettyString());

try {
Thread.sleep(100); // for consistency with the local app
Expand All @@ -38,13 +37,17 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
jsonInput);

ObjectNode jsonOutput = OBJECT_MAPPER.createObjectNode();
log.debug("jsonOutput = {}", jsonOutput);
outputStream.write(jsonOutput.toString().getBytes(StandardCharsets.UTF_8));
log.info("jsonOutput {}", jsonOutput.toPrettyString());
try (BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
writer.write(jsonOutput.toPrettyString());
writer.flush();
}
} catch (RuntimeException | Error e) {
log.error("Unhandled exception: ", e);
log.error("Unhandled exception: {}", e, e);
throw e;
} catch (IOException e) {
log.error("Unhandled exception: ", e);
log.error("Unhandled exception: {}", e, e);
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.*;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.dcsa.conformance.core.toolkit.JsonToolkit;
import org.dcsa.conformance.sandbox.ConformanceAccessException;
import org.dcsa.conformance.sandbox.ConformanceWebuiHandler;
import org.dcsa.conformance.sandbox.state.ConformancePersistenceProvider;
import software.amazon.lambda.powertools.logging.Logging;

@Log4j2
@Slf4j
public class WebuiLambda
implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

@Logging
public APIGatewayProxyResponseEvent handleRequest(
final APIGatewayProxyRequestEvent event, final Context context) {
try {
log.info("event = {}", event);
System.out.println("event = " + event + ", context = " + context);
log.info("event.getPath() = {}", event.getPath());

JsonNode jsonEvent = OBJECT_MAPPER.valueToTree(event);
log.debug("JSON event = {}", jsonEvent);
log.info("JSON event = {}", jsonEvent.toString());

String cognitoIdAsEnvironmentId =
jsonEvent
Expand All @@ -36,7 +35,7 @@ public APIGatewayProxyResponseEvent handleRequest(
.get("claims")
.get("cognito:username")
.asText();
log.debug("cognitoIdAsEnvironmentId='{}'", cognitoIdAsEnvironmentId);
log.info("cognitoIdAsEnvironmentId='{}'", cognitoIdAsEnvironmentId);

ConformancePersistenceProvider persistenceProvider =
LambdaToolkit.createPersistenceProvider();
Expand All @@ -54,7 +53,7 @@ public APIGatewayProxyResponseEvent handleRequest(
webuiHandler
.handleRequest(
cognitoIdAsEnvironmentId, JsonToolkit.stringToJsonNode(event.getBody()))
.toString();
.toPrettyString();
} catch (ConformanceAccessException e) {
return new APIGatewayProxyResponseEvent()
.withMultiValueHeaders(Map.of("Content-Type", List.of(JsonToolkit.JSON_UTF_8)))
Expand All @@ -67,7 +66,7 @@ public APIGatewayProxyResponseEvent handleRequest(
.withStatusCode(200)
.withBody(responseBody);
} catch (RuntimeException | Error e) {
log.error("Unhandled exception: ", e);
log.error("Unhandled exception: {}", e, e);
throw e;
}
}
Expand Down
17 changes: 0 additions & 17 deletions lambda/src/main/resources/log4j2.xml

This file was deleted.

0 comments on commit 819f264

Please sign in to comment.