pyblock/pybitblock/log.py
GaltRanch 7366e9fe9c Refactor architecture: config singleton, logging, menu system, dependencies
Major improvements across 7 areas:

- Add centralized Config singleton (config.py) replacing ~176 config
  reloads per session with a single cached load
- Add logging framework (log.py) with RotatingFileHandler, replacing
  silent except Exception: pass blocks with structured logging
- Refactor menu system (menu.py) with data-driven color selection,
  eliminating ~1,370 lines of duplicate menu functions
- Create shared/ modules extracting 7 utility functions duplicated
  between PyBlock.py and SPV/spvblock.py
- Clean dependencies: pin all versions, remove stdlib packages
  (asyncio, threading), remove unused imports
- Improve Docker: pin ubuntu:24.04, add non-root user, use venv
- Improve CI: update to actions v4/v5, add test job before publish
- Fix entry point: wrap main loop in def main(), proper module import

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 10:58:24 -03:00

52 lines
1.4 KiB
Python

"""
Logging configuration for PyBLOCK.
Usage:
from log import get_logger
logger = get_logger(__name__)
logger.debug("detailed info")
logger.error("user-facing error: %s", e)
"""
import logging
import os
from logging.handlers import RotatingFileHandler
_configured = False
def _setup():
global _configured
if _configured:
return
_configured = True
log_dir = os.path.join(os.path.dirname(__file__), "config")
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, "pyblock.log")
root = logging.getLogger("pyblock")
root.setLevel(logging.DEBUG)
if not root.handlers:
file_handler = RotatingFileHandler(
log_file, maxBytes=1_048_576, backupCount=3, encoding="utf-8"
)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter(
"%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
))
root.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.WARNING)
console_handler.setFormatter(logging.Formatter(
"\033[1;31;40m[%(levelname)s]\033[0;37;40m %(message)s"
))
root.addHandler(console_handler)
def get_logger(name):
_setup()
return logging.getLogger(f"pyblock.{name}")