Skip to content

Commit

Permalink
chore: deprecate applicationLogLevel and systemLogLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
dali546 committed Apr 24, 2024
1 parent d764fbc commit eeb896c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ new Function(stack, 'LambdaWithLogLevel', {
applicationLogLevel: ApplicationLogLevel.INFO,
});

new Function(stack, 'LambdaWithLogLevelV2', {
code: new InlineCode('foo'),
handler: 'index.handler',
runtime: Runtime.NODEJS_18_X,
loggingFormat: LoggingFormat.JSON,
systemLogLevelV2: SystemLogLevel.INFO,
applicationLogLevelV2: ApplicationLogLevel.INFO,
});

new integ.IntegTest(app, 'lambda-logging-config', {
testCases: [stack],
});
Expand Down
12 changes: 6 additions & 6 deletions packages/aws-cdk-lib/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,20 @@ as choosing the log group:
```ts
import { ILogGroup } from 'aws-cdk-lib/aws-logs';

declare const logGroup: ILogGroup;
declare const logGroup: ILogGroup;

new lambda.Function(this, 'Lambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_18_X,
loggingFormat: lambda.LoggingFormat.JSON,
systemLogLevel: lambda.SystemLogLevel.INFO,
applicationLogLevel: lambda.ApplicationLogLevel.INFO,
systemLogLevelV2: lambda.SystemLogLevel.INFO,
applicationLogLevelV2: lambda.ApplicationLogLevel.INFO,
logGroup: logGroup,
});
```

To use `applicationLogLevel` and/or `systemLogLevel` you must set `loggingFormat` to `LoggingFormat.JSON`.
To use `applicationLogLevelV2` and/or `systemLogLevelV2` you must set `loggingFormat` to `LoggingFormat.JSON`.

## Resource-based Policies

Expand Down Expand Up @@ -1077,8 +1077,8 @@ const fn = new lambda.Function(this, 'MyLambda', {

## IPv6 support

You can configure IPv6 connectivity for lambda function by setting `Ipv6AllowedForDualStack` to true.
It allows Lambda functions to specify whether the IPv6 traffic should be allowed when using dual-stack VPCs.
You can configure IPv6 connectivity for lambda function by setting `Ipv6AllowedForDualStack` to true.
It allows Lambda functions to specify whether the IPv6 traffic should be allowed when using dual-stack VPCs.
To access IPv6 network using Lambda, Dual-stack VPC is required. Using dual-stack VPC a function communicates with subnet over either of IPv4 or IPv6.

```ts
Expand Down
38 changes: 31 additions & 7 deletions packages/aws-cdk-lib/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,15 +536,29 @@ export interface FunctionOptions extends EventInvokeConfigOptions {

/**
* Sets the application log level for the function.
* @deprecated Use `applicationLogLevelV2` as a property instead.
* @default "INFO"
*/
readonly applicationLogLevel?: ApplicationLogLevel;
readonly applicationLogLevel?: string;

/**
* Sets the application log level for the function.
* @default ApplicationLogLevel.INFO
*/
readonly applicationLogLevelV2?: ApplicationLogLevel;

/**
* Sets the system log level for the function.
* @deprecated Use `systemLogLevelV2` as a property instead.
* @default "INFO"
*/
readonly systemLogLevel?: SystemLogLevel;
readonly systemLogLevel?: string;

/**
* Sets the system log level for the function.
* @default SystemLogLevel.INFO
*/
readonly systemLogLevelV2?: SystemLogLevel;
}

export interface FunctionProps extends FunctionOptions {
Expand Down Expand Up @@ -1162,25 +1176,35 @@ 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
&& props.loggingFormat === undefined) {
if ((props.applicationLogLevel || props.applicationLogLevelV2 || props.systemLogLevel || props.systemLogLevelV2)
&& props.logFormat !== LogFormat.JSON
&& props.loggingFormat === undefined) {
throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LogFormat to '${LogFormat.JSON}', got '${props.logFormat}'.`);
}

if ((props.applicationLogLevel || props.systemLogLevel) && props.loggingFormat !== LoggingFormat.JSON && props.logFormat === undefined) {
if ((props.applicationLogLevel || props.applicationLogLevelV2 || props.systemLogLevel || props.systemLogLevelV2)
&& props.loggingFormat !== LoggingFormat.JSON && props.logFormat === undefined) {
throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LoggingFormat to '${LoggingFormat.JSON}', got '${props.loggingFormat}'.`);
}

if (props.logFormat && props.loggingFormat) {
throw new Error('Only define LogFormat or LoggingFormat, not both.');
}

if (props.applicationLogLevel && props.applicationLogLevelV2) {
throw new Error('Only define applicationLogLevel or applicationLogLevelV2, not both.');
}

if (props.systemLogLevel && props.systemLogLevelV2) {
throw new Error('Only define systemLogLevel or systemLogLevelV2, not both.');
}

let loggingConfig: CfnFunction.LoggingConfigProperty;
if (props.logFormat || props.logGroup || props.loggingFormat) {
loggingConfig = {
logFormat: props.logFormat || props.loggingFormat,
systemLogLevel: props.systemLogLevel,
applicationLogLevel: props.applicationLogLevel,
systemLogLevel: props.systemLogLevel || props.systemLogLevelV2,
applicationLogLevel: props.applicationLogLevel || props.applicationLogLevelV2,
logGroup: props.logGroup?.logGroupName,
};
return loggingConfig;
Expand Down
52 changes: 52 additions & 0 deletions packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ describe('logging Config', () => {
});
});

test('Logging Config with LogLevel set with enum keys', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'stack');
new lambda.Function(stack, 'Lambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_18_X,
logFormat: lambda.LogFormat.JSON,
systemLogLevelV2: lambda.SystemLogLevel.INFO,
applicationLogLevelV2: lambda.ApplicationLogLevel.INFO,
});
// WHEN
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
LoggingConfig: {
LogFormat: 'JSON',
SystemLogLevel: 'INFO',
ApplicationLogLevel: 'INFO',
},
});
});

test('Get function custom logGroup', () => {
// GIVEN
const app = new cdk.App();
Expand Down Expand Up @@ -258,3 +280,33 @@ test('Throws when loggingFormat and logFormat are both specified', () => {
});
}).toThrow(/Only define LogFormat or LoggingFormat, not both./);
});

test('Throws when applicationLogLevel and applicationLogLevelV2 are both specified', () => {
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,
loggingFormat: lambda.LoggingFormat.JSON,
applicationLogLevel: lambda.ApplicationLogLevel.INFO,
applicationLogLevelV2: lambda.ApplicationLogLevel.WARN,
});
}).toThrow(/Only define applicationLogLevel or applicationLogLevelV2, not both./);
});

test('Throws when systemLogLevel and systemLogLevelV2 are both specified', () => {
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,
loggingFormat: lambda.LoggingFormat.JSON,
systemLogLevel: lambda.SystemLogLevel.INFO,
systemLogLevelV2: lambda.SystemLogLevel.WARN,
});
}).toThrow(/Only define systemLogLevel or systemLogLevelV2, not both./);
});

0 comments on commit eeb896c

Please sign in to comment.