mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-07-27 06:28:50 +00:00
# What does this PR do? currently `print` is being used with custom formatting to achieve telemetry output in the console_span_processor This causes telemetry not to show up in log files when using `LLAMA_STACK_LOG_FILE`. During testing it looks like telemetry is not being captured when it is switch to using Rich formatting with the logger and then strip the formatting off when a log file is being used so the formatting looks normal ## Test Plan before: console: <img width="967" height="127" alt="Screenshot 2025-07-21 at 4 02 15 PM" src="https://github.com/user-attachments/assets/b09518cc-9d38-4970-9877-70e2c41fcbb5" /> log file (no telemetry): ``` 2025-07-21 16:01:32,481 llama_stack.providers.remote.inference.ollama.ollama:117 inference: checking connectivity to Ollama at `http://localhost:11434`... 2025-07-21 16:01:34,779 opentelemetry.trace:537 uncategorized: Overriding of current TracerProvider is not allowed 2025-07-21 16:01:35,083 __main__:587 server: Listening on ['::', '0.0.0.0']:8321 2025-07-21 16:01:35,091 uvicorn.error:84 uncategorized: Started server process [68679] 2025-07-21 16:01:35,091 uvicorn.error:48 uncategorized: Waiting for application startup. 2025-07-21 16:01:35,092 __main__:163 server: Starting up 2025-07-21 16:01:35,092 uvicorn.error:62 uncategorized: Application startup complete. 2025-07-21 16:01:35,092 uvicorn.error:216 uncategorized: Uvicorn running on http://['::', '0.0.0.0']:8321 (Press CTRL+C to quit) 2025-07-21 16:01:37,167 uvicorn.access:473 uncategorized: 127.0.0.1:53145 - "POST /v1/openai/v1/chat/completions HTTP/1.1" 200 ``` after: console: <img width="797" height="165" alt="Screenshot 2025-07-22 at 3 28 44 PM" src="https://github.com/user-attachments/assets/44d40e3b-6502-439d-9ea5-38058b289962" /> log file: ``` 2025-07-21 15:59:51,481 llama_stack.providers.remote.inference.ollama.ollama:117 inference: checking connectivity to Ollama at `http://localhost:11434`... 2025-07-21 15:59:53,801 opentelemetry.trace:537 uncategorized: Overriding of current TracerProvider is not allowed 2025-07-21 15:59:54,059 __main__:587 server: Listening on ['::', '0.0.0.0']:8321 2025-07-21 15:59:54,066 uvicorn.error:84 uncategorized: Started server process [68578] 2025-07-21 15:59:54,067 uvicorn.error:48 uncategorized: Waiting for application startup. 2025-07-21 15:59:54,067 __main__:163 server: Starting up 2025-07-21 15:59:54,067 uvicorn.error:62 uncategorized: Application startup complete. 2025-07-21 15:59:54,068 uvicorn.error:216 uncategorized: Uvicorn running on http://['::', '0.0.0.0']:8321 (Press CTRL+C to quit) 2025-07-21 15:59:55,381 [TELEMETRY] 19:59:55.381 /v1/openai/v1/chat/completions 2025-07-21 15:59:55,619 uvicorn.access:473 uncategorized: 127.0.0.1:53102 - "POST /v1/openai/v1/chat/completions HTTP/1.1" 200 2025-07-21 15:59:55,621 [TELEMETRY] 19:59:55.621 /v1/openai/v1/chat/completions [StatusCode.OK] (240.07ms) 2025-07-21 15:59:55,622 [TELEMETRY] 19:59:55.620 127.0.0.1:53102 - "POST /v1/openai/v1/chat/completions HTTP/1.1" 200 ``` Signed-off-by: Charlie Doern <cdoern@redhat.com>
262 lines
8.5 KiB
Python
262 lines
8.5 KiB
Python
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
# All rights reserved.
|
|
#
|
|
# This source code is licensed under the terms described in the LICENSE file in
|
|
# the root directory of this source tree.
|
|
|
|
import logging
|
|
import os
|
|
import re
|
|
import sys
|
|
from logging.config import dictConfig
|
|
|
|
from rich.console import Console
|
|
from rich.errors import MarkupError
|
|
from rich.logging import RichHandler
|
|
from termcolor import cprint
|
|
|
|
from .distribution.datatypes import LoggingConfig
|
|
|
|
# Default log level
|
|
DEFAULT_LOG_LEVEL = logging.INFO
|
|
|
|
# Predefined categories
|
|
CATEGORIES = [
|
|
"core",
|
|
"server",
|
|
"router",
|
|
"inference",
|
|
"agents",
|
|
"safety",
|
|
"eval",
|
|
"tools",
|
|
"client",
|
|
"telemetry",
|
|
]
|
|
|
|
# Initialize category levels with default level
|
|
_category_levels: dict[str, int] = dict.fromkeys(CATEGORIES, DEFAULT_LOG_LEVEL)
|
|
|
|
|
|
def config_to_category_levels(category: str, level: str):
|
|
"""
|
|
Helper function to be called either by environment parsing or yaml parsing to go from a list of categories and levels to a dictionary ready to be
|
|
used by the logger dictConfig.
|
|
|
|
Parameters:
|
|
category (str): logging category to apply the level to
|
|
level (str): logging level to be used in the category
|
|
|
|
Returns:
|
|
Dict[str, int]: A dictionary mapping categories to their log levels.
|
|
"""
|
|
|
|
category_levels: dict[str, int] = {}
|
|
level_value = logging._nameToLevel.get(str(level).upper())
|
|
if level_value is None:
|
|
logging.warning(f"Unknown log level '{level}' for category '{category}'. Falling back to default 'INFO'.")
|
|
return category_levels
|
|
|
|
if category == "all":
|
|
# Apply the log level to all categories and the root logger
|
|
for cat in CATEGORIES:
|
|
category_levels[cat] = level_value
|
|
# Set the root logger's level to the specified level
|
|
category_levels["root"] = level_value
|
|
elif category in CATEGORIES:
|
|
category_levels[category] = level_value
|
|
logging.info(f"Setting '{category}' category to level '{level}'.")
|
|
else:
|
|
logging.warning(f"Unknown logging category: {category}. No changes made.")
|
|
return category_levels
|
|
|
|
|
|
def parse_yaml_config(yaml_config: LoggingConfig) -> dict[str, int]:
|
|
"""
|
|
Helper function to parse a yaml logging configuration found in the run.yaml
|
|
|
|
Parameters:
|
|
yaml_config (Logging): the logger config object found in the run.yaml
|
|
|
|
Returns:
|
|
Dict[str, int]: A dictionary mapping categories to their log levels.
|
|
"""
|
|
category_levels = {}
|
|
for category, level in yaml_config.category_levels.items():
|
|
category_levels.update(config_to_category_levels(category=category, level=level))
|
|
|
|
return category_levels
|
|
|
|
|
|
def parse_environment_config(env_config: str) -> dict[str, int]:
|
|
"""
|
|
Parse the LLAMA_STACK_LOGGING environment variable and return a dictionary of category log levels.
|
|
|
|
Parameters:
|
|
env_config (str): The value of the LLAMA_STACK_LOGGING environment variable.
|
|
|
|
Returns:
|
|
Dict[str, int]: A dictionary mapping categories to their log levels.
|
|
"""
|
|
category_levels = {}
|
|
for pair in env_config.split(";"):
|
|
if not pair.strip():
|
|
continue
|
|
|
|
try:
|
|
category, level = pair.split("=", 1)
|
|
category = category.strip().lower()
|
|
level = level.strip().upper() # Convert to uppercase for logging._nameToLevel
|
|
category_levels.update(config_to_category_levels(category=category, level=level))
|
|
|
|
except ValueError:
|
|
logging.warning(f"Invalid logging configuration: '{pair}'. Expected format: 'category=level'.")
|
|
|
|
return category_levels
|
|
|
|
|
|
def strip_rich_markup(text):
|
|
"""Remove Rich markup tags like [dim], [bold magenta], etc."""
|
|
return re.sub(r"\[/?[a-zA-Z0-9 _#=,]+\]", "", text)
|
|
|
|
|
|
class CustomRichHandler(RichHandler):
|
|
def __init__(self, *args, **kwargs):
|
|
kwargs["console"] = Console(width=150)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def emit(self, record):
|
|
"""Override emit to handle markup errors gracefully."""
|
|
try:
|
|
super().emit(record)
|
|
except MarkupError:
|
|
original_markup = self.markup
|
|
self.markup = False
|
|
try:
|
|
super().emit(record)
|
|
finally:
|
|
self.markup = original_markup
|
|
|
|
|
|
class CustomFileHandler(logging.FileHandler):
|
|
def __init__(self, filename, mode="a", encoding=None, delay=False):
|
|
super().__init__(filename, mode, encoding, delay)
|
|
# Default formatter to match console output
|
|
self.default_formatter = logging.Formatter("%(asctime)s %(name)s:%(lineno)d %(category)s: %(message)s")
|
|
self.setFormatter(self.default_formatter)
|
|
|
|
def emit(self, record):
|
|
if hasattr(record, "msg"):
|
|
record.msg = strip_rich_markup(str(record.msg))
|
|
super().emit(record)
|
|
|
|
|
|
def setup_logging(category_levels: dict[str, int], log_file: str | None) -> None:
|
|
"""
|
|
Configure logging based on the provided category log levels and an optional log file.
|
|
|
|
Parameters:
|
|
category_levels (Dict[str, int]): A dictionary mapping categories to their log levels.
|
|
log_file (str): Path to a log file to additionally pipe the logs into
|
|
"""
|
|
log_format = "%(asctime)s %(name)s:%(lineno)d %(category)s: %(message)s"
|
|
|
|
class CategoryFilter(logging.Filter):
|
|
"""Ensure category is always present in log records."""
|
|
|
|
def filter(self, record):
|
|
if not hasattr(record, "category"):
|
|
record.category = "uncategorized" # Default to 'uncategorized' if no category found
|
|
return True
|
|
|
|
# Determine the root logger's level (default to WARNING if not specified)
|
|
root_level = category_levels.get("root", logging.WARNING)
|
|
|
|
handlers = {
|
|
"console": {
|
|
"()": CustomRichHandler, # Use custom console handler
|
|
"formatter": "rich",
|
|
"rich_tracebacks": True,
|
|
"show_time": False,
|
|
"show_path": False,
|
|
"markup": True,
|
|
"filters": ["category_filter"],
|
|
}
|
|
}
|
|
|
|
# Add a file handler if log_file is set
|
|
if log_file:
|
|
handlers["file"] = {
|
|
"()": CustomFileHandler,
|
|
"filename": log_file,
|
|
"mode": "a",
|
|
"encoding": "utf-8",
|
|
}
|
|
|
|
logging_config = {
|
|
"version": 1,
|
|
"disable_existing_loggers": False,
|
|
"formatters": {
|
|
"rich": {
|
|
"()": logging.Formatter,
|
|
"format": log_format,
|
|
}
|
|
},
|
|
"handlers": handlers,
|
|
"filters": {
|
|
"category_filter": {
|
|
"()": CategoryFilter,
|
|
}
|
|
},
|
|
"loggers": {
|
|
category: {
|
|
"handlers": list(handlers.keys()), # Apply all handlers
|
|
"level": category_levels.get(category, DEFAULT_LOG_LEVEL),
|
|
"propagate": False, # Disable propagation to root logger
|
|
}
|
|
for category in CATEGORIES
|
|
},
|
|
"root": {
|
|
"handlers": list(handlers.keys()),
|
|
"level": root_level, # Set root logger's level dynamically
|
|
},
|
|
}
|
|
dictConfig(logging_config)
|
|
|
|
# Ensure third-party libraries follow the root log level
|
|
for _, logger in logging.root.manager.loggerDict.items():
|
|
if isinstance(logger, logging.Logger):
|
|
logger.setLevel(root_level)
|
|
|
|
|
|
def get_logger(
|
|
name: str, category: str = "uncategorized", config: LoggingConfig | None | None = None
|
|
) -> logging.LoggerAdapter:
|
|
"""
|
|
Returns a logger with the specified name and category.
|
|
If no category is provided, defaults to 'uncategorized'.
|
|
|
|
Parameters:
|
|
name (str): The name of the logger (e.g., module or filename).
|
|
category (str): The category of the logger (default 'uncategorized').
|
|
config (Logging): optional yaml config to override the existing logger configuration
|
|
|
|
Returns:
|
|
logging.LoggerAdapter: Configured logger with category support.
|
|
"""
|
|
if config:
|
|
_category_levels.update(parse_yaml_config(config))
|
|
|
|
logger = logging.getLogger(name)
|
|
logger.setLevel(_category_levels.get(category, DEFAULT_LOG_LEVEL))
|
|
return logging.LoggerAdapter(logger, {"category": category})
|
|
|
|
|
|
env_config = os.environ.get("LLAMA_STACK_LOGGING", "")
|
|
if env_config:
|
|
cprint(f"Environment variable LLAMA_STACK_LOGGING found: {env_config}", color="yellow", file=sys.stderr)
|
|
_category_levels.update(parse_environment_config(env_config))
|
|
|
|
log_file = os.environ.get("LLAMA_STACK_LOG_FILE")
|
|
|
|
setup_logging(_category_levels, log_file)
|