# Debugging

Debugging is a process that allows developers to fix log messages or variables, for example. The debugging method allows **Functions** users to generate logs as if they were using a browser's JavaScript.

---

## What it is

The debugging process in **Functions** has the purpose of promoting better control over the application to identify errors. It can also be used to assist in the development and debugging of a function, resulting in greater observability.

---

## How it works

To debug codes, you must create a new [function](/en/documentation/products/build/applications/functions/) that will use the **log** method. This method prints to the console a message previously sent by a parameter to the function.

### Creating an function

Follow these steps to create an function that logs messages:

1. Access [Azion Console](https://console.azion.com/) with your login credentials.
2. In the **Products Menu**, navigate to **Libraries** > **Functions**.
3. Click on **+ Function** to create a new function.
4. Choose a name for your function.
5. In the **Code** tab, paste the following code:

:::note
The following snippet shows a simple implementation as a example for the purpose of this guide. When creating your function, make sure it collects and logs the data you need.
:::

```js
   async function handleRequest(request) {
       console.log("Hello World");

       return new Response("Checking console output.", {
           status: 200,
       });
   }
   addEventListener("fetch", (event) => {
       event.respondWith(handleRequest(event.request));
   });
```

6. Click the **Save** button to finish the process.

Now that you've created the function, you must configure your **application** to run the function before the logs are accessible.

---

### Instantiating the function

To instantiate the newly created function in your application, follow these steps:

1. On the **Products Menu**, navigate to **Build** > **Applications**.
2. Click on the application where you want to add the function or [create a new one](/en/documentation/products/build/applications/first-steps/).
3. Go to the **Functions Instances** tab and click **+ Function Instance**.
4. Name your function instance.
5. Select the newly created function from the dropdown menu.
6. Click the **Save** button.

Now your function is instantiated and ready to be used in your application. You can configure the execution conditions and behaviors using the [Rules Engine](/en/documentation/products/build/applications/rules-engine/):

1. While still configuring your application, navigate to the **Rules Engine** tab and click the **+ Rule** button.
2. Give your rule a descriptive name.
3. Select **Request Phase**.
4. In the **Criteria** section, configure the criteria as follows:
   - If `${uri}` *starts with* `/`
5. In the **Behaviors** section, select **Run Function** from the dropdown menu.
6. Choose the instance of your function.
7. Click the **Save** button.

Now the function will be executed according to the rule and its logs are ready to be captured by **Data Stream**.

---

### Setting up a Data Stream

1. On the **Products Menu**, go to **Observe** > **Data Stream**.
2. Select an existing stream or create a new one by clicking the **+ Stream** button.
3. In the *Name** field, give your stream a unique and easy-to-remember name.
4. In the **Data Settings** section, select **Functions** as the source.
5. Select the **Functions Event Collector** template. In the **Data Set** box, you'll see the preset of variables:

```json
{
	"time": "$time",
	"client": "$client",
	"configuration": "$global_id",
	"edgeFunctionID": "$edge_function_id",
	"requestID": "$request_id",
	"messageSource": "$message_source",
	"logLevel": "$log_level",
	"logMessage": "$log_message"
}
```

| **Variable**      | **Definition**                                              |
| ----------------- | ----------------------------------------------------------- |
| $time             | Date and time of the request.                               |
| $client           | Unique Azion customer identifier.                           |
| $global_id        | Settings identification.                                    |
| $edge_function_id | Function identifier.                                   | 
| $request_id       | Request identifier.                                         |
| $message_source   | The source of the message.                                  |
| $log_level        | Level of the log created (ERROR, WARN, INFO, DEBUG, TRACE). |
| $log_message      | Message used on the log when the function is requested.     |

6. In the **Destination** section, leave the **Connector** as `Standard HTTP/HTTPS POST` and enter the **URL** which will receive the collected data.

7. Click the **Save** button to finish the process.

Now your data stream will collect logs from functions and send them to the specified URL.

Check the [documentation](/en/documentation/products/observe/data-stream/) to learn more about **Data Stream** and its configurations. You can also use **Real-Time Events** to observe and analyze the generated logs. 

1. In the **Products Menu**, navigate to **Observe** > **Real-Time Events**.
2. Select the **Functions Console** tab to see the logs originated from functions. Use the filters to specify a detailed query.
3. On the **Data Stream** tab, you can see the logs of data sent to various endpoints via **Data Stream**.
4. Click on any item to see details of the data sent.

Read the [Real-Time Events documentation](/en/documentation/products/observe/real-time-events/) to learn more.