Skip to content

Commit 9a4441a

Browse files
authored
Fix/Improve logging API, validation, and doc (#492)
1 parent 88723e1 commit 9a4441a

File tree

7 files changed

+50
-31
lines changed

7 files changed

+50
-31
lines changed

doc/general-config.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ appSync:
4747
- `substitutions`: See [Substitutions](substitutions.md)
4848
- `caching`: See [Cacing](caching.md)
4949
- `waf`: See [Web Application Firefall](WAF.md)
50-
- `log`: See [Logs](#Logs)
50+
- `logging`: See [Logging](#Logging)
5151
- `defaultMappingTemplates`:
5252
- `request`: Optional. A default request mapping template filename for all resolvers.
5353
- `response`: Optional. A default response mapping template filename for all resolvers.
@@ -176,17 +176,18 @@ Old-style descriptions (using `#`) are supported by AppSync but will be removed
176176

177177
Types can implement multiple [interfaces](https://spec.graphql.org/October2021/#sec-Interfaces) using an ampersand `&` in GraphQL, but AppSync uses the old comma (`,`) separator. `&` is the only separator suported by this plugin, but it will automatically be replaced with a `,`.
178178

179-
## Logs
179+
## Logging
180180

181181
```yaml
182182
appSync:
183183
name: my-api
184-
log:
184+
logging:
185185
level: ERROR
186-
logRetentionInDays: 14
186+
retentionInDays: 14
187187
```
188188

189189
- `level`: `ERROR`, `NONE`, or `ALL`
190-
- `excludeVerboseContent`: Boolean. Exclude or not verbose content.
191-
- `logRetentionInDays`: Number of days to retain the logs.
190+
- `enabled`: Boolean, Optional. Defaults to `true` when `logging` is present.
191+
- `excludeVerboseContent`: Boolean, Optional. Exclude or not verbose content (headers, response headers, context, and evaluated mapping templates), regardless of field logging level. Defaults to `false`.
192+
- `retentionInDays`: Optional. Number of days to retain the logs. Defaults to [`provider.logRetentionInDays`](https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml#general-function-settings).
192193
- `roleArn`: Optional. The role ARN to use for AppSync to write into CloudWatch. If not specified, a new role is created by default.

src/__tests__/api.test.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ describe('Api', () => {
3333
it('should compile the Api Resource with logs enabled', () => {
3434
const api = new Api(
3535
given.appSyncConfig({
36-
log: {
36+
logging: {
3737
level: 'ERROR',
3838
excludeVerboseContent: false,
39-
logRetentionInDays: 14,
39+
retentionInDays: 14,
4040
},
4141
}),
4242
plugin,
@@ -344,19 +344,35 @@ describe('Api', () => {
344344
});
345345

346346
describe('Logs', () => {
347-
it('should not compile CloudWatch Resources when disabled', () => {
347+
it('should not compile CloudWatch Resources when logging not configured', () => {
348348
const api = new Api(given.appSyncConfig(), plugin);
349349
expect(api.compileCloudWatchLogGroup()).toMatchInlineSnapshot(
350350
`Object {}`,
351351
);
352352
});
353353

354+
it('should not compile CloudWatch Resources when logging is disabled', () => {
355+
const api = new Api(
356+
given.appSyncConfig({
357+
logging: {
358+
level: 'ERROR',
359+
retentionInDays: 14,
360+
enabled: false,
361+
},
362+
}),
363+
plugin,
364+
);
365+
expect(api.compileCloudWatchLogGroup()).toMatchInlineSnapshot(
366+
`Object {}`,
367+
);
368+
});
369+
354370
it('should compile CloudWatch Resources when enaabled', () => {
355371
const api = new Api(
356372
given.appSyncConfig({
357-
log: {
373+
logging: {
358374
level: 'ERROR',
359-
logRetentionInDays: 14,
375+
retentionInDays: 14,
360376
},
361377
}),
362378
plugin,

src/__tests__/validation/__snapshots__/base.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ exports[`Valdiation Domain Invalid should validate a Invalid Route 53 1`] = `
1414
`;
1515

1616
exports[`Valdiation Log Invalid should validate a Invalid 1`] = `
17-
"/log/level: must be \\"ALL\\", \\"ERROR\\" or \\"NONE\\"
18-
/log/logRetentionInDays: must be integer
19-
/log/excludeVerboseContent: must be boolean"
17+
"/logging/level: must be one of 'ALL', 'ERROR' or 'NONE'
18+
/logging/retentionInDays: must be integer
19+
/logging/excludeVerboseContent: must be boolean"
2020
`;
2121

2222
exports[`Valdiation Waf Invalid should validate a Invalid 1`] = `

src/__tests__/validation/base.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Valdiation', () => {
1818
name: 'Minimum',
1919
config: {
2020
...basicConfig,
21-
log: {
21+
logging: {
2222
level: 'ALL',
2323
},
2424
} as AppSyncConfigInput,
@@ -27,9 +27,9 @@ describe('Valdiation', () => {
2727
name: 'Full',
2828
config: {
2929
...basicConfig,
30-
log: {
30+
logging: {
3131
level: 'ALL',
32-
logRetentionInDays: 14,
32+
retentionInDays: 14,
3333
excludeVerboseContent: true,
3434
loggingRoleArn: { Ref: 'MyLogGorupArn' },
3535
},
@@ -50,9 +50,9 @@ describe('Valdiation', () => {
5050
name: 'Invalid',
5151
config: {
5252
...basicConfig,
53-
log: {
53+
logging: {
5454
level: 'FOO',
55-
logRetentionInDays: 'bar',
55+
retentionInDays: 'bar',
5656
excludeVerboseContent: 'buzz',
5757
loggingRoleArn: 123,
5858
},

src/resources/Api.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ export class Api {
9292
});
9393
}
9494

95-
if (this.config.log) {
95+
if (this.config.logging && this.config.logging.enabled !== false) {
9696
const logicalIdCloudWatchLogsRole =
9797
this.naming.getLogGroupRoleLogicalId();
9898
set(endpointResource, 'Properties.LogConfig', {
99-
CloudWatchLogsRoleArn: this.config.log.roleArn || {
99+
CloudWatchLogsRoleArn: this.config.logging.roleArn || {
100100
'Fn::GetAtt': [logicalIdCloudWatchLogsRole, 'Arn'],
101101
},
102-
FieldLogLevel: this.config.log.level,
103-
ExcludeVerboseContent: this.config.log.excludeVerboseContent,
102+
FieldLogLevel: this.config.logging.level,
103+
ExcludeVerboseContent: this.config.logging.excludeVerboseContent,
104104
});
105105
}
106106

@@ -112,7 +112,7 @@ export class Api {
112112
}
113113

114114
compileCloudWatchLogGroup(): CfnResources {
115-
if (!this.config.log) {
115+
if (!this.config.logging || this.config.logging.enabled === false) {
116116
return {};
117117
}
118118

@@ -132,7 +132,7 @@ export class Api {
132132
],
133133
},
134134
RetentionInDays:
135-
this.config.log.logRetentionInDays ||
135+
this.config.logging.retentionInDays ||
136136
this.plugin.serverless.service.provider.logRetentionInDays,
137137
},
138138
},

src/types/plugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type AppSyncConfig = {
2020
pipelineFunctions: Record<string, PipelineFunctionConfig>;
2121
substitutions?: Substitutions;
2222
xrayEnabled?: boolean;
23-
log?: LogConfig;
23+
logging?: LoggingConfig;
2424
caching?: CachingConfig;
2525
waf?: WafConfig;
2626
tags?: Record<string, string>;
@@ -296,10 +296,11 @@ export type VisibilityConfig = {
296296
sampledRequestsEnabled?: boolean;
297297
};
298298

299-
export type LogConfig = {
299+
export type LoggingConfig = {
300300
level: 'ERROR' | 'NONE' | 'ALL';
301+
enabled?: boolean;
301302
excludeVerboseContent?: boolean;
302-
logRetentionInDays?: number;
303+
retentionInDays?: number;
303304
roleArn?: string | IntrinsicFunction;
304305
};
305306

src/validation.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,17 +731,18 @@ export const appSyncSchema = {
731731
},
732732
},
733733
},
734-
log: {
734+
logging: {
735735
type: 'object',
736736
properties: {
737737
roleArn: { $ref: '#/definitions/stringOrIntrinsicFunction' },
738738
level: {
739739
type: 'string',
740740
enum: ['ALL', 'ERROR', 'NONE'],
741-
errorMessage: 'must be "ALL", "ERROR" or "NONE"',
741+
errorMessage: "must be one of 'ALL', 'ERROR' or 'NONE'",
742742
},
743-
logRetentionInDays: { type: 'integer' },
743+
retentionInDays: { type: 'integer' },
744744
excludeVerboseContent: { type: 'boolean' },
745+
enabled: { type: 'boolean' },
745746
},
746747
required: ['level'],
747748
},

0 commit comments

Comments
 (0)