File size: 3,641 Bytes
e00e744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
import logging
from typing import Optional
from datetime import datetime
from enum import Enum

# Add NullHandler to prevent "No handler could be found" warnings
# This is the only logging configuration the library should do
logging.getLogger(__name__).addHandler(logging.NullHandler())

# Color enum for console output (from database.py)
class Color(Enum):
    WHITE = "\033[97m"
    CYAN = "\033[96m"
    GREEN = "\033[92m"
    YELLOW = "\033[93m"
    MAGENTA = "\033[95m"
    RED = "\033[91m"
    RESET = "\033[0m"

# Color mapping for different log types (from database.py)
color_mapper = {
    "trace": Color.WHITE,
    "agent": Color.CYAN,
    "function": Color.GREEN,
    "generation": Color.YELLOW,
    "response": Color.MAGENTA,
    "account": Color.RED,
    "span": Color.CYAN,  # Default for span type
}

def get_logger(name: str) -> logging.Logger:
    """
    Get a logger with the specified name.
    
    This is a simple wrapper around logging.getLogger() that ensures
    the library follows proper logging patterns.
    
    Args:
        name: The name for the logger (usually __name__)
        
    Returns:
        logging.Logger: Logger instance
    """
    return logging.getLogger(name)

def write_lineage_log(name: str, type: str, message: str):
    """
    Write a log entry using standard logging.
    
    This function uses standard logging instead of writing to files directly.
    The application is responsible for configuring where logs go (console, files, etc.).
    
    Args:
        name (str): The name associated with the log
        type (str): The type of log entry
        message (str): The log message
    """
    # Map log types to standard logging levels
    type_to_level = {
        "trace": logging.INFO,
        "agent": logging.INFO,
        "function": logging.DEBUG,
        "generation": logging.INFO,
        "response": logging.INFO,
        "account": logging.WARNING,
        "span": logging.DEBUG,
        "error": logging.ERROR,
        "warning": logging.WARNING,
        "info": logging.INFO,
        "debug": logging.DEBUG
    }
    
    level = type_to_level.get(type.lower(), logging.INFO)
    
    # Use the lineage logger with structured data
    lineage_logger = logging.getLogger(f"lf_algorithm.lineage.{name}")
    
    # Log with structured context
    lineage_logger.log(
        level, 
        f"{type}: {message}",
        extra={
            "lineage_name": name,
            "lineage_type": type,
            "lineage_datetime": datetime.now().isoformat()
        }
    )

# Legacy functions for backward compatibility - these now just return loggers
# without any configuration, as the application should handle all configuration
def setup_logging(
    level: int = None,
    log_to_file: bool = None,
    log_to_console: bool = None,
    use_colors: bool = None
) -> None:
    """
    Legacy function - does nothing in library mode.
    
    Applications should configure logging themselves using:
    - logging.basicConfig() for simple setups
    - logging.config.dictConfig() for advanced setups
    """
    # This function is kept for backward compatibility but does nothing
    # The library should not configure logging - that's the application's job
    pass

def configure_logging(
    log_dir: str = None,
    log_file: str = None,
    enabled: bool = None
) -> None:
    """
    Legacy function - does nothing in library mode.
    
    Applications should configure logging themselves.
    """
    # This function is kept for backward compatibility but does nothing
    # The library should not configure logging - that's the application's job
    pass