-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging_github.py
More file actions
70 lines (55 loc) · 2.72 KB
/
Copy pathlogging_github.py
File metadata and controls
70 lines (55 loc) · 2.72 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
import logging
import os
class DiagnosticSystem:
"""
A foundational system class showcasing standard logging configuration
with dual-routing (Console and File).
"""
def __init__(self, log_filename="system_logs.log"):
self.log_filename = log_filename
self._initialize_logger()
def _initialize_logger(self):
# Create a dedicated logger instance
self.logger = logging.getLogger("CoreApplication")
self.logger.setLevel(logging.DEBUG) # Capture everything at the root level
# Prevent duplicate logs if initialized multiple times
if self.logger.hasHandlers():
self.logger.handlers.clear()
# Create a File Handler (Captures detailed Debug logs)
file_handler = logging.FileHandler(self.log_filename)
file_handler.setLevel(logging.DEBUG)
file_formatter = logging.Formatter(
'%(asctime)s | %(levelname)-8s | [%(filename)s:%(lineno)d] | %(message)s'
)
file_handler.setFormatter(file_formatter)
# Create a Console Handler (Captures clean Info/Warning logs for terminal)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_formatter = logging.Formatter('%(levelname)s: %(message)s')
console_handler.setFormatter(console_formatter)
# Attach handlers to the logger
self.logger.addHandler(file_handler)
self.logger.addHandler(console_handler)
def process_transaction(self, amount, user_id):
"""
Simulates a real-world application operation utilizing different log levels.
"""
self.logger.debug(f"Starting transaction for User ID: {user_id} with Amount: ${amount}")
if amount <= 0:
self.logger.error(f"Transaction failed: Invalid amount ${amount} for User ID: {user_id}")
return False
if amount > 10000:
self.logger.warning(f"High-value transaction flagged for User ID: {user_id} (${amount})")
self.logger.info(f"Transaction successfully processed for User ID: {user_id}")
return True
def main():
# Initialize the system
sys_monitor = DiagnosticSystem()
print("--- Executing Script (Watch the Console Output) ---\n")
# Run some test cases
sys_monitor.process_transaction(250, "user_01") # Normal behavior
sys_monitor.process_transaction(15000, "user_02") # Triggers a warning
sys_monitor.process_transaction(-50, "user_03") # Triggers an error
print(f"\n--- Script Finished. Check '{sys_monitor.log_filename}' for full DEBUG logs. ---")
if __name__ == "__main__":
main()