-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
84 lines (68 loc) · 2.64 KB
/
example.py
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
import time
from typing import List, Dict, Any
import random
# Import the PieLogger and PieLogLevel
from pretty_pie_log import PieLogger, PieLogLevel
# Create a logger instance with custom configuration
logger = PieLogger(
logger_name="TestLogger",
minimum_log_level=PieLogLevel.DEBUG,
colorful=True
)
# Example class with complex data for testing details logging
class TestDataClass:
def __init__(self, name: str, value: int):
self.name = name
self.value = value
def __repr__(self):
return f"TestDataClass(name={self.name}, value={self.value})"
# Function decorated with log_execution to demonstrate function logging
@logger.log_execution(
start_message="Starting complex data processing",
end_message="Data processing completed",
print_args_at_start=True,
print_result_at_end=True
)
def process_complex_data(data: List[int]) -> Dict[str, Any]:
"""Simulate some data processing with a complex return"""
time.sleep(1) # Simulate processing time
return {
"original_length": len(data),
"sum": sum(data),
"max": max(data),
"min": min(data),
"processed_object": TestDataClass("result", len(data))
}
def main():
# Demonstrate different log levels
logger.debug("This is a debug message", details={"key": "debug_value"})
logger.info("This is an info message", details={"user_id": 12345})
logger.warning("This is a warning message", details={"warning_code": "W001"})
logger.error("This is an error message", details={"error_code": "E123"})
logger.critical("This is a critical message", details={"critical_event": "System failure"})
# Demonstrate error logging with exception trace
try:
x = 1 / 0 # Intentional division by zero
except:
logger.exception("This is an exception message")
# Demonstrate critical logging
logger.critical("This is a critical message",
details={"critical_event": "System failure simulation"})
# Test log with complex nested details
complex_details = {
"nested_dict": {
"list": [1, 2, 3],
"dict_in_dict": {"a": 1, "b": 2},
"set": {4, 5, 6}
},
"object": TestDataClass("test", 42)
}
logger.info("Logging complex nested details", details=complex_details)
# Demonstrate log_execution decorator
sample_data = [random.randint(1, 100) for _ in range(10)]
result = process_complex_data(sample_data)
# Test different color settings
logger.debug("Colored debug message", colorful=True)
logger.info("Black and white info message", colorful=False)
if __name__ == "__main__":
main()