[Yandex Cloud documentation](../../index.md) > [Yandex API Gateway](../index.md) > [Step-by-step guides](index.md) > Managing an API gateway > Managing logs > Writing logs

# Writing to the execution log in API Gateway

{% note info %}

Logging is not free of charge. For more information, see [Yandex Cloud Logging pricing](../../logging/pricing.md).

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}
    
    1. In the [management console](https://console.yandex.cloud), navigate to the [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) containing the API gateway.
    1. Navigate to **API Gateway**.
    1. Select the API gateway for which you want to configure logging.
    1. At the top of the page, click ![image](../../_assets/console-icons/pencil.svg) **Edit**.
    1. Under **Logging**:

        1. Enable **Write logs**.
        1. In the **Destination** field, select:
                  
           * `Folder`: To write logs to the default [log group](../../logging/concepts/log-group.md) for the folder containing the API gateway.
           * `Log group`: To write logs to a custom log group.
             
             Select the log group to write logs to or [create](../../logging/operations/create-group.md) a new one.
        
        1. Optionally, select the minimum logging level.

    1. Click **Save**. 
    
    If you set the minimum logging level, logs of that level and higher will be written to the execution log. With no minimum logging level set, all API gateway logs will be written to the execution log.

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    ## Logging destination {#destination}

    If a custom [log group](../../logging/concepts/log-group.md) is not specified in the API gateway parameters or logging is not turned off, the API gateway automatically writes all logs to the default log group for the [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) it resides in.

    For logging to another folder's default log group, provide that folder's ID in the `--log-folder-id` parameter when [creating](api-gw-create.md) or [updating](api-gw-update.md) an API gateway. The [account](../../iam/concepts/users/accounts.md) used to run the command must have the `logging.editor` [role](../../logging/security/index.md#logging-editor) or higher for that folder.

    For logging to a custom log group, provide that log group's ID in the `--log-group-id` parameter when creating or updating an API gateway. The log group may reside in a different folder. The account used to run the command must have the `logging.editor` role or higher for that folder.

    ## Minimum logging level {#log-level}

    To set the minimum logging level, provide it in the `--min-log-level` parameter when creating or updating an API gateway. 

    If you set the minimum logging level, logs of that level and higher will be written to the execution log. With no minimum logging level set, all API gateway logs will be written to the execution log.

    ## Disabling logging {#disabled}

    To disable logging, set the `--no-logging` parameter when creating or updating an API gateway.

    ## Command example {#example}

    To write logs to a custom log group, run this command:

    ```bash
    yc serverless api-gateway update <API_gateway_name_or_ID> \
      --log-group-id <log_group_ID> \
      --min-log-level <minimum_logging_level>
    ```

    Where:
    * `--log-group-id`: ID of the log group to write logs to.
    * `--min-log-level`: Minimum logging level. The available levels are `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, and `FATAL`. This is an optional setting.

    Result:

    ```text
    id: d5dr8k465604********
    folder_id: b1g3f9i71bpm********
    created_at: "2024-01-26T09:18:55.985Z"
    name: example_gateway
    status: ACTIVE
    domain: d5dm1lba80md********.i9******.apigw.yandexcloud.net
    log_group_id: ckgsh1kdbvj1********
    connectivity: {}
    log_options:
      log_group_id: e23u2vn449av********
      min_level: ERROR
    ```

- Terraform {#tf}
    
    With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
    
    Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
    
    For more information about the provider resources, see the guides on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../terraform/index.md).
    
    If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
    
    
    To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../terraform/authentication.md) using the appropriate method.

    ## Logging destination {#destination}

    If a custom [log group](../../logging/concepts/log-group.md) is not specified in the API gateway parameters or logging is not turned off, the API gateway automatically writes all logs to the default log group for the [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) it resides in.

    For logging to another folder's default log group, provide that folder's ID under `log_options` in the `folder_id` parameter when [creating](api-gw-create.md) or [updating](api-gw-update.md) an API gateway. The [account](../../iam/concepts/users/accounts.md) used to run the command must have the `logging.editor` [role](../../logging/security/index.md#logging-editor) or higher for the folder.

    For logging to a custom log group, provide that log group's ID under `log_options` in the `log_group_id` parameter when creating or updating an API gateway. The log group may reside in a different folder. The account used to run the command must have the `logging.editor` role or higher for that folder.

    ## Minimum logging level {#log-level}

    To set the minimum logging level, provide it under `log_options` in the `min_level` parameter when creating or updating an API gateway. 

    If you set the minimum logging level, logs of that level and higher will be written to the execution log. With no minimum logging level set, all API gateway logs will be written to the execution log.

    ## Disabling logging {#disabled}

    To disable logging, under `log_options`, set the `disabled` parameter to `true` when creating or updating an API gateway.

    ## Example {#example}

    To write logs to a custom log group:

    1. Open the Terraform configuration file and add the `log_options` section to the `yandex_api_gateway` resource description:

        Here is an example of the configuration file structure:
        
        ```hcl
        resource "yandex_api_gateway" "<API_gateway_name>" {
          ...
          log_options {
            folder_id = "<folder_ID>"
            min_level = "<minimum_logging_level>"
          }
          ...
        }
        ```

        Where:
        * `folder_id`: Folder ID.
        * `min_level`: Minimum logging level. The available levels are `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, and `FATAL`. This is an optional setting.

        For more on the properties of the `yandex_api_gateway` resource in Terraform, see [this provider guide](../../terraform/resources/api_gateway.md).
    
    1. Validate your configuration using this command:

        ```bash
        terraform validate
        ```

        If the configuration is valid, you will get this message:

        ```text
        Success! The configuration is valid.
        ```

    1. Run this command:

        ```bash
        terraform plan
        ```

        You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
    
    1. Apply the configuration changes:

        ```bash
        terraform apply
        ```

    1. Type `yes` and press **Enter** to confirm the changes.

- API {#api}

    For logging to the execution log in Yandex API Gateway, use the [update](../apigateway/api-ref/ApiGateway/update.md) REST API method for the [ApiGateway](../apigateway/api-ref/ApiGateway/index.md) resource or the [ApiGatewayService/Update](../apigateway/api-ref/grpc/ApiGateway/update.md) gRPC API call.

{% endlist %}