[Yandex Cloud documentation](../../index.md) > [Yandex Cloud Logging](../index.md) > [Tutorials](index.md) > Transferring logs through Unified Agent HTTP input to Cloud Logging

# Transferring logs through Unified Agent HTTP input to Cloud Logging

# Transferring logs through Unified Agent HTTP input to Yandex Cloud Logging

[Yandex Unified Agent](../../monitoring/concepts/data-collection/unified-agent/index.md) allows you to receive and send user application logs to [Yandex Cloud Logging](../index.md).

In this tutorial, you will configure log transfer from a test Python application. The application will send logs to Unified Agent [http input](../../monitoring/concepts/data-collection/unified-agent/inputs.md#http_input). Unified Agent will send the received logs via the [yc_logs](../../monitoring/concepts/data-collection/unified-agent/outputs.md#yc_logs_output) output to the Cloud Logging `default` log group.

To set up log transfer:

1. [Get your cloud ready](#before-begin).
1. [Install and configure Yandex Unified Agent](#configure-ua).
1. [Create and run a log-generating application](#generate-logs).
1. [View the logs](#read-logs).

If you no longer need the resources you created, [delete them](#clear-out).

## Get your cloud ready {#before-begin}

Sign up for Yandex Cloud and create a [billing account](../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../billing/quickstart/index.md) and [link](../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../resource-manager/concepts/resources-hierarchy.md).

### Required paid resources {#paid-resources}

1. Fee for continuously running VMs (see [Yandex Compute Cloud pricing](../../compute/pricing.md)).

1. Fee for logging and log storage in a log group (see [Yandex Cloud Logging pricing](../pricing.md)).

## Set up your infrastructure {#infrastructure-prepare}

### Create a service account {#sa-create}

1. [Create](../../iam/operations/sa/create.md) a service account named `sa-logger` in the folder you want to write logs to.
1. [Assign](../../iam/operations/roles/grant.md) the `logging.writer` [role](../security/index.md#logging-writer) to the service account.

### Create a VM {#vm-create}

1. [Create a VM](../../compute/operations/vm-create/create-linux-vm.md) from a public [Ubuntu 24.04](https://yandex.cloud/en/marketplace/products/yc/ubuntu-24-04-lts) image.
   
   Under **Access**, specify `sa-logger`.
   
1. [Connect to the VM](../../compute/operations/vm-connect/ssh.md#vm-connect) over SSH.

## Install and configure Unified Agent {#configure-ua}

1. Download the latest deb package:

      ```bash
      ubuntu_name="ubuntu-22.04-jammy" ua_version=$(curl --silent https://storage.yandexcloud.net/yc-unified-agent/latest-version) bash -c 'curl --silent --remote-name https://storage.yandexcloud.net/yc-unified-agent/releases/${ua_version}/deb/${ubuntu_name}/yandex-unified-agent_${ua_version}_amd64.deb'
      ```
1. Check the deb package version using the `ls` command.
   
1. Install Unified Agent from the deb package by specifying its version:

   ```bash
   sudo dpkg -i yandex-unified-agent_24.09.03_amd64.deb
   ```
   
   You can find other installation methods in [Installing and updating Yandex Unified Agent](../../monitoring/concepts/data-collection/unified-agent/installation.md). 

1. Check that Unified Agent is running:

   ```bash
   sudo systemctl status unified-agent.service
   ```

1. Open the Unified Agent configuration file:
   
   ```bash
   sudo nano /etc/yandex/unified_agent/config.yml
   ```

1. Add the following configuration to the file to receive and send logs:

   ```yaml
   status:
     port: "16241"

   routes:
      - input:
         plugin: http
         config:
            port: 22132
        channel:
         output:
            plugin: yc_logs
            config:
               folder_id: "b1grj7grr1kn********"
               iam:
                  cloud_meta: {}
   
   import:
   - /etc/yandex/unified_agent/conf.d/*.yml
   ```

   Where `folder_id` is the ID of the folder you want to write logs to.

1. Make sure the configuration file is correct. The command should output the contents of the file:

   ```bash
   unified_agent check-config -c /etc/yandex/unified_agent/config.yml
   ```

1. Restart Unified Agent:

   ```bash
   sudo systemctl restart unified-agent.service
   ```

1. Check the Unified Agent status:

   ```bash
   sudo systemctl status unified-agent.service
   ```

## Create and run a log-generating application {#generate-logs}

1. Create a file named `logtest.py`:

   ```py
   import time
   import requests
   import random

   # Possible URLs for requests
   urls = [
      '/',
      '/admin',
      '/hello',
      '/docs',
      '/api/resource1',
      '/api/resource2',
      '/api/resource3'
   ]

   # Unified Agent HTTP input configuration
   unified_agent_url = 'http://51.250.98.18:22132/write'

   # Possible response codes and their probabilities
   response_codes = [200, 201, 400, 404, 500]
   response_weights = [0.7, 0.1, 0.1, 0.05, 0.05]

   # Generating and sending logs to HTTP input
   def generate_and_send_logs():
      while True:
         url = random.choice(urls)
         status_code = random.choices(response_codes, response_weights)[0]
         log_message = f"Requested {url}, received status code {status_code}"
         print(log_message)
         
         # Sending log to Unified Agent HTTP input
         send_logs_to_http(log_message)
         
         # Sending logs every 5 seconds
         time.sleep(5)

   # Sending logs to HTTP input
   def send_logs_to_http(log_message):
      headers = {"Content-Type": "text/plain"}
      response = requests.post(unified_agent_url, headers=headers, data=log_message)
      if response.status_code == 200:
         print("Log sent successfully")
      else:
         print(f"Failed to send log: {response.status_code}")

   if __name__ == "__main__":
      generate_and_send_logs()
   ```

   Where `unified_agent_url` is the public IP address of the VM with Unified Agent.

   By default, Unified Agent accepts data on all interfaces. Therefore, you can specify a public IP address even if the log source is on the same VM. If there is no public address, put `localhost`.

1. Upgrade the versions of the installed packages:

    ```bash
    sudo apt-get update
    ```

1. Install Python:

   ```bash
   sudo apt install python3
   ```

1. Run the application:
   ```bash
   python3 logtest.py
   ```

## View the logs {#read-logs}

{% list tabs group=instructions %}

- Management console {#console}

    1. In the [management console](https://console.yandex.cloud), go to the folder you specified in the Yandex Unified Agent settings.
    1. Navigate to **Cloud Logging**.
    1. Select the `default` log group.
    1. Navigate to the **Logs** tab.
    1. The page that opens will show the records.

- 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 records in the log group, run this command:
    ```
    yc logging read --folder-id=<folder_ID>
    ```

    Where `--folder-id` is the folder ID specified in the Yandex Unified Agent settings.

- API {#api}

    To view log group records, use the [LogReadingService/Read](../api-ref/grpc/LogReading/read.md) gRPC API call.

{% endlist %}

## Delete the resources you created {#delete-resources}

If you no longer need the resources you created, delete them:

1. [Delete the VM](../../compute/operations/vm-control/vm-delete.md).
1. [Delete the log group](../operations/delete-group.md).