Skip to content

Commit f90f523

Browse files
fix: centralize logging configuration to respect LOGLEVEL environment variable
- Move logging configuration to top of __init__.py to ensure it runs before any imports - Remove duplicate logging.basicConfig from llm/__init__.py that was overriding settings - Clean up main.py to remove redundant logging configuration - Add test_logging.py script to verify the fix works correctly This ensures that the LOGLEVEL environment variable (DEBUG, INFO, WARNING, ERROR) properly controls logging verbosity throughout the package. Fixes #831 Co-authored-by: Mervin Praison <[email protected]>
1 parent 4a12aa9 commit f90f523

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

src/praisonai-agents/praisonaiagents/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22
Praison AI Agents - A package for hierarchical AI agent task execution
33
"""
44

5+
# Configure logging before any other imports
6+
import os
7+
import logging
8+
from rich.logging import RichHandler
9+
10+
# Get log level from environment variable
11+
LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper()
12+
13+
# Configure root logger
14+
logging.basicConfig(
15+
level=getattr(logging, LOGLEVEL, logging.INFO),
16+
format="%(asctime)s %(filename)s:%(lineno)d %(levelname)s %(message)s",
17+
datefmt="[%X]",
18+
handlers=[RichHandler(rich_tracebacks=True)]
19+
)
20+
21+
# Suppress specific noisy loggers
22+
logging.getLogger("litellm").setLevel(logging.WARNING)
23+
logging.getLogger("litellm.utils").setLevel(logging.WARNING)
24+
logging.getLogger("markdown_it").setLevel(logging.WARNING)
25+
logging.getLogger("rich.markdown").setLevel(logging.WARNING)
26+
logging.getLogger("httpx").setLevel(logging.WARNING)
27+
logging.getLogger("httpcore").setLevel(logging.WARNING)
28+
529
from .agent.agent import Agent
630
from .agent.image_agent import ImageAgent
731
from .agents.agents import PraisonAIAgents

src/praisonai-agents/praisonaiagents/llm/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
# Suppress pydantic warnings
1616
warnings.filterwarnings("ignore", category=UserWarning, module="pydantic")
1717

18-
# Configure logging to suppress all INFO messages
19-
logging.basicConfig(level=logging.WARNING)
20-
2118
# Import after suppressing warnings
2219
from .llm import LLM, LLMContextLengthExceededException
2320
from .openai_client import (

src/praisonai-agents/praisonaiagents/main.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,15 @@
99
from rich.panel import Panel
1010
from rich.text import Text
1111
from rich.markdown import Markdown
12-
from rich.logging import RichHandler
1312
from rich.live import Live
1413
import asyncio
1514

16-
# # Configure root logger
17-
# logging.basicConfig(level=logging.WARNING)
18-
19-
# Suppress litellm logs
15+
# Logging is already configured in __init__.py, just clean up handlers for litellm
2016
logging.getLogger("litellm").handlers = []
2117
logging.getLogger("litellm.utils").handlers = []
2218
logging.getLogger("litellm").propagate = False
2319
logging.getLogger("litellm.utils").propagate = False
2420

25-
LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper()
26-
27-
logging.basicConfig(
28-
level=getattr(logging, LOGLEVEL, logging.INFO),
29-
format="%(asctime)s %(filename)s:%(lineno)d %(levelname)s %(message)s",
30-
datefmt="[%X]",
31-
handlers=[RichHandler(rich_tracebacks=True)]
32-
)
33-
34-
# Add these lines to suppress markdown parser debug logs
35-
logging.getLogger('markdown_it').setLevel(logging.WARNING)
36-
logging.getLogger('rich.markdown').setLevel(logging.WARNING)
37-
3821
# Global list to store error logs
3922
error_logs = []
4023

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
"""Test script to verify logging is working correctly with LOGLEVEL environment variable"""
3+
4+
import os
5+
import sys
6+
import logging
7+
8+
# Test 1: Show current environment variable
9+
print(f"Current LOGLEVEL environment variable: {os.environ.get('LOGLEVEL', 'Not set')}")
10+
11+
# Test 2: Import praisonaiagents and check logging level
12+
print("\nImporting praisonaiagents...")
13+
import praisonaiagents
14+
15+
print(f"Root logger level after import: {logging.getLevelName(logging.getLogger().getEffectiveLevel())}")
16+
17+
# Test 3: Create a test agent and check its logging
18+
print("\nCreating test agent...")
19+
from praisonaiagents import Agent
20+
21+
agent = Agent(
22+
name="TestAgent",
23+
role="Logger Tester",
24+
goal="Test if logging works",
25+
backstory="I am a test agent created to verify logging functionality"
26+
)
27+
28+
# Test 4: Test different log levels
29+
print("\nTesting different log levels:")
30+
test_logger = logging.getLogger(__name__)
31+
32+
test_logger.debug("This is a DEBUG message - should show only when LOGLEVEL=DEBUG")
33+
test_logger.info("This is an INFO message - should show when LOGLEVEL=INFO or DEBUG")
34+
test_logger.warning("This is a WARNING message - should always show")
35+
test_logger.error("This is an ERROR message - should always show")
36+
37+
print("\nTest complete! Check above for log messages based on your LOGLEVEL setting.")
38+
print("\nTo test different levels, run:")
39+
print(" export LOGLEVEL=DEBUG && python test_logging.py")
40+
print(" export LOGLEVEL=INFO && python test_logging.py")
41+
print(" export LOGLEVEL=WARNING && python test_logging.py")

0 commit comments

Comments
 (0)