Skip to content

Commit

Permalink
chore(lambda): validate logLevel with JSON logFormat for advanced log…
Browse files Browse the repository at this point in the history
…ging
  • Loading branch information
go-to-k committed Nov 17, 2023
1 parent 95538a1 commit f533ba2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/aws-cdk-lib/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,10 @@ export class Function extends FunctionBase {
* function and undefined if not.
*/
private getLoggingConfig(props: FunctionProps): CfnFunction.LoggingConfigProperty | undefined {
if ((props.applicationLogLevel || props.systemLogLevel) && props.logFormat !== LogFormat.JSON) {
throw new Error('ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON.');
}

let loggingConfig: CfnFunction.LoggingConfigProperty;
if (props.logFormat || props.logGroup) {
loggingConfig = {
Expand Down
56 changes: 55 additions & 1 deletion packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,60 @@ describe('logging Config', () => {
logGroupName: 'customLogGroup',
}),
});
}).toThrowError('CDK does not support setting logRetention and logGroup');
}).toThrow(/CDK does not support setting logRetention and logGroup/);
});

test('Throws when applicationLogLevel is specified with TEXT logFormat', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'stack');
expect(() => {
new lambda.Function(stack, 'Lambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_18_X,
logFormat: lambda.LogFormat.TEXT,
applicationLogLevel: lambda.ApplicationLogLevel.INFO,
});
}).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./);
});

test('Throws when systemLogLevel is specified with TEXT logFormat', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'stack');
expect(() => {
new lambda.Function(stack, 'Lambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_18_X,
logFormat: lambda.LogFormat.TEXT,
systemLogLevel: lambda.SystemLogLevel.INFO,
});
}).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./);
});

test('Throws when applicationLogLevel is specified if logFormat is undefined', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'stack');
expect(() => {
new lambda.Function(stack, 'Lambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_18_X,
applicationLogLevel: lambda.ApplicationLogLevel.INFO,
});
}).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./);
});

test('Throws when systemLogLevel is specified if logFormat is undefined', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'stack');
expect(() => {
new lambda.Function(stack, 'Lambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_18_X,
systemLogLevel: lambda.SystemLogLevel.INFO,
});
}).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./);
});
});

0 comments on commit f533ba2

Please sign in to comment.