Spaces:
Runtime error
Runtime error
File size: 11,492 Bytes
6391dfd f2bfacd 6391dfd f2bfacd 6391dfd f2bfacd 6391dfd f2bfacd 6391dfd f2bfacd 6391dfd f2bfacd 6391dfd f2bfacd 6391dfd f2bfacd | 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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | """
Production-grade logging configuration for Sync Worker Microservice.
Features:
- Structured JSON output for log aggregation (ELK, CloudWatch, Datadog, etc.)
- Rotating file handlers with size/backup limits
- Separate error log file
- Console handler with human-readable format for local dev
- Worker/entity context injection via LoggerAdapter
- Startup/shutdown structured events
"""
import logging
import logging.handlers
import sys
import os
import json
import traceback
from datetime import datetime, timezone
from typing import Optional, Any, Dict
from pathlib import Path
# ββ Constants ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
SERVICE_NAME = "sync-worker-ms"
LOG_DIR = Path(os.getenv("LOG_DIR", "logs"))
LOG_MAX_BYTES = int(os.getenv("LOG_MAX_BYTES", 50 * 1024 * 1024)) # 50 MB
LOG_BACKUP_COUNT = int(os.getenv("LOG_BACKUP_COUNT", "10"))
# ββ JSON Formatter ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class JSONFormatter(logging.Formatter):
"""
Emit log records as single-line JSON objects.
Every record includes:
timestamp β ISO-8601 UTC
level β DEBUG / INFO / WARNING / ERROR / CRITICAL
logger β dotted module name
message β formatted log message
service β SERVICE_NAME constant
pid β process id
Any key/value pairs passed via ``extra={}`` are merged at the top level.
Exception info is serialised into ``exception.type``, ``exception.message``
and ``exception.stacktrace``.
"""
RESERVED = frozenset(
{
"args", "created", "exc_info", "exc_text", "filename",
"funcName", "levelname", "levelno", "lineno", "message",
"module", "msecs", "msg", "name", "pathname", "process",
"processName", "relativeCreated", "stack_info", "thread",
"threadName",
}
)
def format(self, record: logging.LogRecord) -> str:
# Base fields
payload: Dict[str, Any] = {
"timestamp": datetime.fromtimestamp(record.created, tz=timezone.utc).isoformat(),
"level": record.levelname,
"logger": record.name,
"message": record.getMessage(),
"service": SERVICE_NAME,
"pid": record.process,
}
# Merge caller location for WARNING and above
if record.levelno >= logging.WARNING:
payload["caller"] = f"{record.pathname}:{record.lineno}"
# Merge extra fields (skip stdlib internals)
for key, val in record.__dict__.items():
if key not in self.RESERVED and not key.startswith("_"):
payload[key] = val
# Serialise exception
if record.exc_info and record.exc_info[0] is not None:
exc_type, exc_value, exc_tb = record.exc_info
payload["exception"] = {
"type": exc_type.__name__,
"message": str(exc_value),
"stacktrace": traceback.format_exception(exc_type, exc_value, exc_tb),
}
try:
return json.dumps(payload, default=str)
except Exception:
# Fallback β never let the formatter crash the app
payload["message"] = str(record.getMessage())
return json.dumps(payload, default=str)
# ββ Human-readable formatter (console / local dev) βββββββββββββββββββββββββββ
class ConsoleFormatter(logging.Formatter):
"""Coloured, human-readable formatter for local development."""
GREY = "\x1b[38;5;240m"
CYAN = "\x1b[36m"
YELLOW = "\x1b[33m"
RED = "\x1b[31m"
BOLD_RED = "\x1b[1;31m"
RESET = "\x1b[0m"
LEVEL_COLOURS = {
logging.DEBUG: GREY,
logging.INFO: CYAN,
logging.WARNING: YELLOW,
logging.ERROR: RED,
logging.CRITICAL: BOLD_RED,
}
FMT = "%(asctime)s %(levelname)-8s %(name)s β %(message)s"
def format(self, record: logging.LogRecord) -> str:
colour = self.LEVEL_COLOURS.get(record.levelno, self.RESET)
formatter = logging.Formatter(
f"{colour}{self.FMT}{self.RESET}",
datefmt="%Y-%m-%d %H:%M:%S",
)
result = formatter.format(record)
# Append extra context fields inline
extras = {
k: v for k, v in record.__dict__.items()
if k not in JSONFormatter.RESERVED and not k.startswith("_")
and k not in ("color_message",)
}
if extras:
result += f" {self.GREY}{extras}{self.RESET}"
return result
# ββ Setup βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def setup_logging(level: str = "INFO") -> None:
"""
Configure root logger with production-grade handlers.
Handlers created:
- stdout β JSON (production) or coloured console (LOG_FORMAT=console)
- app.log β rotating JSON, all levels β₯ level
- error.log β rotating JSON, ERROR and above only
Args:
level: Minimum log level string (DEBUG / INFO / WARNING / ERROR)
"""
numeric_level = getattr(logging, level.upper(), logging.INFO)
log_format = os.getenv("LOG_FORMAT", "json").lower()
# Ensure log directory exists
LOG_DIR.mkdir(parents=True, exist_ok=True)
root = logging.getLogger()
root.setLevel(numeric_level)
# Remove any handlers added by earlier basicConfig calls
root.handlers.clear()
# ββ stdout handler ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(numeric_level)
if log_format == "console":
stdout_handler.setFormatter(ConsoleFormatter())
else:
stdout_handler.setFormatter(JSONFormatter())
root.addHandler(stdout_handler)
# ββ Rotating app log (all levels) βββββββββββββββββββββββββββββββββββββββββ
app_log_path = LOG_DIR / "app.log"
file_handler = logging.handlers.RotatingFileHandler(
filename=app_log_path,
maxBytes=LOG_MAX_BYTES,
backupCount=LOG_BACKUP_COUNT,
encoding="utf-8",
)
file_handler.setLevel(numeric_level)
file_handler.setFormatter(JSONFormatter())
root.addHandler(file_handler)
# ββ Rotating error log (ERROR+) βββββββββββββββββββββββββββββββββββββββββββ
error_log_path = LOG_DIR / "error.log"
error_handler = logging.handlers.RotatingFileHandler(
filename=error_log_path,
maxBytes=LOG_MAX_BYTES,
backupCount=LOG_BACKUP_COUNT,
encoding="utf-8",
)
error_handler.setLevel(logging.ERROR)
error_handler.setFormatter(JSONFormatter())
root.addHandler(error_handler)
# Silence noisy third-party loggers
for noisy in ("motor", "pymongo", "asyncpg", "sqlalchemy.engine", "aioredis"):
logging.getLogger(noisy).setLevel(logging.WARNING)
# Emit a structured startup event so log pipelines can detect restarts
startup_logger = logging.getLogger(SERVICE_NAME)
startup_logger.info(
"Logging initialised",
extra={
"event": "logging_init",
"log_level": level.upper(),
"log_format": log_format,
"log_dir": str(LOG_DIR.resolve()),
"app_log": str(app_log_path.resolve()),
"error_log": str(error_log_path.resolve()),
},
)
# ββ Logger factory ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def get_logger(name: str) -> logging.Logger:
"""Return a standard Logger for the given module name."""
return logging.getLogger(name)
def get_worker_logger(name: str, entity_type: str, worker_id: int) -> logging.LoggerAdapter:
"""
Return a LoggerAdapter that automatically injects worker context into every
log record, so you never have to repeat entity_type / worker_id in extra={}.
Usage::
logger = get_worker_logger(__name__, "catalogue", 3)
logger.info("Processing item", extra={"entity_id": "abc-123"})
# β {"entity_type": "catalogue", "worker_id": 3, "entity_id": "abc-123", ...}
"""
base = logging.getLogger(name)
return logging.LoggerAdapter(base, {"entity_type": entity_type, "worker_id": worker_id})
# ββ Structured event helpers ββββββββββββββββββββββββββββββββββββββββββββββββββ
def log_sync_start(logger: logging.Logger, entity_type: str, entity_id: str, operation: str, worker_id: int) -> None:
logger.info(
"Sync started",
extra={
"event": "sync_start",
"entity_type": entity_type,
"entity_id": entity_id,
"operation": operation,
"worker_id": worker_id,
},
)
def log_sync_success(
logger: logging.Logger,
entity_type: str,
entity_id: str,
operation: str,
worker_id: int,
duration_ms: float,
) -> None:
logger.info(
"Sync succeeded",
extra={
"event": "sync_success",
"entity_type": entity_type,
"entity_id": entity_id,
"operation": operation,
"worker_id": worker_id,
"duration_ms": round(duration_ms, 2),
},
)
def log_sync_failure(
logger: logging.Logger,
entity_type: str,
entity_id: str,
operation: str,
worker_id: int,
duration_ms: float,
error: str,
attempt: int,
max_attempts: int,
) -> None:
logger.error(
"Sync failed",
extra={
"event": "sync_failure",
"entity_type": entity_type,
"entity_id": entity_id,
"operation": operation,
"worker_id": worker_id,
"duration_ms": round(duration_ms, 2),
"error": error,
"attempt": attempt,
"max_attempts": max_attempts,
},
)
def log_worker_heartbeat(
logger: logging.Logger,
entity_type: str,
worker_id: int,
iteration: int,
queue_size: int,
) -> None:
logger.info(
"Worker heartbeat",
extra={
"event": "worker_heartbeat",
"entity_type": entity_type,
"worker_id": worker_id,
"iteration": iteration,
"queue_size": queue_size,
},
)
def log_service_lifecycle(logger: logging.Logger, event: str, **kwargs: Any) -> None:
"""Emit a structured lifecycle event (startup, shutdown, connection, etc.)."""
logger.info(
event.replace("_", " ").capitalize(),
extra={"event": event, **kwargs},
)
|