File size: 6,006 Bytes
085d1c5 | 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | """
Logging utility for structured logging across the application.
Provides consistent logging format and helpers.
"""
import logging
import sys
from datetime import datetime
from typing import Any, Dict, Optional
class ColoredFormatter(logging.Formatter):
"""Custom formatter with colors for console output."""
# ANSI 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"
BOLD = "\033[1m"
def format(self, record: logging.LogRecord) -> str:
"""Format log record with colors."""
# Add color to level name
levelname = record.levelname
if levelname in self.COLORS:
record.levelname = (
f"{self.COLORS[levelname]}{self.BOLD}{levelname}{self.RESET}"
)
# Format the message
formatted = super().format(record)
return formatted
def setup_logger(
name: str = "finagent",
level: int = logging.INFO,
log_file: Optional[str] = None,
) -> logging.Logger:
"""
Set up a logger with console and optional file handlers.
Args:
name: Logger name
level: Logging level (default: INFO)
log_file: Optional path to log file
Returns:
Configured logger instance
"""
logger = logging.getLogger(name)
logger.setLevel(level)
# Prevent duplicate handlers
if logger.handlers:
return logger
# Console handler with colors
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(level)
console_formatter = ColoredFormatter(
fmt="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)
# File handler (optional)
if log_file:
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(level)
file_formatter = logging.Formatter(
fmt="%(asctime)s | %(levelname)s | %(name)s | %(funcName)s:%(lineno)d | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
return logger
def log_request(
logger: logging.Logger,
method: str,
path: str,
user_id: Optional[str] = None,
**kwargs: Any,
) -> None:
"""
Log an API request with structured data.
Args:
logger: Logger instance
method: HTTP method
path: Request path
user_id: Optional user ID
**kwargs: Additional context data
"""
context = {
"method": method,
"path": path,
"user_id": user_id,
"timestamp": datetime.utcnow().isoformat(),
**kwargs,
}
logger.info(f"Request: {method} {path}", extra={"context": context})
def log_response(
logger: logging.Logger,
method: str,
path: str,
status_code: int,
duration_ms: float,
**kwargs: Any,
) -> None:
"""
Log an API response with structured data.
Args:
logger: Logger instance
method: HTTP method
path: Request path
status_code: HTTP status code
duration_ms: Request duration in milliseconds
**kwargs: Additional context data
"""
context = {
"method": method,
"path": path,
"status_code": status_code,
"duration_ms": round(duration_ms, 2),
"timestamp": datetime.utcnow().isoformat(),
**kwargs,
}
logger.info(
f"Response: {method} {path} - {status_code} ({duration_ms:.2f}ms)",
extra={"context": context},
)
def log_error(
logger: logging.Logger,
error: Exception,
context: Optional[Dict[str, Any]] = None,
) -> None:
"""
Log an error with structured context.
Args:
logger: Logger instance
error: Exception instance
context: Optional context dictionary
"""
error_context = {
"error_type": type(error).__name__,
"error_message": str(error),
"timestamp": datetime.utcnow().isoformat(),
**(context or {}),
}
logger.error(
f"Error: {type(error).__name__}: {str(error)}",
extra={"context": error_context},
exc_info=True,
)
def log_agent_action(
logger: logging.Logger, action: str, details: Dict[str, Any]
) -> None:
"""
Log an agent action (tool call, decision, etc.).
Args:
logger: Logger instance
action: Action name/type
details: Action details
"""
context = {
"action": action,
"timestamp": datetime.utcnow().isoformat(),
**details,
}
logger.info(f"Agent Action: {action}", extra={"context": context})
def log_underwriting_decision(
logger: logging.Logger,
user_id: str,
decision: str,
amount: float,
credit_score: int,
foir: float,
) -> None:
"""
Log an underwriting decision with key metrics.
Args:
logger: Logger instance
user_id: User ID
decision: Decision (APPROVED/REJECTED/ADJUST)
amount: Approved amount
credit_score: Credit score
foir: FOIR ratio
"""
context = {
"user_id": user_id,
"decision": decision,
"approved_amount": amount,
"credit_score": credit_score,
"foir": round(foir, 3),
"timestamp": datetime.utcnow().isoformat(),
}
logger.info(
f"Underwriting Decision: {decision} for user {user_id}",
extra={"context": context},
)
# Default logger instance
default_logger = setup_logger()
|