Skip to content

Commit f533ba2

Browse files
committed
chore(lambda): validate logLevel with JSON logFormat for advanced logging
1 parent 95538a1 commit f533ba2

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

packages/aws-cdk-lib/aws-lambda/lib/function.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,10 @@ export class Function extends FunctionBase {
10771077
* function and undefined if not.
10781078
*/
10791079
private getLoggingConfig(props: FunctionProps): CfnFunction.LoggingConfigProperty | undefined {
1080+
if ((props.applicationLogLevel || props.systemLogLevel) && props.logFormat !== LogFormat.JSON) {
1081+
throw new Error('ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON.');
1082+
}
1083+
10801084
let loggingConfig: CfnFunction.LoggingConfigProperty;
10811085
if (props.logFormat || props.logGroup) {
10821086
loggingConfig = {

packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts

+55-1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,60 @@ describe('logging Config', () => {
133133
logGroupName: 'customLogGroup',
134134
}),
135135
});
136-
}).toThrowError('CDK does not support setting logRetention and logGroup');
136+
}).toThrow(/CDK does not support setting logRetention and logGroup/);
137+
});
138+
139+
test('Throws when applicationLogLevel is specified with TEXT logFormat', () => {
140+
const app = new cdk.App();
141+
const stack = new cdk.Stack(app, 'stack');
142+
expect(() => {
143+
new lambda.Function(stack, 'Lambda', {
144+
code: new lambda.InlineCode('foo'),
145+
handler: 'index.handler',
146+
runtime: lambda.Runtime.NODEJS_18_X,
147+
logFormat: lambda.LogFormat.TEXT,
148+
applicationLogLevel: lambda.ApplicationLogLevel.INFO,
149+
});
150+
}).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./);
151+
});
152+
153+
test('Throws when systemLogLevel is specified with TEXT logFormat', () => {
154+
const app = new cdk.App();
155+
const stack = new cdk.Stack(app, 'stack');
156+
expect(() => {
157+
new lambda.Function(stack, 'Lambda', {
158+
code: new lambda.InlineCode('foo'),
159+
handler: 'index.handler',
160+
runtime: lambda.Runtime.NODEJS_18_X,
161+
logFormat: lambda.LogFormat.TEXT,
162+
systemLogLevel: lambda.SystemLogLevel.INFO,
163+
});
164+
}).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./);
165+
});
166+
167+
test('Throws when applicationLogLevel is specified if logFormat is undefined', () => {
168+
const app = new cdk.App();
169+
const stack = new cdk.Stack(app, 'stack');
170+
expect(() => {
171+
new lambda.Function(stack, 'Lambda', {
172+
code: new lambda.InlineCode('foo'),
173+
handler: 'index.handler',
174+
runtime: lambda.Runtime.NODEJS_18_X,
175+
applicationLogLevel: lambda.ApplicationLogLevel.INFO,
176+
});
177+
}).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./);
178+
});
179+
180+
test('Throws when systemLogLevel is specified if logFormat is undefined', () => {
181+
const app = new cdk.App();
182+
const stack = new cdk.Stack(app, 'stack');
183+
expect(() => {
184+
new lambda.Function(stack, 'Lambda', {
185+
code: new lambda.InlineCode('foo'),
186+
handler: 'index.handler',
187+
runtime: lambda.Runtime.NODEJS_18_X,
188+
systemLogLevel: lambda.SystemLogLevel.INFO,
189+
});
190+
}).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./);
137191
});
138192
});

0 commit comments

Comments
 (0)