diff --git a/python/packages/core/agent_framework/_logging.py b/python/packages/core/agent_framework/_logging.py index 16385be6bc..012de28bf1 100644 --- a/python/packages/core/agent_framework/_logging.py +++ b/python/packages/core/agent_framework/_logging.py @@ -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: diff --git a/python/samples/getting_started/observability/README.md b/python/samples/getting_started/observability/README.md index 824a4c164b..8e306d509d 100644 --- a/python/samples/getting_started/observability/README.md +++ b/python/samples/getting_started/observability/README.md @@ -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: @@ -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 @@ -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. | diff --git a/python/samples/getting_started/observability/setup_observability_with_parameters.py b/python/samples/getting_started/observability/setup_observability_with_parameters.py index f152f7372c..2d28e00881 100644 --- a/python/samples/getting_started/observability/setup_observability_with_parameters.py +++ b/python/samples/getting_started/observability/setup_observability_with_parameters.py @@ -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 @@ -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(