File size: 6,886 Bytes
2b44e69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
Centralized Logging Utility for Invoice Processing System
Provides structured logging with different levels and formatters
"""

import logging
import sys
from datetime import datetime
from typing import Optional
from pathlib import Path


class ColoredFormatter(logging.Formatter):
    """Custom formatter with colors for console output"""
    
    # Color codes
    COLORS = {
        'DEBUG': '\033[36m',    # Cyan
        'INFO': '\033[32m',     # Green
        'WARNING': '\033[33m',  # Yellow
        'ERROR': '\033[31m',    # Red
        'CRITICAL': '\033[35m', # Magenta
        'RESET': '\033[0m'      # Reset
    }
    
    def format(self, record):
        # Add color to levelname
        if record.levelname in self.COLORS:
            record.levelname = (
                f"{self.COLORS[record.levelname]}{record.levelname}"
                f"{self.COLORS['RESET']}"
            )
        
        return super().format(record)


def setup_logging(log_level: str = "INFO", log_file: Optional[str] = None):
    """
    Setup centralized logging configuration
    
    Args:
        log_level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
        log_file: Optional log file path
    """
    
    # Create logs directory if it doesn't exist
    if log_file:
        log_path = Path(log_file)
        log_path.parent.mkdir(parents=True, exist_ok=True)
    
    # Configure root logger
    root_logger = logging.getLogger()
    root_logger.setLevel(getattr(logging, log_level.upper()))
    
    # Clear existing handlers
    root_logger.handlers.clear()
    
    # Console handler with colors
    console_handler = logging.StreamHandler(sys.stdout)
    console_formatter = ColoredFormatter(
        fmt='%(asctime)s | %(levelname)-8s | %(name)-20s | %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S'
    )
    console_handler.setFormatter(console_formatter)
    root_logger.addHandler(console_handler)
    
    # File handler (if specified)
    if log_file:
        file_handler = logging.FileHandler(log_file)
        file_formatter = logging.Formatter(
            fmt='%(asctime)s | %(levelname)-8s | %(name)-20s | %(funcName)-15s | %(message)s',
            datefmt='%Y-%m-%d %H:%M:%S'
        )
        file_handler.setFormatter(file_formatter)
        root_logger.addHandler(file_handler)
    
    # Set specific logger levels
    logging.getLogger("httpx").setLevel(logging.WARNING)
    logging.getLogger("urllib3").setLevel(logging.WARNING)
    logging.getLogger("requests").setLevel(logging.WARNING)


def get_logger(name: str) -> logging.Logger:
    """
    Get a logger instance with the specified name
    
    Args:
        name: Logger name (typically module or class name)
        
    Returns:
        Configured logger instance
    """
    return logging.getLogger(name)


class StructuredLogger:
    """
    Structured logger for agent operations with consistent formatting
    """
    
    def __init__(self, name: str):
        self.logger = get_logger(name)
        self.name = name
    
    def log_agent_start(self, agent_name: str, process_id: str, **kwargs):
        """Log agent execution start"""
        self.logger.info(
            f"[START] Agent {agent_name} started for process {process_id}",
            extra={"agent": agent_name, "process_id": process_id, **kwargs}
        )
    
    def log_agent_complete(self, agent_name: str, process_id: str, 
                          duration_ms: int, **kwargs):
        """Log agent execution completion"""
        self.logger.info(
            f"[COMPLETE] Agent {agent_name} completed for process {process_id} "
            f"in {duration_ms}ms",
            extra={
                "agent": agent_name, 
                "process_id": process_id, 
                "duration_ms": duration_ms,
                **kwargs
            }
        )
    
    def log_agent_error(self, agent_name: str, process_id: str, 
                       error: Exception, **kwargs):
        """Log agent execution error"""
        self.logger.error(
            f"[ERROR] Agent {agent_name} failed for process {process_id}: {str(error)}",
            extra={
                "agent": agent_name,
                "process_id": process_id,
                "error_type": type(error).__name__,
                "error_message": str(error),
                **kwargs
            },
            exc_info=True
        )
    
    def log_decision(self, agent_name: str, process_id: str, 
                    decision: str, reasoning: str, confidence: float = None):
        """Log agent decision"""
        message = f"[DECISION] Agent {agent_name} decided: {decision} - {reasoning}"
        if confidence:
            message += f" (confidence: {confidence:.2f})"
        
        self.logger.info(
            message,
            extra={
                "agent": agent_name,
                "process_id": process_id,
                "decision": decision,
                "reasoning": reasoning,
                "confidence": confidence
            }
        )
    
    def log_escalation(self, agent_name: str, process_id: str, 
                      reason: str, **kwargs):
        """Log escalation event"""
        self.logger.warning(
            f"[ESCALATION] Agent {agent_name} escalating process {process_id}: {reason}",
            extra={
                "agent": agent_name,
                "process_id": process_id,
                "escalation_reason": reason,
                **kwargs
            }
        )
    
    def log_workflow_start(self, workflow_type: str, process_id: str, **kwargs):
        """Log workflow start"""
        self.logger.info(
            f"[WORKFLOW] Starting {workflow_type} workflow for process {process_id}",
            extra={
                "workflow_type": workflow_type,
                "process_id": process_id,
                **kwargs
            }
        )
    
    def log_workflow_complete(self, workflow_type: str, process_id: str, 
                             duration_ms: int, **kwargs):
        """Log workflow completion"""
        self.logger.info(
            f"[WORKFLOW_COMPLETE] Completed {workflow_type} workflow for process {process_id} "
            f"in {duration_ms}ms",
            extra={
                "workflow_type": workflow_type,
                "process_id": process_id,
                "duration_ms": duration_ms,
                **kwargs
            }
        )
    
    def log_metric(self, metric_name: str, value: float, **kwargs):
        """Log metric value"""
        self.logger.info(
            f"[METRIC] {metric_name}: {value}",
            extra={
                "metric_name": metric_name,
                "metric_value": value,
                **kwargs
            }
        )


# Initialize default logging
setup_logging()

# Create default structured logger
default_logger = StructuredLogger("invoice_system")