Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions python/packages/core/agent_framework/_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

from .exceptions import AgentFrameworkException

logging.basicConfig(
format="[%(asctime)s - %(pathname)s:%(lineno)d - %(levelname)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
__all__ = ["get_logger", "setup_logging"]

__all__ = ["get_logger"]

def setup_logging() -> None:
"""Setup the logging configuration for the agent framework."""
logging.basicConfig(
format="[%(asctime)s - %(pathname)s:%(lineno)d - %(levelname)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)


def get_logger(name: str = "agent_framework") -> logging.Logger:
Expand Down
16 changes: 15 additions & 1 deletion python/samples/getting_started/observability/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ from agent_framework.observability import setup_observability
setup_observability()
```

Agent Framework also has an opinionated logging format, which you can setup using:
```python
from agent_framework import setup_logging

setup_logging()
```

#### Environment variables for `setup_observability()`

The `setup_observability()` function will look for the following environment variables to determine how to setup the exporters and providers:
Expand All @@ -89,6 +96,13 @@ setup_observability(exporters=[exporter])
> Using this method implicitly enables telemetry, so you do not need to set the `ENABLE_OTEL` environment variable. You can still set `ENABLE_SENSITIVE_DATA` to control whether sensitive data is included in the telemetry, or call the `setup_observability()` function with the `enable_sensitive_data` parameter set to `True`.

#### Logging
Agent Framework has a built-in logging configuration that works well with telemetry. It sets the format to a standard format that includes timestamp, pathname, line number, and log level. You can use that by calling the `setup_logging()` function from the `agent_framework` module.

```python
from agent_framework import setup_logging

setup_logging()
```
You can control at what level logging happens and thus what logs get exported, you can do this, by adding this:

```python
Expand All @@ -105,7 +119,7 @@ This folder contains different samples demonstrating how to use telemetry in var

| Sample | Description |
|--------|-------------|
| [setup_observability_with_parameters.py](./setup_observability_with_parameters.py) | A simple example showing how to setup telemetry by passing in parameters to the `setup_observability()` function. |
| [setup_observability_with_parameters.py](./setup_observability_with_parameters.py) | A simple example showing how to setup telemetry by passing in parameters to the `setup_observability()` function. This sample also uses the `setup_logging()` function to configure logging. |
| [setup_observability_with_env_var.py](./setup_observability_with_env_var.py) | A simple example showing how to setup telemetry with the `setup_observability()` function using environment variables. |
| [agent_observability.py](./agent_observability.py) | A simple example showing how to setup telemetry for an agentic application. |
| [azure_ai_agent_observability.py](./azure_ai_agent_observability.py) | A simple example showing how to setup telemetry for an agentic application with an Azure AI project. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from random import randint
from typing import TYPE_CHECKING, Annotated, Literal

from agent_framework import ai_function
from agent_framework import ai_function, setup_logging
from agent_framework.observability import get_tracer, setup_observability
from agent_framework.openai import OpenAIResponsesClient
from opentelemetry import trace
Expand Down Expand Up @@ -98,6 +98,8 @@ async def run_ai_function() -> None:
async def main(scenario: Literal["chat_client", "chat_client_stream", "ai_function", "all"] = "all"):
"""Run the selected scenario(s)."""

# Setup the logging with the more complete format
setup_logging()
# This will enable tracing and create the necessary tracing, logging and metrics providers
# based on the provided parameters.
setup_observability(
Expand Down
Loading