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

# Logging

> Learn about using the Splunk Agent Observability CrewAI logging integration to log agent traces

The Splunk Agent Observability CrewAI integration allows you to automatically log all CrewAI interactions with LLMs, including prompts, responses, and performance evaluators. The Splunk Agent Observability SDK has a custom event listener that listens to CrewAI events, and logs these to Splunk Agent Observability.

> **[CrewAIEventListener](/sdk-api/python/reference/handlers/crewai/handler.md)**
>
> The Python Splunk Agent Observability CrewAI SDK reference.

## Basic usage

The integration is based on the `CrewAIEventListener` class, which implements CrewAI's event listener. To use it, create an instance of the listener before you kick off your crew, for example in your `run` function. When this listener is created, it automatically registers event handlers to capture the relevant events.

```python Python theme={null}
from splunk_ao.handlers.crewai.handler import CrewAIEventListener

def run():
    # Create the CrewAI Event Listener
    CrewAIEventListener()

    # Run the crew
    inputs = {
        'topic': 'AI Agents'
    }

    MyCrew().crew().kickoff(inputs=inputs)
```

The `CrewAIEventListener` captures various CrewAI events, including:

* Crew Kickoff events
* Agent events
* Task events
* Tool usage
* LLM calls

For each of these events, the callback logs relevant information to Splunk Agent Observability, such as:

* Input prompts and messages
* Output responses
* Model information
* Timing data
* Token usage
* Error information (if any)

The `CrewAIEventListener` automatically handles nested crews and agents, creating a hierarchical trace that reflects the structure of your CrewAI application.

## Use a custom logger

When initializing the `CrewAIEventListener`, you can optionally specify a Splunk Agent Observability logger instance, either by creating a new logger, or by using the current logger from the Splunk Agent Observability context:

```python Python theme={null}
from splunk_ao.handlers.crewai.handler import CrewAIEventListener

def run():
    # Create a custom logger
    logger = SplunkAOLogger(project="my-project", agent_stream="my-agent-stream")

    # Create the CrewAI Event Listener
    CrewAIEventListener(
        splunk_ao_logger=logger,       # Optional custom logger
        start_new_trace=True,        # Start a new trace for each crew
        flush_on_crew_completed=True # Flush traces when the crew completes
    )

    # Run the crew
    inputs = {
        'topic': 'AI Agents'
    }

    MyCrew().crew().kickoff(inputs=inputs)
```

This is particularly useful if you want to call your CrewAI code from inside a function [decorated with the `log` decorator](/sdk-api/logging/log-decorator/log-decorator), or from inside an [experiment](/sdk-api/third-party-integrations/crewai/experiments.md).

### Session and trace support

Every time you kick off a crew, a new session and trace is created. If you want to manually manage sessions or traces, you can do this using by passing a Splunk Agent Observability logger instance to the listener.

To add the crew kickoff as a new trace to an existing session, create the session first using the logger instance that was used to create the listener:

```python Python theme={null}
from splunk_ao.handlers.crewai.handler import CrewAIEventListener

def run():
    # Create a custom logger
    logger = SplunkAOLogger(project="my-project", agent_stream="my-agent-stream")

    # Create the CrewAI Event Listener
    CrewAIEventListener(
        splunk_ao_logger=logger
    )

    # Create a new session
    logger.start_session(name="My new session")

    # Run the crew
    inputs = {
        'topic': 'AI Agents'
    }

    MyCrew().crew().kickoff(inputs=inputs)
```

To add the crew kickoff to an existing trace, ensure the trace is started, and set the `start_new_trace` parameter to `False`.

```python Python theme={null}
from splunk_ao.handlers.crewai.handler import CrewAIEventListener

def run():
    # Create a custom logger
    logger = SplunkAOLogger(project="my-project", agent_stream="my-agent-stream")

    # Create the CrewAI Event Listener
    CrewAIEventListener(
        splunk_ao_logger=logger,
        start_new_trace=False
    )

    # Create a new session
    logger.start_session(name="My new session")

    # Add a trace and a span
    logger.add_trace("My trace")
    logger.add_workflow_span("Crew workflow")

    # Run the crew
    inputs = {
        'topic': 'AI Agents'
    }

    MyCrew().crew().kickoff(inputs=inputs)
```

## Next steps

### Basic logging components

> **[Splunk Agent Observability logger](/sdk-api/logging/splunk-ao-logger.md)**
>
> Log with full control over sessions, traces, and spans using the Splunk Agent Observability logger.

> **[Log decorator](/sdk-api/logging/log-decorator/log-decorator.md)**
>
> Quickly add logging to your code with the log decorator and wrapper.

> **[Splunk Agent Observability context](/sdk-api/logging/splunk-ao-context.md)**
>
> Manage logging using the Splunk Agent Observability context manager.

### How-to guides

> **[Add Splunk Agent Observability to a CrewAI Application](/how-to-guides/third-party-integrations/add-splunk-ao-to-crewai/add-splunk-ao-to-crewai.md)**
>
> Learn how to add Splunk Agent Observability to an existing CrewAI application


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