Skip to content

Commit 1b4103d

Browse files
Python: fix for logging setup (#2371)
* fix for logging setup * typo fixes * fix text
1 parent 2a4802e commit 1b4103d

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

python/packages/core/agent_framework/_logging.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
from .exceptions import AgentFrameworkException
66

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

12-
__all__ = ["get_logger"]
9+
10+
def setup_logging() -> None:
11+
"""Setup the logging configuration for the agent framework."""
12+
logging.basicConfig(
13+
format="[%(asctime)s - %(pathname)s:%(lineno)d - %(levelname)s] %(message)s",
14+
datefmt="%Y-%m-%d %H:%M:%S",
15+
)
1316

1417

1518
def get_logger(name: str = "agent_framework") -> logging.Logger:

python/samples/getting_started/observability/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ from agent_framework.observability import setup_observability
6767
setup_observability()
6868
```
6969

70+
Agent Framework also has an opinionated logging format, which you can setup using:
71+
```python
72+
from agent_framework import setup_logging
73+
74+
setup_logging()
75+
```
76+
7077
#### Environment variables for `setup_observability()`
7178

7279
The `setup_observability()` function will look for the following environment variables to determine how to setup the exporters and providers:
@@ -89,6 +96,13 @@ setup_observability(exporters=[exporter])
8996
> 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`.
9097
9198
#### Logging
99+
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.
100+
101+
```python
102+
from agent_framework import setup_logging
103+
104+
setup_logging()
105+
```
92106
You can control at what level logging happens and thus what logs get exported, you can do this, by adding this:
93107

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

106120
| Sample | Description |
107121
|--------|-------------|
108-
| [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. |
122+
| [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. |
109123
| [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. |
110124
| [agent_observability.py](./agent_observability.py) | A simple example showing how to setup telemetry for an agentic application. |
111125
| [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. |

python/samples/getting_started/observability/setup_observability_with_parameters.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from random import randint
77
from typing import TYPE_CHECKING, Annotated, Literal
88

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

101+
# Setup the logging with the more complete format
102+
setup_logging()
101103
# This will enable tracing and create the necessary tracing, logging and metrics providers
102104
# based on the provided parameters.
103105
setup_observability(

0 commit comments

Comments
 (0)