File size: 1,023 Bytes
3064989
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
import sys
import uuid
from typing import Optional


logger = logging.getLogger("animalmind_ml")


def setup_structured_logging():
    """Sets up python-json-logger if installed, or standard clean logger."""
    try:
        from pythonjsonlogger import jsonlogger

        handler = logging.StreamHandler(sys.stdout)
        formatter = jsonlogger.JsonFormatter(
            "%(asctime)s %(levelname)s %(name)s %(message)s %(correlation_id)s"
        )
        handler.setFormatter(formatter)
        logger.handlers = [handler]
        logger.setLevel(logging.INFO)
        print("[Logging] Structured JSON logging initialized.")
    except ImportError:
        logging.basicConfig(
            level=logging.INFO,
            format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
        )
        print("[Logging] Standard logging initialized (python-json-logger not installed).")


def get_correlation_id() -> str:
    """Generates a unique request correlation ID."""
    return str(uuid.uuid4())