# How to setup "Custom Access Logging" for api gateway using serverless

**URL:** <https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288>\
**Category:** Serverless Framework\
**Created:** [December 10, 2017, 8:39am UTC](https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288 "2017-12-10T08:39:18Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![bill](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bill/32/1072_2.png) [@bill](https://forum.serverless.com/u/bill)\
**Post date:** [April 17, 2018, 7:03am UTC](https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288/5 "2018-04-17T07:03:09Z")

</div>

@cassiesu

I successfully did it via aws cli as work around.

aws cli supports `accessLogSettings`: [https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-stage.html](https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-stage.html)

Wrote simple script to enable this feature.

```
$ cat custom_access_log.sh

#!/usr/bin/env bash

if ["$#" -ne 3] || ! [-f "$3"]; then
  echo "Usage: $0 [rest_api_id] [stage_name] [config_file_name]" >&2
  exit 1
fi

rest_api_id=$1
stage_name=$2
cli_input_json=$3

aws apigateway update-stage \
--rest-api-id "${rest_api_id}" \
--stage-name "${stage_name}" \
--cli-input-json "file://${cli_input_json}"  

$ cat config-dev.json

{
  "patchOperations": [
    {
      "op": "replace",
      "path": "/accessLogSettings/format",
      "value": "{ \"requestId\": \"$context.requestId\", \"ip\": \"$context.identity.sourceIp\", \"caller\": \"$context.identity.caller\", \"requestTime\": \"$context.requestTimeEpoch\", \"httpMethod\": \"$context.httpMethod\", \"resourcePath\": \"$context.resourcePath\", \"status\": \"$context.status\", \"protocol\": \"$context.protocol\", \"responseLength\": \"$context.responseLength\" }"
    },
    {
      "op": "replace",
      "path": "/accessLogSettings/destinationArn",
      "value": "arn:aws:logs:ap-southeast-2:123456789012:log-group:/aws/apigateway/serverless-CustomAccessLogs"
    }
  ]
}

```

So I can run below command to enable custom access log on nominated APIG

```
aws apigateway update-stage --rest-api-id xxxdwgi337 --stage-name dev --cli-input-json file://config-dev.json

```

Reference: [https://forums.aws.amazon.com/message.jspa?messageID=715755](https://forums.aws.amazon.com/message.jspa?messageID=715755)

---

_[View the full topic](https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288)._
