Skip to content

Commit

Permalink
feat: GuLambdaFunction should use JSON logging by default (#2260)
Browse files Browse the repository at this point in the history
* feat: GuLambdaFunction uses JSON logging by default

* test: add unit tests to cover new log formatting behaviour

* bring back log format prop

* address PR comments

* use logging format enum in unit tests
  • Loading branch information
NovemberTang authored Apr 11, 2024
1 parent f11bf2f commit 5e59797
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ exports[`GuLambdaThrottlingAlarm construct should match snapshot 1`] = `
},
},
"Handler": "handler.ts",
"LoggingConfig": {
"LogFormat": "JSON",
},
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
Expand Down Expand Up @@ -322,6 +325,9 @@ exports[`The GuLambdaErrorPercentageAlarm construct should create the correct al
},
},
"Handler": "handler.ts",
"LoggingConfig": {
"LogFormat": "JSON",
},
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
Expand Down
51 changes: 50 additions & 1 deletion src/constructs/lambda/lambda.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Duration } from "aws-cdk-lib";
import { Match, Template } from "aws-cdk-lib/assertions";
import { Runtime } from "aws-cdk-lib/aws-lambda";
import { LoggingFormat, Runtime } from "aws-cdk-lib/aws-lambda";
import { simpleGuStackForTesting } from "../../utils/test";
import type { GuStack } from "../core";
import { GuLambdaFunction } from "./lambda";

describe("The GuLambdaFunction class", () => {
Expand Down Expand Up @@ -193,6 +194,54 @@ describe("The GuLambdaFunction class", () => {
});
});

function hasLoggingFormat(stack: GuStack, logFormat: LoggingFormat) {
Template.fromStack(stack).hasResourceProperties("AWS::Lambda::Function", {
LoggingConfig: {
LogFormat: logFormat,
},
});
}

it("should use JSON log formatting by default", () => {
const stack = simpleGuStackForTesting();

new GuLambdaFunction(stack, "lambda", {
fileName: "my-app.jar",
handler: "handler.ts",
runtime: Runtime.JAVA_17,
app: "testing",
});

hasLoggingFormat(stack, LoggingFormat.JSON);
});

it("should use JSON log formatting when JSON is specified", () => {
const stack = simpleGuStackForTesting();

new GuLambdaFunction(stack, "lambda", {
fileName: "my-app.jar",
handler: "handler.ts",
runtime: Runtime.JAVA_17,
app: "testing",
logFormat: "JSON",
});

hasLoggingFormat(stack, LoggingFormat.JSON);
});
it("should use Text log formatting when it is defined", () => {
const stack = simpleGuStackForTesting();

new GuLambdaFunction(stack, "lambda", {
fileName: "my-app.jar",
handler: "handler.ts",
runtime: Runtime.JAVA_17,
app: "testing",
logFormat: "Text",
});

hasLoggingFormat(stack, LoggingFormat.TEXT);
});

it("should not create an alias or version if the enableVersioning prop is unset", () => {
const stack = simpleGuStackForTesting();

Expand Down
4 changes: 3 additions & 1 deletion src/constructs/lambda/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Duration } from "aws-cdk-lib";
import type { PolicyStatement } from "aws-cdk-lib/aws-iam";
import type { FunctionProps, Runtime } from "aws-cdk-lib/aws-lambda";
import { Alias, Code, Function, RuntimeFamily } from "aws-cdk-lib/aws-lambda";
import { Alias, Code, Function, LoggingFormat, RuntimeFamily } from "aws-cdk-lib/aws-lambda";
import { Bucket } from "aws-cdk-lib/aws-s3";
import { StringParameter } from "aws-cdk-lib/aws-ssm";
import { GuDistributable } from "../../types";
Expand Down Expand Up @@ -124,6 +124,7 @@ export class GuLambdaFunction extends Function {
bucketNamePath,
withoutFilePrefix = false,
withoutArtifactUpload = false,
logFormat = LoggingFormat.JSON,
} = props;

const bucketName = bucketNamePath
Expand All @@ -141,6 +142,7 @@ export class GuLambdaFunction extends Function {
const code = Code.fromBucket(bucket, objectKey);
super(scope, id, {
...props,
logFormat,
environment: {
...props.environment,
...defaultEnvironmentVariables,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ exports[`The GuKinesisLambda pattern should create the correct resources for a n
},
"FunctionName": "my-lambda-function",
"Handler": "my-lambda/handler",
"LoggingConfig": {
"LogFormat": "JSON",
},
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ exports[`The GuSnsLambda pattern should create the correct resources for a new s
},
"FunctionName": "my-lambda-function",
"Handler": "my-lambda/handler",
"LoggingConfig": {
"LogFormat": "JSON",
},
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
Expand Down
6 changes: 6 additions & 0 deletions src/patterns/__snapshots__/api-lambda.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ exports[`The GuApiLambda pattern should allow us to link a domain name to a Lamb
},
},
"Handler": "handler.ts",
"LoggingConfig": {
"LogFormat": "JSON",
},
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
Expand Down Expand Up @@ -838,6 +841,9 @@ exports[`The GuApiLambda pattern should create the correct resources with minima
},
},
"Handler": "handler.ts",
"LoggingConfig": {
"LogFormat": "JSON",
},
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
Expand Down
12 changes: 12 additions & 0 deletions src/patterns/__snapshots__/api-multiple-lambdas.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,9 @@ exports[`The GuApiGatewayWithLambdaByPath pattern should create the correct reso
},
},
"Handler": "handler.ts",
"LoggingConfig": {
"LogFormat": "JSON",
},
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
Expand Down Expand Up @@ -957,6 +960,9 @@ exports[`The GuApiGatewayWithLambdaByPath pattern should create the correct reso
},
},
"Handler": "handler.ts",
"LoggingConfig": {
"LogFormat": "JSON",
},
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
Expand Down Expand Up @@ -1164,6 +1170,9 @@ exports[`The GuApiGatewayWithLambdaByPath pattern should create the correct reso
},
},
"Handler": "handler.ts",
"LoggingConfig": {
"LogFormat": "JSON",
},
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
Expand Down Expand Up @@ -1371,6 +1380,9 @@ exports[`The GuApiGatewayWithLambdaByPath pattern should create the correct reso
},
},
"Handler": "handler.ts",
"LoggingConfig": {
"LogFormat": "JSON",
},
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
Expand Down
3 changes: 3 additions & 0 deletions src/patterns/__snapshots__/scheduled-lambda.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ exports[`The GuScheduledLambda pattern should create the correct resources with
},
"FunctionName": "my-lambda-function",
"Handler": "my-lambda/handler",
"LoggingConfig": {
"LogFormat": "JSON",
},
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
Expand Down

0 comments on commit 5e59797

Please sign in to comment.