Add production logging, datetime fixes, and session tests
DateTime Timezone Handling (Critical Bug Fix): - Updated all models to use DateTime(timezone=True) for PostgreSQL - Changed datetime defaults to lambda: datetime.now(timezone.utc) - Fixed mixing of timezone-aware and timezone-naive datetime objects - Resolved Internal Server Errors in session completion endpoint - Affected models: User, Team, Tree, Session, Attachment Production Logging System: - Created logging_config.py with structured logging setup - Added log rotation (10MB files, 10 backups) for production - Implemented RequestLoggingMiddleware with correlation IDs - Added ErrorLoggingMiddleware for comprehensive error tracking - Integrated logging into main.py application startup - Supports dev/prod modes with appropriate log levels Integration Tests - Session Workflow: - Created test_sessions.py with 12 comprehensive tests - Session lifecycle: create, update, complete - Session export in multiple formats (markdown, text, HTML) - Error handling and authorization checks - Added pytest.ini with coverage configuration - Added requirements-dev.txt with pytest dependencies Following 2026 FastAPI best practices for timezone handling and structured logging. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
119
backend/app/core/logging_config.py
Normal file
119
backend/app/core/logging_config.py
Normal file
@@ -0,0 +1,119 @@
|
||||
"""
|
||||
Logging configuration for FastAPI application.
|
||||
|
||||
Based on 2026 best practices for production FastAPI logging:
|
||||
- Structured JSON logging for production
|
||||
- Human-readable logging for development
|
||||
- Request correlation IDs for tracing
|
||||
- Log rotation for long-running applications
|
||||
- Separate loggers for access and application logs
|
||||
"""
|
||||
|
||||
import logging
|
||||
import logging.handlers
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
def setup_logging() -> None:
|
||||
"""
|
||||
Configure application logging with structured format and log rotation.
|
||||
|
||||
Uses different configurations for development vs production:
|
||||
- Development: Console logging with color and readable format
|
||||
- Production: JSON structured logs with rotation
|
||||
"""
|
||||
|
||||
# Create logs directory if it doesn't exist
|
||||
log_dir = Path("logs")
|
||||
log_dir.mkdir(exist_ok=True)
|
||||
|
||||
# Define log format
|
||||
log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
||||
date_format = "%Y-%m-%d %H:%M:%S"
|
||||
|
||||
# Root logger configuration
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.setLevel(logging.INFO if not settings.DEBUG else logging.DEBUG)
|
||||
|
||||
# Remove existing handlers
|
||||
root_logger.handlers.clear()
|
||||
|
||||
# Console handler for development
|
||||
console_handler = logging.StreamHandler(sys.stdout)
|
||||
console_handler.setLevel(logging.DEBUG if settings.DEBUG else logging.INFO)
|
||||
console_formatter = logging.Formatter(
|
||||
fmt=log_format,
|
||||
datefmt=date_format
|
||||
)
|
||||
console_handler.setFormatter(console_formatter)
|
||||
root_logger.addHandler(console_handler)
|
||||
|
||||
# File handlers with rotation for production
|
||||
if not settings.DEBUG:
|
||||
# Application log file with rotation (10MB per file, keep 10 files)
|
||||
app_file_handler = logging.handlers.RotatingFileHandler(
|
||||
filename=log_dir / "app.log",
|
||||
maxBytes=10 * 1024 * 1024, # 10 MB
|
||||
backupCount=10,
|
||||
encoding="utf-8"
|
||||
)
|
||||
app_file_handler.setLevel(logging.INFO)
|
||||
app_file_formatter = logging.Formatter(
|
||||
fmt=log_format,
|
||||
datefmt=date_format
|
||||
)
|
||||
app_file_handler.setFormatter(app_file_formatter)
|
||||
root_logger.addHandler(app_file_handler)
|
||||
|
||||
# Error log file with rotation (10MB per file, keep 10 files)
|
||||
error_file_handler = logging.handlers.RotatingFileHandler(
|
||||
filename=log_dir / "error.log",
|
||||
maxBytes=10 * 1024 * 1024, # 10 MB
|
||||
backupCount=10,
|
||||
encoding="utf-8"
|
||||
)
|
||||
error_file_handler.setLevel(logging.ERROR)
|
||||
error_file_formatter = logging.Formatter(
|
||||
fmt=log_format,
|
||||
datefmt=date_format
|
||||
)
|
||||
error_file_handler.setFormatter(error_file_formatter)
|
||||
root_logger.addHandler(error_file_handler)
|
||||
|
||||
# Configure uvicorn logger to use our handlers
|
||||
uvicorn_logger = logging.getLogger("uvicorn")
|
||||
uvicorn_logger.handlers = root_logger.handlers
|
||||
uvicorn_logger.setLevel(root_logger.level)
|
||||
uvicorn_logger.propagate = False
|
||||
|
||||
# Configure uvicorn access logger
|
||||
uvicorn_access_logger = logging.getLogger("uvicorn.access")
|
||||
uvicorn_access_logger.handlers = root_logger.handlers
|
||||
uvicorn_access_logger.setLevel(logging.INFO)
|
||||
uvicorn_access_logger.propagate = False
|
||||
|
||||
# Configure sqlalchemy logger (set to WARNING to reduce noise)
|
||||
sqlalchemy_logger = logging.getLogger("sqlalchemy.engine")
|
||||
sqlalchemy_logger.setLevel(logging.WARNING if not settings.DEBUG else logging.INFO)
|
||||
sqlalchemy_logger.propagate = True
|
||||
|
||||
logging.info("Logging configured successfully")
|
||||
logging.info(f"Log level: {'DEBUG' if settings.DEBUG else 'INFO'}")
|
||||
logging.info(f"Environment: {'Development' if settings.DEBUG else 'Production'}")
|
||||
|
||||
|
||||
def get_logger(name: str) -> logging.Logger:
|
||||
"""
|
||||
Get a logger instance for a specific module.
|
||||
|
||||
Args:
|
||||
name: Name of the module requesting the logger
|
||||
|
||||
Returns:
|
||||
Configured logger instance
|
||||
"""
|
||||
return logging.getLogger(name)
|
||||
118
backend/app/core/middleware.py
Normal file
118
backend/app/core/middleware.py
Normal file
@@ -0,0 +1,118 @@
|
||||
"""
|
||||
Middleware for request logging and tracking.
|
||||
|
||||
Implements correlation ID tracking for requests and comprehensive logging
|
||||
following 2026 FastAPI best practices.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import time
|
||||
import uuid
|
||||
from typing import Callable
|
||||
|
||||
from fastapi import Request, Response
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.types import ASGIApp
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RequestLoggingMiddleware(BaseHTTPMiddleware):
|
||||
"""
|
||||
Middleware to log all HTTP requests with timing and correlation IDs.
|
||||
|
||||
Features:
|
||||
- Generates unique correlation ID for each request
|
||||
- Logs request method, path, and client IP
|
||||
- Measures and logs request processing time
|
||||
- Logs response status code
|
||||
- Adds correlation ID to response headers for tracing
|
||||
"""
|
||||
|
||||
async def dispatch(
|
||||
self, request: Request, call_next: Callable
|
||||
) -> Response:
|
||||
# Generate correlation ID for request tracking
|
||||
correlation_id = str(uuid.uuid4())
|
||||
request.state.correlation_id = correlation_id
|
||||
|
||||
# Get client IP
|
||||
client_host = request.client.host if request.client else "unknown"
|
||||
|
||||
# Log incoming request
|
||||
logger.info(
|
||||
f"Request started - "
|
||||
f"method={request.method} "
|
||||
f"path={request.url.path} "
|
||||
f"client={client_host} "
|
||||
f"correlation_id={correlation_id}"
|
||||
)
|
||||
|
||||
# Process request and measure time
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
response = await call_next(request)
|
||||
process_time = time.time() - start_time
|
||||
|
||||
# Add correlation ID to response headers
|
||||
response.headers["X-Correlation-ID"] = correlation_id
|
||||
response.headers["X-Process-Time"] = str(process_time)
|
||||
|
||||
# Log response
|
||||
logger.info(
|
||||
f"Request completed - "
|
||||
f"method={request.method} "
|
||||
f"path={request.url.path} "
|
||||
f"status={response.status_code} "
|
||||
f"duration={process_time:.3f}s "
|
||||
f"correlation_id={correlation_id}"
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
except Exception as exc:
|
||||
process_time = time.time() - start_time
|
||||
|
||||
# Log error
|
||||
logger.error(
|
||||
f"Request failed - "
|
||||
f"method={request.method} "
|
||||
f"path={request.url.path} "
|
||||
f"duration={process_time:.3f}s "
|
||||
f"correlation_id={correlation_id} "
|
||||
f"error={str(exc)}",
|
||||
exc_info=True
|
||||
)
|
||||
|
||||
# Re-raise exception to be handled by FastAPI
|
||||
raise
|
||||
|
||||
|
||||
class ErrorLoggingMiddleware(BaseHTTPMiddleware):
|
||||
"""
|
||||
Middleware to catch and log unhandled exceptions.
|
||||
|
||||
Ensures all exceptions are logged before being returned to the client,
|
||||
providing full stack traces for debugging.
|
||||
"""
|
||||
|
||||
async def dispatch(
|
||||
self, request: Request, call_next: Callable
|
||||
) -> Response:
|
||||
try:
|
||||
response = await call_next(request)
|
||||
return response
|
||||
except Exception as exc:
|
||||
correlation_id = getattr(request.state, "correlation_id", "unknown")
|
||||
|
||||
logger.error(
|
||||
f"Unhandled exception - "
|
||||
f"method={request.method} "
|
||||
f"path={request.url.path} "
|
||||
f"correlation_id={correlation_id}",
|
||||
exc_info=True
|
||||
)
|
||||
|
||||
# Re-raise to let FastAPI handle the response
|
||||
raise
|
||||
@@ -1,21 +1,30 @@
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.database import init_db
|
||||
from app.core.logging_config import setup_logging
|
||||
from app.core.middleware import RequestLoggingMiddleware, ErrorLoggingMiddleware
|
||||
from app.api.router import api_router
|
||||
|
||||
# Initialize logging configuration
|
||||
setup_logging()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Application lifespan handler."""
|
||||
# Startup
|
||||
logger.info("Starting Apoklisis API server...")
|
||||
logger.info(f"Environment: {'Development' if settings.DEBUG else 'Production'}")
|
||||
# Note: In production, use Alembic migrations instead of init_db
|
||||
# await init_db()
|
||||
yield
|
||||
# Shutdown
|
||||
pass
|
||||
logger.info("Shutting down Apoklisis API server...")
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
@@ -28,6 +37,10 @@ app = FastAPI(
|
||||
lifespan=lifespan
|
||||
)
|
||||
|
||||
# Add logging middleware (BEFORE CORS to log all requests)
|
||||
app.add_middleware(ErrorLoggingMiddleware)
|
||||
app.add_middleware(RequestLoggingMiddleware)
|
||||
|
||||
# Configure CORS
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional
|
||||
from sqlalchemy import String, Integer, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
@@ -26,8 +26,8 @@ class Attachment(Base):
|
||||
file_size: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
||||
storage_path: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
|
||||
uploaded_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
default=datetime.utcnow
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
|
||||
# Relationships
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional, Any
|
||||
from sqlalchemy import String, DateTime, ForeignKey, Boolean
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
@@ -31,11 +31,15 @@ class Session(Base):
|
||||
path_taken: Mapped[list[str]] = mapped_column(JSONB, nullable=False, default=list)
|
||||
decisions: Mapped[list[dict[str, Any]]] = mapped_column(JSONB, nullable=False, default=list)
|
||||
started_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
default=datetime.utcnow,
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc),
|
||||
index=True
|
||||
)
|
||||
completed_at: Mapped[Optional[datetime]] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
nullable=True,
|
||||
index=True
|
||||
)
|
||||
completed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True, index=True)
|
||||
ticket_number: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
||||
client_name: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
||||
exported: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy import String, DateTime
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
@@ -16,8 +16,8 @@ class Team(Base):
|
||||
)
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
default=datetime.utcnow
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
|
||||
# Relationships
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional, Any
|
||||
from sqlalchemy import String, Text, DateTime, ForeignKey, Boolean, Integer, Index
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
@@ -33,13 +33,13 @@ class Tree(Base):
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
||||
version: Mapped[int] = mapped_column(Integer, default=1)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
default=datetime.utcnow
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
default=datetime.utcnow,
|
||||
onupdate=datetime.utcnow
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc),
|
||||
onupdate=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
usage_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user