Skip to content

Commit

Permalink
feat(v2): new logging module (#1435)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromevdl authored Dec 11, 2023
1 parent 6b0fe5a commit b139a96
Show file tree
Hide file tree
Showing 147 changed files with 6,070 additions and 2,778 deletions.
1,134 changes: 791 additions & 343 deletions docs/core/logging.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/stylesheets/extra.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.md-grid {
max-width: 81vw
max-width: 90vw
}

.highlight .hll {
Expand Down
2 changes: 1 addition & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Powertools for AWS Lambda (Java) library Examples</name>
<name>Powertools for AWS Lambda (Java) - Examples</name>
<description>
A suite of examples accompanying for Powertools for AWS Lambda (Java).
</description>
Expand Down
7 changes: 3 additions & 4 deletions examples/powertools-examples-batch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
<version>2.0.0-SNAPSHOT</version>
<artifactId>powertools-examples-batch</artifactId>
<packaging>jar</packaging>
<name>Powertools for AWS Lambda (Java) library Examples - Batch</name>
<name>Powertools for AWS Lambda (Java) - Examples - Batch</name>

<properties>
<log4j.version>2.20.0</log4j.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<aspectj.version>1.9.20</aspectj.version>
<aspectj.version>1.9.20.1</aspectj.version>
<sdk.version>2.21.1</sdk.version>
</properties>

Expand All @@ -26,7 +25,7 @@
</dependency>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging</artifactId>
<artifactId>powertools-logging-log4j</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
import com.amazonaws.services.lambda.runtime.events.StreamsEventResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.lambda.powertools.batch.BatchMessageHandlerBuilder;
import software.amazon.lambda.powertools.batch.handler.BatchMessageHandler;

public class DynamoDBStreamBatchHandler implements RequestHandler<DynamodbEvent, StreamsEventResponse> {

private final static Logger LOGGER = LogManager.getLogger(DynamoDBStreamBatchHandler.class);
private static final Logger LOGGER = LoggerFactory.getLogger(DynamoDBStreamBatchHandler.class);
private final BatchMessageHandler<DynamodbEvent, StreamsEventResponse> handler;

public DynamoDBStreamBatchHandler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.demo.batch.model.DdbProduct;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
Expand All @@ -21,7 +21,7 @@

public class DynamoDBWriter implements RequestHandler<ScheduledEvent, String> {

private static final Logger LOGGER = LogManager.getLogger(DynamoDBWriter.class);
private static final Logger LOGGER = LoggerFactory.getLogger(DynamoDBWriter.class);

private final DynamoDbEnhancedClient enhancedClient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.KinesisEvent;
import com.amazonaws.services.lambda.runtime.events.StreamsEventResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.demo.batch.model.Product;
import software.amazon.lambda.powertools.batch.BatchMessageHandlerBuilder;
import software.amazon.lambda.powertools.batch.handler.BatchMessageHandler;

public class KinesisBatchHandler implements RequestHandler<KinesisEvent, StreamsEventResponse> {

private final static Logger LOGGER = LogManager.getLogger(KinesisBatchHandler.class);
private static final Logger LOGGER = LoggerFactory.getLogger(KinesisBatchHandler.class);
private final BatchMessageHandler<KinesisEvent, StreamsEventResponse> handler;

public KinesisBatchHandler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import java.security.SecureRandom;
import java.util.List;
import java.util.stream.IntStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.demo.batch.model.Product;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
Expand All @@ -28,7 +28,7 @@
*/
public class KinesisBatchSender implements RequestHandler<ScheduledEvent, String> {

private static final Logger LOGGER = LogManager.getLogger(KinesisBatchSender.class);
private static final Logger LOGGER = LoggerFactory.getLogger(KinesisBatchSender.class);

private final KinesisClient kinesisClient;
private final SecureRandom random;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.SQSBatchResponse;
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.demo.batch.model.Product;
import software.amazon.lambda.powertools.batch.BatchMessageHandlerBuilder;
import software.amazon.lambda.powertools.batch.handler.BatchMessageHandler;

public class SqsBatchHandler implements RequestHandler<SQSEvent, SQSBatchResponse> {
private final static Logger LOGGER = LogManager.getLogger(SqsBatchHandler.class);
private static final Logger LOGGER = LoggerFactory.getLogger(SqsBatchHandler.class);
private final BatchMessageHandler<SQSEvent, SQSBatchResponse> handler;

public SqsBatchHandler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import java.security.SecureRandom;
import java.util.List;
import java.util.stream.IntStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.demo.batch.model.Product;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.services.sqs.SqsClient;
Expand All @@ -27,7 +27,7 @@
*/
public class SqsBatchSender implements RequestHandler<ScheduledEvent, String> {

private static final Logger LOGGER = LogManager.getLogger(SqsBatchSender.class);
private static final Logger LOGGER = LoggerFactory.getLogger(SqsBatchSender.class);

private final SqsClient sqsClient;
private final SecureRandom random;
Expand Down
25 changes: 3 additions & 22 deletions examples/powertools-examples-cloudformation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
<artifactId>powertools-examples-cloudformation</artifactId>
<packaging>jar</packaging>

<name>Powertools for AWS Lambda (Java) library Examples - CloudFormation</name>
<name>Powertools for AWS Lambda (Java) - Examples - CloudFormation</name>

<properties>
<log4j.version>2.20.0</log4j.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<lambda.core.version>1.2.3</lambda.core.version>
<lambda.events.version>3.11.3</lambda.events.version>
<aws.sdk.version>2.21.0</aws.sdk.version>
<aspectj.version>1.9.20</aspectj.version>
<aspectj.version>1.9.20.1</aspectj.version>

</properties>
<dependencyManagement>
Expand Down Expand Up @@ -49,19 +48,9 @@
</dependency>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging</artifactId>
<artifactId>powertools-logging-log4j</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
Expand Down Expand Up @@ -91,14 +80,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j.version}</version>
</dependency>



</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.CloudFormationCustomResourceEvent;
import java.util.Objects;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.waiters.WaiterResponse;
Expand All @@ -24,7 +24,7 @@
*/

public class App extends AbstractCustomResourceHandler {
private final static Logger log = LogManager.getLogger(App.class);
private static final Logger log = LoggerFactory.getLogger(App.class);
private final S3Client s3Client;

public App() {
Expand All @@ -47,7 +47,7 @@ protected Response create(CloudFormationCustomResourceEvent cloudFormationCustom
Objects.requireNonNull(cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName"),
"BucketName cannot be null.");

log.info(cloudFormationCustomResourceEvent);
log.info(cloudFormationCustomResourceEvent.toString());
String bucketName = (String) cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName");
log.info("Bucket Name {}", bucketName);
try {
Expand All @@ -57,7 +57,7 @@ protected Response create(CloudFormationCustomResourceEvent cloudFormationCustom
return Response.success(bucketName);
} catch (AwsServiceException | SdkClientException e) {
// In case of error, return a failed response, with the bucketName as the physicalResourceId
log.error(e);
log.error("Unable to create bucket", e);
return Response.failed(bucketName);
}
}
Expand All @@ -77,7 +77,7 @@ protected Response update(CloudFormationCustomResourceEvent cloudFormationCustom
Objects.requireNonNull(cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName"),
"BucketName cannot be null.");

log.info(cloudFormationCustomResourceEvent);
log.info(cloudFormationCustomResourceEvent.toString());
// Get the physicalResourceId. physicalResourceId is the value returned to CloudFormation in the Create request, and passed in on subsequent requests (e.g. UPDATE or DELETE)
String physicalResourceId = cloudFormationCustomResourceEvent.getPhysicalResourceId();
log.info("Physical Resource ID {}", physicalResourceId);
Expand All @@ -94,7 +94,7 @@ protected Response update(CloudFormationCustomResourceEvent cloudFormationCustom
// Return a successful response with the newBucketName
return Response.success(newBucketName);
} catch (AwsServiceException | SdkClientException e) {
log.error(e);
log.error("Unable to create bucket", e);
return Response.failed(newBucketName);
}
} else {
Expand All @@ -120,7 +120,7 @@ protected Response delete(CloudFormationCustomResourceEvent cloudFormationCustom
Objects.requireNonNull(cloudFormationCustomResourceEvent.getPhysicalResourceId(),
"PhysicalResourceId cannot be null.");

log.info(cloudFormationCustomResourceEvent);
log.info(cloudFormationCustomResourceEvent.toString());
// Get the physicalResourceId. physicalResourceId is the value provided to CloudFormation in the Create request.
String bucketName = cloudFormationCustomResourceEvent.getPhysicalResourceId();
log.info("Bucket Name {}", bucketName);
Expand All @@ -135,7 +135,7 @@ protected Response delete(CloudFormationCustomResourceEvent cloudFormationCustom
return Response.success(bucketName);
} catch (AwsServiceException | SdkClientException e) {
// Return a failed response in case of errors during the bucket deletion
log.error(e);
log.error("Unable to delete bucket", e);
return Response.failed(bucketName);
}
} else {
Expand Down Expand Up @@ -166,7 +166,7 @@ private void createBucket(String bucketName) {
s3Client.createBucket(createBucketRequest);
WaiterResponse<HeadBucketResponse> waiterResponse =
waiter.waitUntilBucketExists(HeadBucketRequest.builder().bucket(bucketName).build());
waiterResponse.matched().response().ifPresent(log::info);
waiterResponse.matched().response().ifPresent(res -> log.info(res.toString()));
log.info("Bucket Created {}", bucketName);
}
}
4 changes: 2 additions & 2 deletions examples/powertools-examples-core-utilities/cdk/app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<name>Powertools for AWS Lambda (Java) library Examples - Core Utilities (logging, tracing, metrics) with CDK</name>
<name>Powertools for AWS Lambda (Java) - Examples - Core Utilities (logging, tracing, metrics) with CDK</name>
<groupId>software.amazon.lambda.examples</groupId>
<!-- TODO TODO TODO this should build from SNAPSHOT, but it doesn't, because the snapshots
don't appear in the docker environment CDK builds it in in our CDK tests. How to procede? V2 blocker -->
Expand All @@ -14,7 +14,7 @@
<log4j.version>2.20.0</log4j.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<aspectj.version>1.9.20</aspectj.version>
<aspectj.version>1.9.20.1</aspectj.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.Unit;
import software.amazon.lambda.powertools.logging.Logging;
Expand All @@ -44,8 +44,7 @@
* Handler for requests to Lambda function.
*/
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private final static Logger log = LogManager.getLogger(App.class);

private static final Logger log = LoggerFactory.getLogger(App.class);
@Logging(logEvent = true, samplingRate = 0.7)
@Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR)
@Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.2.2'
implementation 'com.amazonaws:aws-lambda-java-events:3.11.0'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.2'
implementation 'org.aspectj:aspectjrt:1.9.20.1'
aspect 'software.amazon.lambda:powertools-tracing:2.0.0-SNAPSHOT'
aspect 'software.amazon.lambda:powertools-logging:2.0.0-SNAPSHOT'
aspect 'software.amazon.lambda:powertools-logging-log4j:2.0.0-SNAPSHOT'
aspect 'software.amazon.lambda:powertools-metrics:2.0.0-SNAPSHOT'
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.Unit;
import software.amazon.lambda.powertools.logging.Logging;
Expand All @@ -44,7 +44,7 @@
* Handler for requests to Lambda function.
*/
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private final static Logger log = LogManager.getLogger(App.class);
private static final Logger log = LoggerFactory.getLogger(App.class);

@Logging(logEvent = true, samplingRate = 0.7)
@Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ repositories {
}

dependencies {
implementation("com.amazonaws:aws-lambda-java-core:1.2.2")
implementation("com.fasterxml.jackson.core:jackson-annotations:2.13.2")
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.2.2")
implementation("com.amazonaws:aws-lambda-java-events:3.11.0")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.2")
implementation("com.amazonaws:aws-lambda-java-core:1.2.3")
implementation("com.fasterxml.jackson.core:jackson-annotations:2.15.1")
implementation("com.fasterxml.jackson.core:jackson-databind:2.15.3")
implementation("com.amazonaws:aws-lambda-java-events:3.11.3")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.2")
implementation("org.aspectj:aspectjrt:1.9.20.1")
aspect("software.amazon.lambda:powertools-tracing:2.0.0-SNAPSHOT")
aspect("software.amazon.lambda:powertools-logging:2.0.0-SNAPSHOT")
aspect("software.amazon.lambda:powertools-logging-log4j:2.0.0-SNAPSHOT")
aspect("software.amazon.lambda:powertools-metrics:2.0.0-SNAPSHOT")
testImplementation("junit:junit:4.13.2")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.10")
}

kotlin {
Expand Down
Loading

0 comments on commit b139a96

Please sign in to comment.