diff --git a/packages/aws-cdk-lib/aws-lambda/lib/function.ts b/packages/aws-cdk-lib/aws-lambda/lib/function.ts index 5c64c7069f65e..cc461fa788607 100644 --- a/packages/aws-cdk-lib/aws-lambda/lib/function.ts +++ b/packages/aws-cdk-lib/aws-lambda/lib/function.ts @@ -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 = { diff --git a/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts b/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts index ad4c5dd2b956b..73405aa805b9f 100644 --- a/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts +++ b/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts @@ -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./); }); });