Add a logFormat property

This commit is contained in:
Alex DeBrie 2019-06-27 10:27:15 -05:00
parent 8d629552dd
commit a2b60c3429
3 changed files with 10 additions and 6 deletions

View File

@ -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" }'
```

View File

@ -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',

View File

@ -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(() => {