-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_logger.py
More file actions
73 lines (53 loc) · 2.26 KB
/
print_logger.py
File metadata and controls
73 lines (53 loc) · 2.26 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
import logging
import sys
class LoggingPrint:
def __init__(
self,
name = "app_logger",
log_file = None,
level = logging.DEBUG,
formatter_fmt = '[{asctime}] [{name}] {levelname}: {message}',
formatter_datefmt = '%Y/%m/%d, %I:%M %p',
formatter_style = '{'
):
self.logger = logging.getLogger(name)
self.logger.setLevel(level)
# Clear existing handlers to prevent duplicate logs if re-initialized
if self.logger.hasHandlers():
self.logger.handlers.clear()
# 1. Setup Formatter (Modern Style)
self.formatter = logging.Formatter(
fmt = formatter_fmt,
datefmt = formatter_datefmt,
style = formatter_style
)
# 2. Handler: Terminal (Always included)
self.console_handler = logging.StreamHandler(stream = sys.stdout)
self.console_handler.setFormatter(self.formatter)
self.logger.addHandler(self.console_handler)
# 3. Handler: File (Optional)
if log_file:
self.file_handler = logging.FileHandler(log_file)
self.file_handler.setFormatter(self.formatter)
self.logger.addHandler(self.file_handler)
def __call__(self, message, level=logging.INFO):
"""Allows the class instance to be called like a function."""
self.logger.log(level, message)
def add_custom_handler(self, handler, formatter=None):
"""Flexibility to add any custom handler (e.g., RotatingFile, Socket)."""
if formatter:
handler.setFormatter(formatter)
else:
handler.setFormatter(self.formatter)
self.logger.addHandler(handler)
# --- Usage ---
# 1. Standard usage (Terminal only)
print = LoggingPrint(name="ML_Pipeline")
print("Loading dataset...") # Defaults to INFO
# 2. Multi-destination (Terminal + File)
# Note: We name it print_to_file to avoid conflicting with the first one
# unless we specifically want to override it.
print = LoggingPrint(name="System", log_file="pipeline.log")
print("Starting training process...", level=logging.INFO)
print("Something looks off in the weights...", level=logging.WARNING)
print("CRITICAL: GPU OVERHEATING!", level=logging.CRITICAL)