-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlogging_config.py
More file actions
115 lines (89 loc) · 3.42 KB
/
logging_config.py
File metadata and controls
115 lines (89 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
"""Centralized logging configuration for FastSM."""
import logging
import logging.handlers
import os
import sys
from typing import Optional
# Global logger instance
_logger: Optional[logging.Logger] = None
_config_dir: Optional[str] = None
def setup_logging(config_dir: str, debug: bool = False) -> logging.Logger:
"""Initialize application-wide logging.
Args:
config_dir: Directory to store log files
debug: If True, log at DEBUG level; otherwise INFO
Returns:
Configured logger instance
"""
global _logger, _config_dir
_config_dir = config_dir
# Create or get the fastsm logger
logger = logging.getLogger('fastsm')
# Clear any existing handlers (in case of re-initialization)
logger.handlers.clear()
# Set base level (handlers can filter further)
logger.setLevel(logging.DEBUG)
# Ensure config directory exists
os.makedirs(config_dir, exist_ok=True)
# File handler with rotation (5MB max, keep 3 backups)
log_file = os.path.join(config_dir, 'fastsm.log')
try:
file_handler = logging.handlers.RotatingFileHandler(
log_file,
maxBytes=5 * 1024 * 1024, # 5MB
backupCount=3,
encoding='utf-8'
)
file_handler.setLevel(logging.DEBUG if debug else logging.INFO)
# Detailed format for file (timestamp at end for easier reading)
file_formatter = logging.Formatter(
'%(name)s - %(levelname)s - %(module)s:%(lineno)d - %(message)s - %(asctime)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
except Exception as e:
# If we can't create the file handler, log to stderr
print(f"Warning: Could not create log file: {e}", file=sys.stderr)
# Console handler for errors only (keeps stderr clean)
console_handler = logging.StreamHandler(sys.stderr)
console_handler.setLevel(logging.ERROR)
console_formatter = logging.Formatter('%(levelname)s: %(message)s')
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)
# Prevent propagation to root logger
logger.propagate = False
_logger = logger
logger.info(f"Logging initialized (debug={debug})")
return logger
def get_logger(name: Optional[str] = None) -> logging.Logger:
"""Get a logger instance.
Args:
name: Optional name for a child logger (e.g., 'fastsm.api')
If None, returns the main fastsm logger
Returns:
Logger instance
"""
if name:
return logging.getLogger(f'fastsm.{name}')
return logging.getLogger('fastsm')
def set_debug_mode(enabled: bool) -> None:
"""Enable or disable debug logging at runtime.
Args:
enabled: If True, set file handler to DEBUG level; otherwise INFO
"""
logger = logging.getLogger('fastsm')
for handler in logger.handlers:
if isinstance(handler, logging.handlers.RotatingFileHandler):
handler.setLevel(logging.DEBUG if enabled else logging.INFO)
logger.info(f"Debug logging {'enabled' if enabled else 'disabled'}")
break
def get_log_file_path() -> Optional[str]:
"""Get the path to the current log file.
Returns:
Path to log file, or None if logging not initialized
"""
if _config_dir:
return os.path.join(_config_dir, 'fastsm.log')
return None