From a2b60c3429e5d9fdcaafa69f00ebc4456fcb0986 Mon Sep 17 00:00:00 2001 From: Alex DeBrie Date: Thu, 27 Jun 2019 10:27:15 -0500 Subject: [PATCH] Add a logFormat property --- docs/providers/aws/events/apigateway.md | 5 +++-- .../compile/events/apiGateway/lib/hack/updateStage.js | 7 ++++--- .../compile/events/apiGateway/lib/hack/updateStage.test.js | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/providers/aws/events/apigateway.md b/docs/providers/aws/events/apigateway.md index 66cc90e2b..20394ae62 100644 --- a/docs/providers/aws/events/apigateway.md +++ b/docs/providers/aws/events/apigateway.md @@ -1495,12 +1495,13 @@ By default, API Gateway access logs will use the following format: 'requestId: $context.requestId, ip: $context.identity.sourceIp, caller: $context.identity.caller, user: $context.identity.user, requestTime: $context.requestTime, httpMethod: $context.httpMethod, resourcePath: $context.resourcePath, status: $context.status, protocol: $context.protocol, responseLength: $context.responseLength' ``` -You can specify your own [format for API Gateway Access Logs](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html#apigateway-cloudwatch-log-formats) by including your preferred string as a value to `restApi`: +You can specify your own [format for API Gateway Access Logs](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html#apigateway-cloudwatch-log-formats) by including your preferred string in the `logFormat` property: ```yml # serverless.yml provider: name: aws logs: - restApi: '{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp" }' + restApi: + logFormat: '{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp" }' ``` \ No newline at end of file diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/hack/updateStage.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/hack/updateStage.js index 5e8b2f1ce..afd9a2672 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/hack/updateStage.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/hack/updateStage.js @@ -186,9 +186,10 @@ function handleLogs() { const region = this.options.region; const logGroupName = `/aws/api-gateway/${service}-${stage}`; - const logFormat = provider.logs.restApi === true - ? defaultApiGatewayLogFormat - : provider.logs.restApi; + let logFormat = defaultApiGatewayLogFormat; + if (provider.logs.restApi.logFormat) { + logFormat = provider.logs.restApi.logFormat; + } const destinationArn = { op: 'replace', diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/hack/updateStage.test.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/hack/updateStage.test.js index 32f5db757..0d3d801f1 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/hack/updateStage.test.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/hack/updateStage.test.js @@ -338,7 +338,9 @@ describe('#updateStage()', () => { it('should update the stage with a custom APIGW log format if given', () => { context.state.service.provider.logs = { - restApi: 'requestId: $context.requestId', + restApi: { + logFormat: 'requestId: $context.requestId', + }, }; return updateStage.call(context).then(() => {