Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lambda): validate logLevel with logFormat for advanced logging #28045

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error('ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON.');
throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LogFormat to '${LogFormat.JSON}', got '${props.logFormat}'.`);

Message formatting.

}

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./);
});
});
Loading