Logging

Record runtime information for debugging and tracking. Recommended to use Python's standard logging and configure uniformly when Flask starts.

Basic Configuration:

import logging
from logging.handlers import RotatingFileHandler

handler = RotatingFileHandler("logs/app.log", maxBytes=1_000_000, backupCount=10)
formatter = logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
handler.setFormatter(formatter)
handler.setLevel(logging.INFO)

app.logger.addHandler(handler)
app.logger.setLevel(logging.INFO)
app.logger.info("App started")

Module-level Logging: logger = logging.getLogger(__name__).

Structured Logging: Can use json-logger or configure JSON output at gunicorn/uvicorn level.

Production: Combine with reverse proxy or centralized logging (ELK/EFK) for collection and retrieval.