mirror of
https://github.com/Ap0dexMe0/NF-MSL.git
synced 2026-10-20 06:42:09 +02:00
refactored
This commit is contained in:
+60
-26
@@ -1,47 +1,81 @@
|
||||
"""
|
||||
logging.py — Colored logging setup for the NF-MSL project.
|
||||
logging.py — Centralized colored logging for the NF-MSL project.
|
||||
|
||||
All loggers in this project propagate to the root logger, which is configured
|
||||
once with coloredlogs. Named loggers (platform runners, MSL modules) only need
|
||||
their level set — they inherit formatting and color from the root handler.
|
||||
|
||||
Set the MSL_DEBUG=1 environment variable to enable DEBUG-level output.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
try:
|
||||
import coloredlogs
|
||||
COLOREDLOGS_AVAILABLE = True
|
||||
_COLOREDLOGS = True
|
||||
except ImportError:
|
||||
COLOREDLOGS_AVAILABLE = False
|
||||
_COLOREDLOGS = False
|
||||
|
||||
_ROOT_CONFIGURED = False
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Color scheme
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
LEVEL_STYLES: dict = {
|
||||
"debug": {"color": "blue"},
|
||||
"info": {"color": "green"},
|
||||
"warning": {"color": "yellow", "bold": True},
|
||||
"error": {"color": "red", "bold": True},
|
||||
"critical": {"color": "magenta", "bold": True},
|
||||
}
|
||||
|
||||
FIELD_STYLES: dict = {
|
||||
"name": {"color": "cyan", "bold": True},
|
||||
"levelname": {"color": "white", "bold": True},
|
||||
"asctime": {"color": "white"},
|
||||
"message": {},
|
||||
}
|
||||
|
||||
DEFAULT_FMT = "%(name)s - %(levelname)s - %(message)s"
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Public API
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def setup_logger(
|
||||
name: str,
|
||||
level: int = logging.INFO,
|
||||
fmt: Optional[str] = None,
|
||||
) -> logging.Logger:
|
||||
"""Create and return a logger with optional colored output.
|
||||
"""Return a named logger, configuring the root handler on first call.
|
||||
|
||||
If coloredlogs is installed, the logger will use colored output.
|
||||
Otherwise, falls back to standard logging with the specified format.
|
||||
All loggers propagate to a single root handler so that module-level
|
||||
loggers (``_log = logging.getLogger(__name__)``) automatically inherit
|
||||
the same coloredlogs formatting without extra setup.
|
||||
"""
|
||||
global _ROOT_CONFIGURED
|
||||
|
||||
if not _ROOT_CONFIGURED:
|
||||
root_level = logging.DEBUG if os.getenv("MSL_DEBUG") else logging.INFO
|
||||
_fmt = fmt or DEFAULT_FMT
|
||||
|
||||
if _COLOREDLOGS:
|
||||
coloredlogs.install(
|
||||
level=root_level,
|
||||
fmt=_fmt,
|
||||
level_styles=LEVEL_STYLES,
|
||||
field_styles=FIELD_STYLES,
|
||||
)
|
||||
else:
|
||||
logging.basicConfig(level=root_level, format=_fmt)
|
||||
|
||||
_ROOT_CONFIGURED = True
|
||||
|
||||
logger = logging.getLogger(name)
|
||||
if logger.handlers:
|
||||
return logger
|
||||
|
||||
if fmt is None:
|
||||
fmt = "%(name)s - %(levelname)s - %(message)s"
|
||||
|
||||
if COLOREDLOGS_AVAILABLE:
|
||||
coloredlogs.install(
|
||||
level=level,
|
||||
fmt=fmt,
|
||||
logger=logger,
|
||||
reconfigure=True,
|
||||
)
|
||||
else:
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(logging.Formatter(fmt))
|
||||
logger.addHandler(handler)
|
||||
logger.setLevel(level)
|
||||
|
||||
return logger
|
||||
logger.setLevel(level)
|
||||
return logger
|
||||
|
||||
Reference in New Issue
Block a user