File size: 3,235 Bytes
cacd4d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Logging setup for GEPA Optimizer.

This module provides backward-compatible logging functions that delegate
to the centralized logging infrastructure.

For new code, prefer importing directly from infrastructure.logging:
    from gepa_optimizer.infrastructure.logging import get_logger, configure_logging
"""

import logging
from pathlib import Path
from datetime import datetime
from typing import Optional, Union

# Import from centralized infrastructure
from ..infrastructure.logging import (
    get_logger as _get_logger,
    configure_logging as _configure_logging,
    LogLevel,
)


def setup_logging(
    level: str = "INFO",
    log_file: Optional[Union[str, bool]] = None,
    use_colors: bool = True,
    include_emoji: bool = True,
) -> None:
    """
    Configure logging for GEPA Optimizer with optional file logging.

    This function provides backward compatibility with existing code.
    New code should use configure_logging() from infrastructure.logging.

    Args:
        level: Logging level (e.g. "DEBUG", "INFO", "WARNING")
        log_file: Path to log file. 
                 - None: Auto-generates timestamped filename in logs/
                 - False: Disables file logging
                 - str: Uses specified path
        use_colors: Whether to use colored output in console
        include_emoji: Whether to include emoji in log messages
        
    Example:
        # Basic setup
        setup_logging(level="INFO")
        
        # With file logging
        setup_logging(level="DEBUG", log_file="optimization.log")
        
        # Console only, no colors
        setup_logging(level="INFO", log_file=False, use_colors=False)
    """
    # Handle auto-generated log file
    actual_log_file: Optional[str] = None
    
    if log_file is None:
        # Auto-generate log filename with timestamp
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        log_dir = Path("logs")
        log_dir.mkdir(exist_ok=True)
        actual_log_file = str(log_dir / f"optimization_{timestamp}.log")
    elif log_file is not False:
        # Use specified path
        log_path = Path(log_file)
        log_path.parent.mkdir(parents=True, exist_ok=True)
        actual_log_file = str(log_file)
    
    # Delegate to centralized configuration
    _configure_logging(
        level=level,
        log_file=actual_log_file,
        use_colors=use_colors,
        include_emoji=include_emoji,
    )
    
    # Log configuration info
    logger = _get_logger(__name__)
    if actual_log_file:
        logger.info(f"Logging to file: {actual_log_file}")
        logger.info(f"Logging configured at {level} level (console + file)")
    else:
        logger.info(f"Logging configured at {level} level (console only)")


def get_logger(name: str) -> logging.Logger:
    """
    Get a logger for a specific module.
    
    This function provides backward compatibility. New code should use:
        from gepa_optimizer.infrastructure.logging import get_logger
    
    Args:
        name: Module name (typically __name__)
        
    Returns:
        Configured Logger instance
    """
    return _get_logger(name)


# Re-export for convenience
__all__ = [
    "setup_logging",
    "get_logger",
]