File size: 8,027 Bytes
22a3c56 |
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 |
"""Debug logger module for detailed API request/response logging"""
import json
import logging
from datetime import datetime
from pathlib import Path
from typing import Dict, Any, Optional
from .config import config
class DebugLogger:
"""Debug logger for API requests and responses"""
def __init__(self):
self.log_file = Path("logs.txt")
self._setup_logger()
def _setup_logger(self):
"""Setup file logger"""
# Create logger
self.logger = logging.getLogger("debug_logger")
self.logger.setLevel(logging.DEBUG)
# Remove existing handlers
self.logger.handlers.clear()
# Create file handler
file_handler = logging.FileHandler(
self.log_file,
mode='a',
encoding='utf-8'
)
file_handler.setLevel(logging.DEBUG)
# Create formatter
formatter = logging.Formatter(
'%(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
file_handler.setFormatter(formatter)
# Add handler
self.logger.addHandler(file_handler)
# Prevent propagation to root logger
self.logger.propagate = False
def _mask_token(self, token: str) -> str:
"""Mask token for logging (show first 6 and last 6 characters)"""
if not config.debug_mask_token or len(token) <= 12:
return token
return f"{token[:6]}...{token[-6:]}"
def _format_timestamp(self) -> str:
"""Format current timestamp"""
return datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
def _write_separator(self, char: str = "=", length: int = 100):
"""Write separator line"""
self.logger.info(char * length)
def log_request(
self,
method: str,
url: str,
headers: Dict[str, str],
body: Optional[Any] = None,
files: Optional[Dict] = None,
proxy: Optional[str] = None
):
"""Log API request details to log.txt"""
try:
self._write_separator()
self.logger.info(f"π΅ [REQUEST] {self._format_timestamp()}")
self._write_separator("-")
# Basic info
self.logger.info(f"Method: {method}")
self.logger.info(f"URL: {url}")
# Headers
self.logger.info("\nπ Headers:")
masked_headers = dict(headers)
if "Authorization" in masked_headers:
auth_value = masked_headers["Authorization"]
if auth_value.startswith("Bearer "):
token = auth_value[7:]
masked_headers["Authorization"] = f"Bearer {self._mask_token(token)}"
for key, value in masked_headers.items():
self.logger.info(f" {key}: {value}")
# Body
if body is not None:
self.logger.info("\nπ¦ Request Body:")
if isinstance(body, (dict, list)):
body_str = json.dumps(body, indent=2, ensure_ascii=False)
self.logger.info(body_str)
else:
self.logger.info(str(body))
# Files
if files:
self.logger.info("\nπ Files:")
try:
# Handle both dict and CurlMime objects
if hasattr(files, 'keys') and callable(getattr(files, 'keys', None)):
for key in files.keys():
self.logger.info(f" {key}: <file data>")
else:
# CurlMime or other non-dict objects
self.logger.info(" <multipart form data>")
except (AttributeError, TypeError):
# Fallback for objects that don't support iteration
self.logger.info(" <binary file data>")
# Proxy
if proxy:
self.logger.info(f"\nπ Proxy: {proxy}")
self._write_separator()
self.logger.info("") # Empty line
except Exception as e:
self.logger.error(f"Error logging request: {e}")
def log_response(
self,
status_code: int,
headers: Dict[str, str],
body: Any,
duration_ms: Optional[float] = None
):
"""Log API response details to log.txt"""
try:
self._write_separator()
self.logger.info(f"π’ [RESPONSE] {self._format_timestamp()}")
self._write_separator("-")
# Status
status_emoji = "β
" if 200 <= status_code < 300 else "β"
self.logger.info(f"Status: {status_code} {status_emoji}")
# Duration
if duration_ms is not None:
self.logger.info(f"Duration: {duration_ms:.2f}ms")
# Headers
self.logger.info("\nπ Response Headers:")
for key, value in headers.items():
self.logger.info(f" {key}: {value}")
# Body
self.logger.info("\nπ¦ Response Body:")
if isinstance(body, (dict, list)):
body_str = json.dumps(body, indent=2, ensure_ascii=False)
self.logger.info(body_str)
elif isinstance(body, str):
# Try to parse as JSON
try:
parsed = json.loads(body)
body_str = json.dumps(parsed, indent=2, ensure_ascii=False)
self.logger.info(body_str)
except:
# Not JSON, log as text (limit length)
if len(body) > 2000:
self.logger.info(f"{body[:2000]}... (truncated)")
else:
self.logger.info(body)
else:
self.logger.info(str(body))
self._write_separator()
self.logger.info("") # Empty line
except Exception as e:
self.logger.error(f"Error logging response: {e}")
def log_error(
self,
error_message: str,
status_code: Optional[int] = None,
response_text: Optional[str] = None
):
"""Log API error details to log.txt"""
try:
self._write_separator()
self.logger.info(f"π΄ [ERROR] {self._format_timestamp()}")
self._write_separator("-")
if status_code:
self.logger.info(f"Status Code: {status_code}")
self.logger.info(f"Error Message: {error_message}")
if response_text:
self.logger.info("\nπ¦ Error Response:")
# Try to parse as JSON
try:
parsed = json.loads(response_text)
body_str = json.dumps(parsed, indent=2, ensure_ascii=False)
self.logger.info(body_str)
except:
# Not JSON, log as text
if len(response_text) > 2000:
self.logger.info(f"{response_text[:2000]}... (truncated)")
else:
self.logger.info(response_text)
self._write_separator()
self.logger.info("") # Empty line
except Exception as e:
self.logger.error(f"Error logging error: {e}")
def log_info(self, message: str):
"""Log general info message to log.txt"""
try:
self.logger.info(f"βΉοΈ [{self._format_timestamp()}] {message}")
except Exception as e:
self.logger.error(f"Error logging info: {e}")
# Global debug logger instance
debug_logger = DebugLogger()
|