ReMe/reme2/utils/logger_utils.py
huangsen 514bf35050
Some checks failed
Pre-commit / run (ubuntu-latest) (push) Has been cancelled
```
docs: add ReMe2 architecture design documentation

- Add comprehensive design document (reme2.md) detailing the
  three-layer architecture (L1/L2/L3) for the vault system
- Document new protocols for folder notes and memory management
- Specify interface contracts for memory_* and vault_* tools
- Outline implementation phases from current state to target

refactor: fix typo in personal retriever class

- Correct spelling error: 'retri eved_nodes' -> 'retrieved_nodes'
  in PersonalRetriever.result assignment

chore: update gitignore with vault-related patterns

- Add '/vault' to ignore vault directory
- Add '/reme-plugin' to ignore plugin files
- Add '/reme2/vault' to ignore new vault implementation
```
2026-05-08 16:14:42 +08:00

86 lines
2.8 KiB
Python

"""Logging configuration module for application-wide tracing.
Provides a centralized logging facility using Loguru with support for
both console and file-based output, automatic rotation, and retention.
"""
import os
import sys
from datetime import datetime
from loguru import logger
_initialized = False
def get_logger(
log_dir: str = "logs",
level: str = "INFO",
log_to_console: bool = True,
log_to_file: bool = True,
force_init: bool = False,
):
"""Get a configured logger instance.
Automatically initializes on first call. Subsequent calls return
the same logger without re-initializing unless force_init=True.
This function configures the global Loguru logger with:
- Colorized console output (optional)
- File output with daily rotation (optional)
- 7-day retention with ZIP compression
- Consistent timestamp and location formatting
Args:
log_dir: Directory path for log files. Created if it doesn't exist.
Defaults to "logs" in the current working directory.
level: Logging level threshold. One of: DEBUG, INFO, WARNING,
ERROR, CRITICAL. Defaults to "INFO".
log_to_console: Whether to print logs to stdout. Defaults to True.
log_to_file: Whether to write logs to file. Defaults to True.
force_init: Force re-initialization even if already initialized.
Useful for changing log configuration mid-application.
Defaults to False.
Returns:
The configured Loguru logger instance.
"""
global _initialized
if _initialized and not force_init:
return logger
# Remove default handler to avoid duplicate logs
logger.remove()
# Configure colorized console logging if enabled
if log_to_console:
logger.add(
sink=sys.stdout,
level=level,
format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {file}:{line} | {function} | {message}",
colorize=True,
)
# Configure file-based logging if enabled
if log_to_file:
try:
os.makedirs(log_dir, exist_ok=True)
current_ts = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
log_filepath = os.path.join(log_dir, f"{current_ts}.log")
logger.add(
log_filepath,
level=level,
rotation="00:00", # Rotate at midnight
retention="7 days", # Keep logs for 7 days
compression="zip", # Compress rotated logs
encoding="utf-8",
format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {file}:{line} | {function} | {message}",
)
except Exception as e:
logger.error(f"Error configuring file logging: {e}")
_initialized = True
return logger