[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for Sharded PostgreSQL](../index.md) > [Step-by-step guides](index.md) > Viewing cluster logs

# Viewing Sharded PostgreSQL cluster logs

Managed Service for Sharded PostgreSQL allows you to [get a cluster log snippet](#get-log) for the selected time period and [view logs in real time](#get-log-stream).

{% note info %}

Cluster logs are stored for 45 days.

{% endnote %}

## Getting a cluster log {#get-log}

{% list tabs group=instructions %}

- Management console {#console}

    1. Navigate to **Yandex Managed Service for Sharded&nbsp;PostgreSQL**.
    1. Click the name of your cluster and select the ![image](../../_assets/console-icons/receipt.svg) **Logs** tab.
    1. Specify a time period for the log entries you want to view: enter it manually or select in the calendar using the date input field.
    1. Optionally, add log filters in the row with the date input field:
       1. Select the `INFRA`, `ROUTER`, or `COORDINATOR` service type.
       1. Specify the hosts.
       1. Select the logging level.

    You will see a list of log entries for the chosen time period. To see details of a specific event, click its entry in the list.

    If the list is too long to display all entries at once, click **Load more** at the end of the list.

- 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.

    1. See the description of the CLI command for viewing cluster logs:

        ```bash
        yc managed-sharded-postgresql cluster list-logs --help
        ```

    1. Run the command below to get cluster logs. Note that this example does not show all available command options:

        ```bash
        yc managed-sharded-postgresql cluster list-logs <cluster_name_or_ID> \
           --limit <entry_number_limit> \
           --service-type <service_type> \
           --columns <list_of_data_columns> \
           --filter <entry_filtration_settings> \
           --since <time_range_start> \
           --until <time_range_end>
        ```

        Where:

        * `--limit`: limits on the number of entries to output.
        * `--service-type`: Target service type for log retrieval (`infra`, `router`, or `coordinator`).
        * `--columns`: List of output columns:
            * `hostname`: Host name.
            * `severity`: Logging level, e.g., `info`.
            * `message`: Component’s log message.
            * `contextual_data`: Additional information.

        * `--filter`: Record filter settings, e.g., `message.hostname='node1.mdb.yandexcloud.net'`.
        * `--since`: Left boundary of a time range in [RFC-3339](https://www.ietf.org/rfc/rfc3339.html), `HH:MM:SS` format or a time interval relative to the current time. Examples: `2006-01-02T15:04:05Z`, `15:04:05`, `2h`, `3h30m ago`.
        * `--until`: right boundary of a time range, the format is similar to that of `--since`.

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Call the [Cluster.ListLogs](../api-ref/Cluster/listLogs.md) method, e.g., via the following [cURL](https://curl.se/) request:

     ```bash
     curl \
       --request GET \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --url 'https://mdb.api.cloud.yandex.net/managed-spqr/v1/clusters/<cluster_ID>:logs' \
       --url-query serviceType=<service_type> \
       --url-query columnFilter=<column_1> \
       --url-query columnFilter=<column_2> \
       ... \
       --url-query columnFilter=<column_N> \
       --url-query fromTime=<time_range_start> \
       --url-query toTime=<time_range_end>
     ```

     Where:

     * `serviceType`: Target service type for log retrieval:

       * `INFRA`: `INFRA` host operations, which acts as both the router and coordinator.
       * `ROUTER`: Router operations.
       * `COORDINATOR`: Coordinator operations.

     * `columnFilter`: Name of the data column:

       * `hostname`: Host name.
       * `severity`: Logging level, e.g., `info`.
       * `message`: Component’s log message.
       * `contextual_data`: Additional information.

       You can specify only one column in the `columnFilter` parameter. If you want to filter logs by more than one column, provide a list of the columns in several parameters.

     * `fromTime`: Start of the time range in [RFC-3339](https://www.ietf.org/rfc/rfc3339.html) format, e.g., `2024-09-18T15:04:05Z`.
     * `toTime`: End of the time range in the same format as `fromTime`.

  1. Check the [server response](../api-ref/Cluster/listLogs.md#yandex.cloud.mdb.spqr.v1.ListClusterLogsResponse) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [ClusterService.ListLogs](../api-ref/grpc/Cluster/listLogs.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/spqr/v1/cluster_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "service_type": "<service_type>",
             "column_filter": [
               "<column_1>", "<column_2>", ..., "<column_N>"
             ],
             "from_time": "<time_range_start>",
             "to_time": "<time_range_end>"
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.spqr.v1.ClusterService.ListLogs
     ```

     Where:

     * `service_type`: Target service type for log retrieval:

       * `INFRA`: `INFRA` host operations, which acts as both the router and coordinator.
       * `ROUTER`: Router operations.
       * `COORDINATOR`: Coordinator operations.

     * `column_filter`: List of output columns:

       * `hostname`: Host name.
       * `severity`: Logging level, e.g., `info`.
       * `message`: Component’s log message.
       * `contextual_data`: Additional information.

       You can specify more than one column in the `column_filter` parameter if you want to filter logs by multiple columns.

     * `from_time`: Start of the time range in [RFC-3339](https://www.ietf.org/rfc/rfc3339.html) format, e.g., `2024-09-18T15:04:05Z`.
     * `to_time`: End of the time range in the same format as `from_time`.

  1. Check the [server response](../api-ref/grpc/Cluster/listLogs.md#yandex.cloud.mdb.spqr.v1.ListClusterLogsResponse) to make sure your request was successful.

{% endlist %}

## Streaming cluster logs {#get-log-stream}

This method allows you to stream cluster logs in real time.

{% list tabs group=instructions %}

- 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.

    To view cluster logs in real time, run this command:

    ```bash
    yc managed-sharded-postgresql cluster list-logs <cluster_name_or_ID> \
       --service-type <service_type> \
       --follow
    ```

    Where `--service-type` is the target service type for log retrieval (`infra`, `router`, or `coordinator`).

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Call the [Cluster.StreamLogs](../api-ref/Cluster/streamLogs.md) method, e.g., via the following [cURL](https://curl.se/) request:

     ```bash
     curl \
       --request GET \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --url 'https://mdb.api.cloud.yandex.net/managed-spqr/v1/clusters/<cluster_ID>:stream_logs' \
       --url-query serviceType=<service_type> \
       --url-query columnFilter=<column_1> \
       --url-query columnFilter=<column_2> \
       ... \
       --url-query columnFilter=<column_N> \
     ```

     Where:

     * `serviceType`: Target service type for log retrieval:

       * `INFRA`: `INFRA` host operations, which acts as both the router and coordinator.
       * `ROUTER`: Router operations.
       * `COORDINATOR`: Coordinator operations.

     * `columnFilter`: Name of the data column:

       * `hostname`: Host name.
       * `severity`: Logging level, e.g., `info`.
       * `message`: Component’s log message.
       * `contextual_data`: Additional information.

       You can specify only one column in the `columnFilter` parameter. If you want to filter logs by more than one column, provide a list of the columns in several parameters.

  1. Check the [server response](../api-ref/Cluster/streamLogs.md#yandex.cloud.mdb.spqr.v1.StreamLogRecord) to make sure your request was successful.

     Once you launch the command, it will continue running, displaying new logs in its output in real time.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [ClusterService.StreamLogs](../api-ref/grpc/Cluster/streamLogs.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/spqr/v1/cluster_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "service_type": "<service_type>",
             "column_filter": [
               "<column_1>", "<column_2>", ..., "<column_N>"
             ]
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.spqr.v1.ClusterService.StreamLogs
     ```

     Where:

     * `service_type`: Target service type for log retrieval:

       * `INFRA`: `INFRA` host operations, which acts as both the router and coordinator.
       * `ROUTER`: Router operations.
       * `COORDINATOR`: Coordinator operations.

     * `column_filter`: List of output columns:

       * `hostname`: Host name.
       * `severity`: Logging level, e.g., `info`.
       * `message`: Component’s log message.
       * `contextual_data`: Additional information.

       You can specify more than one column in the `column_filter` parameter if you want to filter logs by multiple columns.

  1. Check the [server response](../api-ref/grpc/Cluster/streamLogs.md#yandex.cloud.mdb.spqr.v1.StreamLogRecord) to make sure your request was successful.

     Once you launch the command, it will continue running, displaying new logs in its output in real time.

{% endlist %}