Virtual-LPU / lpu /monitoring /logger.py
iparaco's picture
Upload 118 files
e59bd9d verified
Raw History Blame Contribute Delete
5.4 kB
"""Rotating structured logging for every LPU component.
Controller-footprint safe: imports only stdlib. JSON-lines on disk keep the
logs machine-parseable for diagnose scripts; a plain console handler is added
for foreground commands.
"""
from __future__ import annotations
import json
import logging
import logging.handlers
import os
import sys
import time
from pathlib import Path
from typing import Optional
_LOGGERS: dict = {}
def _user_state_dir() -> Path:
"""Per-user writable state dir honoring Windows conventions."""
if os.name == "nt":
local = os.environ.get("LOCALAPPDATA")
if local:
return Path(local) / "LPU"
return Path.home() / ".lpu_home"
def _writable(path: Path) -> bool:
try:
path.mkdir(parents=True, exist_ok=True)
probe = path / ".write_probe"
probe.write_text("ok", encoding="utf-8")
probe.unlink()
return True
except OSError:
return False
def lpu_home() -> Path:
"""Canonical LPU state directory (logs, sessions, lock, reports).
Resolution order:
1. LPU_HOME env
2. runtime.home_dir from lpu_config.yaml (when found next to the package)
3. <install_root>/.lpu_home when writable (normal install layout)
4. %LOCALAPPDATA%\\LPU (Windows) or ~/.lpu_home - NEVER the drive root
or Program Files, so no admin privileges are ever required.
"""
env = os.environ.get("LPU_HOME")
if env:
return Path(env)
root = Path(__file__).resolve().parent.parent
try:
while root.parent != root and not (root / "lpu_config.yaml").exists():
root = root.parent
if (root / "lpu_config.yaml").exists():
import yaml # controller footprint allows PyYAML
try:
with open(root / "lpu_config.yaml", "r", encoding="utf-8") as fh:
data = yaml.safe_load(fh) or {}
cfg_dir = (data.get("runtime") or {}).get("home_dir")
if cfg_dir:
return Path(cfg_dir)
except Exception: # noqa: BLE001 - config problems must not kill logging
pass
candidate = root / ".lpu_home"
if _writable(candidate):
return candidate
return _user_state_dir()
# no config found (e.g. pip-installed layout): per-user dir, not root
return _user_state_dir()
except Exception: # noqa: BLE001
return _user_state_dir()
def default_log_dir() -> Path:
return lpu_home() / "logs"
def get_logger(
name: str,
log_dir: Optional[os.PathLike] = None,
level: str = "INFO",
max_mb: float = 10.0,
backups: int = 5,
console: bool = True,
) -> logging.Logger:
"""Idempotent logger factory with rotation + JSON-lines file handler."""
key = (name, str(log_dir))
if key in _LOGGERS:
return _LOGGERS[key]
logger = logging.getLogger(f"lpu.{name}")
logger.setLevel(getattr(logging, level.upper(), logging.INFO))
logger.propagate = False
if not logger.handlers:
d = Path(log_dir) if log_dir else default_log_dir()
try:
d.mkdir(parents=True, exist_ok=True)
fh = logging.handlers.RotatingFileHandler(
d / f"{name}.log",
maxBytes=int(max_mb * 1024 * 1024),
backupCount=backups,
encoding="utf-8",
)
fh.setFormatter(
logging.Formatter(
"%(asctime)s|%(levelname)s|%(name)s|%(message)s", "%Y-%m-%dT%H:%M:%S"
)
)
logger.addHandler(fh)
except Exception: # noqa: BLE001 - read-only FS etc: console only
pass
if console:
ch = logging.StreamHandler(sys.stderr)
ch.setFormatter(logging.Formatter("[lpu] %(levelname)s %(message)s"))
logger.addHandler(ch)
_LOGGERS[key] = logger
return logger
class JsonRecorder:
"""Append-only JSONL event recorder (metrics, session reports).
Writes are atomic-per-line and flushed, so the controller can tail the
file while the target process is still running. Used by monitoring and
the IPC session channel.
"""
def __init__(self, path: os.PathLike):
self.path = Path(path)
self.path.parent.mkdir(parents=True, exist_ok=True)
self._fh = open(self.path, "a", encoding="utf-8")
def write(self, record: dict) -> None:
record = {"ts": round(time.time(), 3), **record}
self._fh.write(json.dumps(record, ensure_ascii=False, default=str) + "\n")
self._fh.flush()
def close(self) -> None:
try:
self._fh.close()
except Exception: # noqa: BLE001
pass
def __enter__(self) -> "JsonRecorder":
return self
def __exit__(self, *exc) -> None:
self.close()
def read_jsonl(path: os.PathLike):
"""Yield parsed records from a JSONL file, skipping torn tail lines."""
p = Path(path)
if not p.exists():
return
with open(p, "r", encoding="utf-8") as fh:
for line in fh:
line = line.strip()
if not line:
continue
try:
yield json.loads(line)
except json.JSONDecodeError:
continue # torn write from a live process; skip