> ## Documentation Index
> Fetch the complete documentation index at: https://agent-observability-docs.splunk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Log Using the Log Decorator

> Learn how to use the Splunk Agent Observability log decorator to log functions to traces

## Overview

This guide shows you how to log spans to Splunk Agent Observability using the `@log` decorator in Python.

In this guide you will:

1. [Set up a project with Splunk Agent Observability](#install-dependencies)

1) [Create a basic app to call OpenAI](#create-the-basic-app-to-call-openai)
2) [Add logging with the log decorator](#add-simple-logging-with-the-splunk-agent-observability-log-decorator-or-wrapper)

## Before you start

To complete this how-to, you will need:

* An [OpenAI API key](https://openai.com/api/)
* A [Splunk Agent Observability project](/concepts/projects.md) configured
* Your [Splunk Agent Observability API key](/references/faqs/find-keys.md#splunk-ao-api-key)

## Install dependencies

To use Splunk Agent Observability, you need to install some package dependencies, and configure environment variables.

**1. Install Required Dependencies**

Install the required dependencies for your app. If you are using Python, create a virtual environment using your preferred method, then install dependencies inside that environment:

```bash Python theme={null}
pip install "splunk-ao[openai]" python-dotenv
```

**2. Create a .env file, and add the following values**

## Create the basic app to call OpenAI

**1. Create a file for your application called app.py or app.ts.**

**2. Add the following code to call OpenAI to ask a question**

```python Python theme={null}
import os
import asyncio
import openai
from dotenv import load_dotenv

load_dotenv()

client = openai.AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

async def prompt_open_ai(prompt: str) -> str:
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}],
    )
    return response.choices[0].message.content.strip()

async def main():
    prompt = "Explain the following topic succinctly: Newton's First Law"
    response = await prompt_open_ai(prompt)
    print(response)

if __name__ == "__main__":
    asyncio.run(main())
```

**3. Run the app to ensure everything is working**

```bash Python theme={null}
python app.py
```

You should see a description of Newton's first law.

```output theme={null}
(.venv) ➜  python app.py
Newton's First Law, also known as the Law of Inertia, states that an object
at rest will stay at rest and an object in motion will stay in motion with
the same speed and in the same direction, unless acted upon by an
unbalanced force. In simpler terms, it means that an object will keep doing
what it's currently doing until a force makes it do something different.
This law is fundamental to understanding motion and forces as it pertains
to physics.
```

## Add simple logging with the Splunk Agent Observability log decorator or wrapper

Splunk Agent Observability has a `@log` decorator in Python that logs function calls as spans. If these decorated or wrapped calls are called whilst there is an active trace, they are added to that trace. If there is no active trace, a new one is created for this span.

In this guide, you will be adding the decorator or wrapper to log the function that calls OpenAI.

**1. Import the log decorator**

At the top of your file, add an import for the log decorator:

```python Python theme={null}
from splunk_ao import log
```

**2. Decorate or wrap the function**

Update the function definition to include the decorator or wrapper:

```python Python theme={null}
@log(span_type="llm", name="OpenAI GPT-4o-mini")
async def prompt_open_ai(prompt: str) -> str:
```

This will log the function as an LLM span using the span type parameter. You can read more about these in our [span types documentation](/sdk-api/logging/log-decorator#span-types).

This also sets the name of the span to "OpenAI GPT-4o-mini".

**3. Run the app**

```bash Python theme={null}
python app.py
```

When the app runs, the span will be logged automatically, with the input as the `prompt`, the output as the returned `response`. The duration will also be logged.

**4. View the logged trace**

From your Splunk Agent Observability homepage, open the Agent Stream for your project. You will see a trace with a single span containing the logged function call.

Select the trace to see a detailed view:

![A trace with an input, OpenAI span, and an output](https://mintcdn.com/agent-observability-docs/zzyRt-AOzuOsu2Cf/images/howto/basic-logging-with-decorator-trace-graph.png?fit=max\&auto=format\&n=zzyRt-AOzuOsu2Cf\&q=85\&s=b6aa85aa7666797e86964182c50db9f0)

Select the OpenAI span, then select the **Evaluators** tab to see the latency.

Your logging is now set up! You are ready to configure evaluators for your project.

## See also

* [Configure evaluators](/concepts/evaluators/overview.md)
* [Agent Streams](/sdk-api/logging/logging-basics.md)
* [The `@log` decorator](/sdk-api/logging/log-decorator)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.