Refine Engine bootstrap logging

This commit is contained in:
2025-10-22 04:31:07 -06:00
parent e0b7f15460
commit 29c5edd932
14 changed files with 376 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
"""Configuration primitives for the Borealis Engine."""
from __future__ import annotations
from .environment import EngineSettings, load_environment
from .logging import configure_logging
__all__ = [
"EngineSettings",
"load_environment",
"configure_logging",
]

View File

@@ -0,0 +1,87 @@
"""Environment detection for the Borealis Engine."""
from __future__ import annotations
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable, Tuple
@dataclass(frozen=True, slots=True)
class EngineSettings:
"""Immutable container describing the Engine runtime configuration."""
project_root: Path
database_path: Path
static_root: Path
cors_allowed_origins: Tuple[str, ...]
secret_key: str
debug: bool
host: str
port: int
@property
def logs_root(self) -> Path:
"""Return the directory where Engine-specific logs should live."""
return self.project_root / "Logs" / "Server"
def _resolve_project_root() -> Path:
candidate = os.getenv("BOREALIS_ROOT")
if candidate:
return Path(candidate).expanduser().resolve()
return Path(__file__).resolve().parents[2]
def _resolve_database_path(project_root: Path) -> Path:
candidate = os.getenv("BOREALIS_DATABASE_PATH")
if candidate:
return Path(candidate).expanduser().resolve()
return (project_root / "database.db").resolve()
def _resolve_static_root(project_root: Path) -> Path:
candidate = os.getenv("BOREALIS_STATIC_ROOT")
if candidate:
return Path(candidate).expanduser().resolve()
return (project_root / "Data" / "Server" / "dist").resolve()
def _parse_origins(raw: str | None) -> Tuple[str, ...]:
if not raw:
return ("*",)
parts: Iterable[str] = (segment.strip() for segment in raw.split(","))
filtered = tuple(part for part in parts if part)
return filtered or ("*",)
def load_environment() -> EngineSettings:
"""Load Engine settings from environment variables and filesystem hints."""
project_root = _resolve_project_root()
database_path = _resolve_database_path(project_root)
static_root = _resolve_static_root(project_root)
cors_allowed_origins = _parse_origins(os.getenv("BOREALIS_CORS_ALLOWED_ORIGINS"))
secret_key = os.getenv("BOREALIS_FLASK_SECRET_KEY", "change-me")
debug = os.getenv("BOREALIS_DEBUG", "false").lower() in {"1", "true", "yes", "on"}
host = os.getenv("BOREALIS_HOST", "127.0.0.1")
try:
port = int(os.getenv("BOREALIS_PORT", "5000"))
except ValueError:
port = 5000
return EngineSettings(
project_root=project_root,
database_path=database_path,
static_root=static_root,
cors_allowed_origins=cors_allowed_origins,
secret_key=secret_key,
debug=debug,
host=host,
port=port,
)
__all__ = ["EngineSettings", "load_environment"]

View File

@@ -0,0 +1,71 @@
"""Logging bootstrap helpers for the Borealis Engine."""
from __future__ import annotations
import logging
from logging.handlers import TimedRotatingFileHandler
from pathlib import Path
from .environment import EngineSettings
_ENGINE_LOGGER_NAME = "borealis.engine"
_SERVICE_NAME = "engine"
_DEFAULT_FORMAT = "%(asctime)s-" + _SERVICE_NAME + "-%(message)s"
def _handler_already_attached(logger: logging.Logger, log_path: Path) -> bool:
for handler in logger.handlers:
if isinstance(handler, TimedRotatingFileHandler):
handler_path = Path(getattr(handler, "baseFilename", ""))
if handler_path == log_path:
return True
return False
def _build_handler(log_path: Path) -> TimedRotatingFileHandler:
handler = TimedRotatingFileHandler(
log_path,
when="midnight",
backupCount=30,
encoding="utf-8",
)
handler.setLevel(logging.INFO)
handler.setFormatter(logging.Formatter(_DEFAULT_FORMAT))
return handler
def configure_logging(settings: EngineSettings) -> logging.Logger:
"""Configure a rotating log handler for the Engine."""
logs_root = settings.logs_root
logs_root.mkdir(parents=True, exist_ok=True)
log_path = logs_root / "engine.log"
logger = logging.getLogger(_ENGINE_LOGGER_NAME)
logger.setLevel(logging.INFO if not settings.debug else logging.DEBUG)
if not _handler_already_attached(logger, log_path):
handler = _build_handler(log_path)
logger.addHandler(handler)
logger.propagate = False
# Also ensure the root logger follows suit so third-party modules inherit the handler.
root_logger = logging.getLogger()
if not _handler_already_attached(root_logger, log_path):
handler = _build_handler(log_path)
root_logger.addHandler(handler)
if root_logger.level == logging.WARNING:
# Default level is WARNING; lower it to INFO so our handler captures application messages.
root_logger.setLevel(logging.INFO if not settings.debug else logging.DEBUG)
# Quieten overly chatty frameworks unless debugging is explicitly requested.
if not settings.debug:
logging.getLogger("werkzeug").setLevel(logging.WARNING)
logging.getLogger("engineio").setLevel(logging.WARNING)
logging.getLogger("socketio").setLevel(logging.WARNING)
return logger
__all__ = ["configure_logging"]