mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 01:58:41 +01:00
The registry imported every task module to read each task's title, schedule, queue and timeout, so `rq cron` and every RQ worker parent loaded the whole app. Task metadata now lives in the registry as `TaskSpec`s with a dotted path to the implementation, which `get_task` imports only when a job runs. Cron, the tasks API, enqueueing and the worker's failure reporting read specs only. Every log line also imported handler.metadata.base_handler, and with it every metadata provider, for the redaction regex. The sensitive keys and their regex now live in logger.formatter. A test pins that importing the cron config or the worker class loads neither handler.database, the auth handlers nor fastapi. Measured RSS at LOGLEVEL=INFO, master -> this branch: - rq cron: 164 MB -> 72 MB - idle RQ worker (x3): 133 MB -> 68 MB - gunicorn web worker: 214 MB -> 206 MB Each job's work horse now imports its task module itself, 0.1-0.5s more per job on an x86 host. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gh1SRMfuK7BZmf6YxMqSyT
185 lines
5.4 KiB
Python
185 lines
5.4 KiB
Python
import logging
|
|
import os
|
|
import re
|
|
from pprint import pformat
|
|
from typing import Final
|
|
|
|
from colorama import Fore, Style, init
|
|
|
|
from config import FORCE_COLOR, LOGLEVEL, NO_COLOR
|
|
|
|
RED = Fore.RED
|
|
LIGHTRED = Fore.LIGHTRED_EX
|
|
GREEN = Fore.GREEN
|
|
LIGHTYELLOW = Fore.LIGHTYELLOW_EX
|
|
YELLOW = Fore.YELLOW
|
|
BLUE = Fore.BLUE
|
|
CYAN = Fore.CYAN
|
|
LIGHTMAGENTA = Fore.LIGHTMAGENTA_EX
|
|
RESET = Fore.RESET
|
|
RESET_ALL = Style.RESET_ALL
|
|
|
|
LOGGING_CONFIG = {
|
|
"version": 1,
|
|
"disable_existing_loggers": False,
|
|
"formatters": {
|
|
"romm": {
|
|
"()": "logger.formatter.Formatter",
|
|
}
|
|
},
|
|
"handlers": {
|
|
"default": {
|
|
"formatter": "romm",
|
|
"class": "logging.StreamHandler",
|
|
"stream": "ext://sys.stdout",
|
|
}
|
|
},
|
|
"root": {
|
|
"handlers": ["default"],
|
|
"level": LOGLEVEL,
|
|
},
|
|
"loggers": {
|
|
"uvicorn": {
|
|
"level": LOGLEVEL,
|
|
"handlers": ["default"],
|
|
"propagate": False,
|
|
},
|
|
"uvicorn.error": {
|
|
"level": LOGLEVEL,
|
|
"handlers": ["default"],
|
|
"propagate": False,
|
|
},
|
|
"uvicorn.access": {
|
|
# Suppress HTTP access logs unless the user has explicitly opted into DEBUG logging.
|
|
"level": "DEBUG" if LOGLEVEL == "DEBUG" else "WARNING",
|
|
"handlers": ["default"],
|
|
"propagate": False,
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
# Strips ANSI SGR escapes (colors) embedded by `highlight()`; they render as
|
|
# colors on a terminal but as garbage anywhere else (e.g. a browser log view).
|
|
_ANSI_RE = re.compile(r"\x1b\[[0-9;]*m")
|
|
|
|
|
|
def strip_ansi(text: str) -> str:
|
|
"""Remove ANSI SGR (color) escape sequences from a string."""
|
|
return _ANSI_RE.sub("", text)
|
|
|
|
|
|
SENSITIVE_KEYS: Final = {
|
|
"Authorization",
|
|
"Client-ID",
|
|
"Client-Secret",
|
|
"client_id",
|
|
"client_secret",
|
|
"api_key",
|
|
"ssid",
|
|
"sspassword",
|
|
"devid",
|
|
"devpassword",
|
|
"y",
|
|
}
|
|
SENSITIVE_KEYS_REGEX: Final = re.compile(
|
|
rf"({'|'.join(re.escape(k) for k in SENSITIVE_KEYS)})=[^&\s\"]*",
|
|
re.IGNORECASE,
|
|
)
|
|
|
|
|
|
def redact_sensitive(text: str) -> str:
|
|
"""Mask sensitive values (api keys, tokens, …) in a log string."""
|
|
return SENSITIVE_KEYS_REGEX.sub(r"\1=***", text)
|
|
|
|
|
|
def resolve_module_name(record: logging.LogRecord) -> str:
|
|
"""Derive the semantic module name shown in logs.
|
|
|
|
Resolution order:
|
|
1. An explicit ``extra={"module_name": ...}`` on the log call — the manual
|
|
override (e.g. the scan handler groups all its lines under ``scan``).
|
|
2. The record's module: the source filename without its extension.
|
|
3. For package ``__init__`` files the bare filename carries no meaning
|
|
(``backend/endpoints/roms/__init__.py`` → ``__init__``); fall back to the
|
|
package — the parent directory name — so it reads as ``roms`` instead.
|
|
"""
|
|
explicit = getattr(record, "module_name", None)
|
|
if explicit is not None:
|
|
return str(explicit)
|
|
|
|
module = record.module
|
|
if module == "__init__" and record.pathname:
|
|
parent = os.path.basename(os.path.dirname(record.pathname))
|
|
if parent:
|
|
return parent
|
|
return module
|
|
|
|
|
|
def should_strip_ansi() -> bool:
|
|
"""Determine if ANSI escape codes should be stripped."""
|
|
# Check if an explicit environment variable is set to control color behavior
|
|
if FORCE_COLOR:
|
|
return False
|
|
if NO_COLOR:
|
|
return True
|
|
# Default: do not strip (Docker will handle colors)
|
|
return False
|
|
|
|
|
|
# Initialize Colorama once, considering different environments
|
|
init(strip=should_strip_ansi())
|
|
|
|
|
|
class Formatter(logging.Formatter):
|
|
"""
|
|
Logger formatter.
|
|
"""
|
|
|
|
def format(self, record: logging.LogRecord) -> str:
|
|
"""
|
|
Formats a log record with color-coded output based on the log level.
|
|
|
|
Args:
|
|
record: The log record to format.
|
|
|
|
Returns:
|
|
The formatted log record as a string.
|
|
"""
|
|
level = "%(levelname)s"
|
|
dots = f"{RESET}:"
|
|
module_label = resolve_module_name(record).lower()
|
|
identifier = f"\t {BLUE}[RomM]{LIGHTMAGENTA}[{module_label}]"
|
|
identifier_warning = f" {BLUE}[RomM]{LIGHTMAGENTA}[{module_label}]"
|
|
identifier_critical = f" {BLUE}[RomM]{LIGHTMAGENTA}[{module_label}]"
|
|
msg = f"{RESET_ALL}%(message)s"
|
|
|
|
message = pformat(record.msg) if hasattr(record, "pprint") else "%(message)s"
|
|
msg = f"{RESET_ALL}{message}"
|
|
date = f"{CYAN}[%(asctime)s] "
|
|
formats = {
|
|
logging.DEBUG: f"{LIGHTMAGENTA}{level}{dots}{identifier}{date}{msg}",
|
|
logging.INFO: f"{GREEN}{level}{dots}{identifier}{date}{msg}",
|
|
logging.WARNING: f"{YELLOW}{level}{dots}{identifier_warning}{date}{msg}",
|
|
logging.ERROR: f"{LIGHTRED}{level}{dots}{identifier}{date}{msg}",
|
|
logging.CRITICAL: f"{RED}{level}{dots}{identifier_critical}{date}{msg}",
|
|
}
|
|
log_fmt = formats.get(record.levelno)
|
|
formatter = logging.Formatter(fmt=log_fmt, datefmt="%Y-%m-%d %H:%M:%S")
|
|
output = formatter.format(record)
|
|
return redact_sensitive(output)
|
|
|
|
|
|
def highlight(msg: str = "", color: str = YELLOW) -> str:
|
|
"""
|
|
Highlights the message to send to the fancylog.
|
|
|
|
Args:
|
|
msg: Message to log.
|
|
color: Highlight with specific color. Available colors: RED, GREEN, YELLOW, BLUE.
|
|
|
|
Returns:
|
|
The highlighted message as a string.
|
|
"""
|
|
return f"{color}{msg}{RESET_ALL}"
|