apac-ops-bot/backend/app/utils/logger.py
SamoilenkoVadym a8151fbe66 Add comprehensive backend test suite and Phase 1 foundation
Backend Tests:
- Add pytest configuration with async support (conftest.py)
- Add model tests: User, Conversation, Message, TokenUsage, Session, UserMemory
- Add configuration tests: Settings validation and environment variables
- Add API tests: Health endpoint and future endpoint stubs
- Add database tests: Connection, transactions, query execution

Phase 1 Foundation:
- FastAPI application structure with main.py
- SQLAlchemy async models for all entities
- Alembic migrations setup
- Configuration management via Pydantic Settings
- Logging system (English only)
- Docker multi-stage builds for backend
- Docker Compose orchestration (PostgreSQL, Redis, backend)
- Frontend React + TypeScript structure
- Dark & Gold theme CSS implementation
- Environment configuration examples

All code and comments in English as per requirements.
Tests cover model relationships, cascade deletes, and constraints.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-27 14:24:59 +00:00

122 lines
2.7 KiB
Python

"""
Logging configuration for the application
All logs are in English only. Provides structured logging
with appropriate log levels and formatting.
"""
import logging
import sys
from typing import Any
from app.config import settings
def setup_logger(name: str = __name__) -> logging.Logger:
"""
Setup and configure logger with appropriate handlers and formatters
Args:
name: Logger name (usually __name__ of the module)
Returns:
Configured logger instance
"""
logger = logging.getLogger(name)
# Set log level based on environment
if settings.DEBUG:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
# Create console handler
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.DEBUG if settings.DEBUG else logging.INFO)
# Create formatter
formatter = logging.Formatter(
fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
)
# Add formatter to handler
console_handler.setFormatter(formatter)
# Add handler to logger
if not logger.handlers:
logger.addHandler(console_handler)
# Prevent propagation to root logger
logger.propagate = False
return logger
# Create default logger instance
logger = setup_logger("apac_ops_bot")
def log_api_call(endpoint: str, method: str, **kwargs: Any) -> None:
"""
Log API call with relevant information
Args:
endpoint: API endpoint being called
method: HTTP method
**kwargs: Additional information to log
"""
logger.info(
f"API Call: {method} {endpoint}",
extra=kwargs
)
def log_openai_request(model: str, tokens: int, **kwargs: Any) -> None:
"""
Log OpenAI API request
Args:
model: Model being used
tokens: Token count
**kwargs: Additional information
"""
logger.info(
f"OpenAI Request: model={model}, tokens={tokens}",
extra=kwargs
)
def log_error(error: Exception, context: str = "") -> None:
"""
Log error with context
Args:
error: Exception that occurred
context: Additional context about where error occurred
"""
logger.error(
f"Error in {context}: {str(error)}",
exc_info=True
)
def log_warning(message: str, **kwargs: Any) -> None:
"""
Log warning message
Args:
message: Warning message
**kwargs: Additional context
"""
logger.warning(message, extra=kwargs)
def log_debug(message: str, **kwargs: Any) -> None:
"""
Log debug message (only in DEBUG mode)
Args:
message: Debug message
**kwargs: Additional context
"""
logger.debug(message, extra=kwargs)