Files
rengine/shared/logging.py
T
Yogesh OjhaandClaude Opus 5 2cb8d73280 style: strip explanatory comments across the codebase
The comment rule in CLAUDE.md is one line maximum, never narration: no
multi-line blocks, no module-header explainers, no sentences that explain or
justify the code to the reader. The tree had drifted a long way from it, and
most of the drift is mine.

Removed:
  232  lines of multi-line comment blocks
  203  single-line comments that argued instead of stating
  154  comments joined by so/because/otherwise/rather-than
  143  narrating comments in the frontend
  112  test docstrings (the test name already says what it asserts)
   25  test module docstrings and 25 decorative section dividers

Collapsed:
  324  multi-line docstrings to one factual line
  271  one-line docstrings that had a justifying clause bolted on

Kept: functional directives, section dividers, short field labels, alembic
revision headers, and the eight FastAPI route docstrings that render in /docs.
Where a fact was load-bearing it is in CLAUDE.md, which is where it belongs.

284 python tests and 34 frontend tests pass, ruff and ruff-format clean across
ten packages, svelte-check 0 errors, prettier and eslint clean, app.main imports.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013dKUV96WkQQETZBMoAG98C
2026-09-11 14:11:23 +05:30

91 lines
2.7 KiB
Python

import logging
import sys
from typing import ClassVar
class ColoredFormatter(logging.Formatter):
# ANSI color codes
COLORS: ClassVar[dict[str, str]] = {
"DEBUG": "\033[36m",
"INFO": "\033[32m",
"WARNING": "\033[33m",
"ERROR": "\033[31m",
"CRITICAL": "\033[35m",
"RESET": "\033[0m",
}
def format(self, record: logging.LogRecord) -> str:
color = self.COLORS.get(record.levelname, self.COLORS["RESET"])
reset = self.COLORS["RESET"]
record.levelname = f"{color}{record.levelname}{reset}"
return super().format(record)
def setup_logging(
name: str = "rengine",
level: str = "INFO",
colored: bool = True,
) -> logging.Logger:
logger = logging.getLogger(name)
logger.setLevel(getattr(logging, level.upper()))
logger.handlers.clear()
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.DEBUG)
if colored:
console_format = ColoredFormatter(
fmt="%(asctime)s | %(levelname)-8s | %(name)s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
else:
console_format = logging.Formatter(
fmt="%(asctime)s | %(levelname)-8s | %(name)s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
console_handler.setFormatter(console_format)
logger.addHandler(console_handler)
return logger
_RESERVED = frozenset({"exc_info", "stack_info", "stacklevel", "extra"})
class _FieldLogger(logging.LoggerAdapter):
"""Accepts structured fields as keywords and appends them to the message."""
def log(self, level, msg, *args, **kwargs):
fields = {k: v for k, v in kwargs.items() if k not in _RESERVED}
for key in fields:
kwargs.pop(key)
if fields:
rendered = " ".join(f"{k}={v}" for k, v in fields.items())
msg = f"{msg} | {rendered}"
super().log(level, msg, *args, **kwargs)
def debug(self, msg, *args, **kwargs):
self.log(logging.DEBUG, msg, *args, **kwargs)
def info(self, msg, *args, **kwargs):
self.log(logging.INFO, msg, *args, **kwargs)
def warning(self, msg, *args, **kwargs):
self.log(logging.WARNING, msg, *args, **kwargs)
def error(self, msg, *args, **kwargs):
self.log(logging.ERROR, msg, *args, **kwargs)
def exception(self, msg, *args, **kwargs):
kwargs.setdefault("exc_info", True)
self.log(logging.ERROR, msg, *args, **kwargs)
def critical(self, msg, *args, **kwargs):
self.log(logging.CRITICAL, msg, *args, **kwargs)
def get_logger(name: str) -> logging.Logger:
return _FieldLogger(logging.getLogger(name), {})