
- Update Supervisor configuration to use python -m uvicorn for reliable module loading - Fix database path resolution using absolute paths - Add in-memory SQLite database option for testing and when file access is problematic - Improve error handling in database connection module - Enhance logging configuration with more detailed output - Add environment variables and documentation for better configuration options - Update troubleshooting guide with common issues and solutions
54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
import logging
|
|
import os
|
|
import sys
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
class LogConfig(BaseModel):
|
|
"""Logging configuration to be set for the server"""
|
|
|
|
LOGGER_NAME: str = "blogging_api"
|
|
LOG_FORMAT: str = "%(levelprefix)s | %(asctime)s | %(name)s | %(message)s"
|
|
LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO").upper()
|
|
|
|
# Logging config
|
|
version: int = 1
|
|
disable_existing_loggers: bool = False
|
|
formatters: dict = {
|
|
"default": {
|
|
"()": "uvicorn.logging.DefaultFormatter",
|
|
"fmt": LOG_FORMAT,
|
|
"datefmt": "%Y-%m-%d %H:%M:%S",
|
|
},
|
|
"detailed": {
|
|
"()": "uvicorn.logging.DefaultFormatter",
|
|
"fmt": "%(levelprefix)s | %(asctime)s | %(name)s | %(filename)s:%(lineno)d | %(message)s",
|
|
"datefmt": "%Y-%m-%d %H:%M:%S",
|
|
}
|
|
}
|
|
handlers: dict = {
|
|
"default": {
|
|
"formatter": "default",
|
|
"class": "logging.StreamHandler",
|
|
"stream": sys.stdout, # Use stdout instead of stderr for better supervisor compatibility
|
|
},
|
|
"detailed": {
|
|
"formatter": "detailed",
|
|
"class": "logging.StreamHandler",
|
|
"stream": sys.stdout,
|
|
}
|
|
}
|
|
loggers: dict = {
|
|
LOGGER_NAME: {"handlers": ["default"], "level": LOG_LEVEL, "propagate": False},
|
|
"uvicorn": {"handlers": ["default"], "level": LOG_LEVEL, "propagate": False},
|
|
"sqlalchemy": {"handlers": ["detailed"], "level": LOG_LEVEL, "propagate": False},
|
|
"app": {"handlers": ["default"], "level": LOG_LEVEL, "propagate": False},
|
|
"db": {"handlers": ["detailed"], "level": LOG_LEVEL, "propagate": False},
|
|
}
|
|
|
|
|
|
def get_logger(name: str = None) -> logging.Logger:
|
|
"""Get logger with the given name"""
|
|
name = name or LogConfig().LOGGER_NAME
|
|
return logging.getLogger(name) |