Skip to content

Commit 43d1dc1

Browse files
authored
Merge pull request #101 from jimmyn/feat/lambda-logging-config
feat(AWS Lambda): Support advanced logging configuration
2 parents 8c42ce6 + 0fcf158 commit 43d1dc1

File tree

5 files changed

+475
-0
lines changed

5 files changed

+475
-0
lines changed

docs/guides/functions.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,33 @@ functions:
873873
ephemeralStorageSize: 1024
874874
```
875875

876+
## Logging Configuration
877+
878+
[Configuring Lambda advanced logging options](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html#monitoring-cloudwatchlogs-advanced)
879+
880+
This can be configured at the provider level (applies to all functions) or individually per function:
881+
882+
```yml
883+
# Provider-level configuration (applies to all functions)
884+
provider:
885+
logs:
886+
lambda:
887+
logFormat: JSON
888+
applicationLogLevel: INFO
889+
systemLogLevel: WARN
890+
logGroup: /aws/lambda/global-log-group
891+
892+
# Function-level configuration (overrides provider settings)
893+
functions:
894+
helloLogging:
895+
handler: handler.handler
896+
logs:
897+
applicationLogLevel: DEBUG
898+
logFormat: JSON
899+
logGroup: helloLoggingLogGroup
900+
systemLogLevel: DEBUG
901+
```
902+
876903
## Lambda Hashing Algorithm migration
877904

878905
**Note** Below migration guide is intended to be used if you are already using `v3` version of the Framework and you have `provider.lambdaHashingVersion` property set to `20200924` in your configuration file. If you are still on v2 and want to upgrade to v3, please refer to [V3 Upgrade docs](../../../guides/upgrading-v3.md#lambda-hashing-algorithm).

docs/guides/serverless.yml.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,17 @@ Configure logs for the deployed resources:
517517
```yml
518518
provider:
519519
logs:
520+
# Optional Configuration of Lambda Logging Configuration
521+
lambda:
522+
# The Log Format to be used for all lambda functions (default: Text)
523+
logFormat: JSON
524+
# The Application Log Level to be used, This can only be set if `logFormat` is set to `JSON`
525+
applicationLogLevel: ERROR
526+
# The System Log Level to be used, This can only be set if `logFormat` is set to `JSON`
527+
systemLogLevel: INFO
528+
# The LogGroup that will be used by default. If this is set the Framework will not create LogGroups for any functions
529+
logGroup: /aws/lambda/global-log-group
530+
520531
# Enable HTTP API logs
521532
# This can either be set to `httpApi: true` to use defaults, or configured via subproperties
522533
# Can only be configured if the API is created by Serverless Framework
@@ -722,6 +733,12 @@ functions:
722733
maximumRetryAttempts: 1
723734
# Maximum event age in seconds when invoking asynchronously (between 60 and 21600)
724735
maximumEventAge: 7200
736+
# Configuring Lambda advanced logging options
737+
logs:
738+
applicationLogLevel: DEBUG
739+
logFormat: JSON
740+
logGroup: helloLoggingLogGroup
741+
systemLogLevel: DEBUG
725742
```
726743
727744
## Lambda events

lib/plugins/aws/package/compile/functions.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,34 @@ class AwsCompileFunctions {
632632
}
633633
}
634634

635+
const logs =
636+
functionObject.logs ||
637+
(this.serverless.service.provider.logs && this.serverless.service.provider.logs.lambda);
638+
639+
if (logs) {
640+
const loggingConfig = {};
641+
642+
if (logs.applicationLogLevel) {
643+
loggingConfig.ApplicationLogLevel = logs.applicationLogLevel;
644+
}
645+
646+
if (logs.logFormat) {
647+
loggingConfig.LogFormat = logs.logFormat;
648+
}
649+
650+
if (logs.logGroup) {
651+
loggingConfig.LogGroup = logs.logGroup;
652+
}
653+
654+
if (logs.systemLogLevel) {
655+
loggingConfig.SystemLogLevel = logs.systemLogLevel;
656+
}
657+
658+
if (Object.keys(loggingConfig).length) {
659+
functionResource.Properties.LoggingConfig = loggingConfig;
660+
}
661+
}
662+
635663
this.compileFunctionUrl(functionName);
636664
this.compileFunctionEventInvokeConfig(functionName);
637665
}

lib/plugins/aws/provider.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,19 @@ class AwsProvider {
12311231
},
12321232
],
12331233
},
1234+
lambda: {
1235+
type: 'object',
1236+
properties: {
1237+
applicationLogLevel: {
1238+
type: 'string',
1239+
enum: ['DEBUG', 'ERROR', 'FATAL', 'INFO', 'TRACE', 'WARN'],
1240+
},
1241+
logFormat: { type: 'string', enum: ['JSON', 'Text'] },
1242+
logGroup: { type: 'string', pattern: '^[.\\-_/#A-Za-z0-9]{1,512}$' },
1243+
systemLogLevel: { type: 'string', enum: ['DEBUG', 'INFO', 'WARN'] },
1244+
},
1245+
additionalProperties: false,
1246+
},
12341247
},
12351248
},
12361249
memorySize: { $ref: '#/definitions/awsLambdaMemorySize' },
@@ -1401,6 +1414,19 @@ class AwsProvider {
14011414
disableLogs: { type: 'boolean' },
14021415
environment: { $ref: '#/definitions/awsLambdaEnvironment' },
14031416
ephemeralStorageSize: { type: 'integer', minimum: 512, maximum: 10240 },
1417+
logs: {
1418+
type: 'object',
1419+
properties: {
1420+
applicationLogLevel: {
1421+
type: 'string',
1422+
enum: ['DEBUG', 'ERROR', 'FATAL', 'INFO', 'TRACE', 'WARN'],
1423+
},
1424+
logFormat: { type: 'string', enum: ['JSON', 'Text'] },
1425+
logGroup: { type: 'string', pattern: '^[.\\-_/#A-Za-z0-9]{1,512}$' },
1426+
systemLogLevel: { type: 'string', enum: ['DEBUG', 'INFO', 'WARN'] },
1427+
},
1428+
additionalProperties: false,
1429+
},
14041430
fileSystemConfig: {
14051431
type: 'object',
14061432
properties: {

0 commit comments

Comments
 (0)