-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.py
More file actions
176 lines (147 loc) · 5.54 KB
/
example2.py
File metadata and controls
176 lines (147 loc) · 5.54 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import logging
import time
import json
import traceback
from datetime import datetime
# Import ml3log and start the server
import ml3log
def main():
# Start the server
ml3log.start_server(port=6020)
# Monkey patch the standard logging module
ml3log.monkey_patch_logging(level=logging.DEBUG)
# Use the standard logging module
logging.info("This log is NOT sent to ML3Log")
# Create a logger using the standard logging module
logger = logging.getLogger("example_standard")
logger.info("This also works with named loggers")
# Log at different levels
log_levels = [
(logger.debug, "Debug message"),
(logger.info, "Info message"),
(logger.warning, "Warning message"),
(logger.error, "Error message"),
(logger.critical, "Critical message"),
]
# Demonstrate all log levels
logger.info("=== Demonstrating all log levels ===")
for i, (log_func, message) in enumerate(log_levels):
log_func(f"{message} #{i+1}")
time.sleep(0.5)
# Demonstrate all highlighting features
logger.info("=== Demonstrating all highlighting features with standard logging ===")
# JSON properties highlighting
sample_json = {
"user": "example_user",
"id": 12345,
"is_active": True,
"tags": ["important", "demo", "test"],
"metadata": {
"created_at": datetime.now().isoformat(),
"version": "1.0.0",
"settings": None,
},
}
logger.info("JSON example: %s", json.dumps(sample_json, indent=2))
# ISO timestamp highlighting
current_time = datetime.now().isoformat()
logger.info("ISO timestamp example: %s", current_time)
# File path highlighting (with and without line numbers)
logger.info(
"File path example: /Users/johndoe/ml3log/example2.py"
)
logger.info(
"File path with line number: /Users/johndoe/ml3log/example2.py:42"
)
# File path with quotes
logger.info(
"File path with quotes: '/Users/johndoe/ml3log/example2.py'"
)
logger.info(
'File path with double quotes: "/Users/johndoe/ml3log/example2.py:25"'
)
# URL highlighting (clickable)
logger.info("URL example: https://github.com/multinear/ml3log")
logger.info("Documentation URL: https://docs.python.org/3/library/logging.html")
# Combined example
logger.info(
"Combined example: timestamp %s, "
"file /Users/johndoe/ml3log/example2.py:50, "
"and URL https://multinear.com",
current_time,
)
# Try with another logger
another_logger = logging.getLogger("another_module")
another_logger.info("Logs from another module")
# Demonstrate exception logging
try:
1 / 0
except Exception as e:
logger.exception("Caught an exception: %s", str(e))
# Compare with direct ml3log usage
ml3_logger = ml3log.get_logger("direct_ml3log")
ml3_logger.info("This log is sent directly via ml3log")
# Also demonstrate highlighting with direct ml3log usage
ml3_logger.info(
"=== Demonstrating all highlighting features with direct ml3log ==="
)
ml3_logger.info("JSON example: %s", json.dumps(sample_json, indent=2))
ml3_logger.info(
"URL and file path: https://multinear.com and "
"/Users/johndoe/ml3log/logger.py:128"
)
# Demonstrate truncation for long log messages
logger.info("=== Demonstrating log truncation for long messages ===")
# Example 1: Long multi-line log message
long_text = "\n".join([f"This is line {i} of a very long log message" for i in range(1, 20)])
logger.info("Long multi-line message:\n%s", long_text)
# Example 2: Very long single line
long_single_line = "This is a very long single line log message that exceeds the character limit. " * 10
logger.info("Long single line: %s", long_single_line)
# Example 3: Long error traceback
try:
# Intentionally cause a deep error traceback
def level1(): return level2()
def level2(): return level3()
def level3(): return level4()
def level4(): return level5()
def level5(): return level6()
def level6(): return level7()
def level7(): return level8()
def level8(): return level9()
def level9(): return level10()
def level10(): return 1/0
level1()
except Exception as e:
error_traceback = "".join(traceback.format_exception(type(e), e, e.__traceback__))
logger.error("Complex error with long traceback:\n%s", error_traceback)
# Example 4: Long JSON output
large_json = {
"data": [
{
"id": i,
"name": f"Item {i}",
"attributes": {
"created": datetime.now().isoformat(),
"values": [j for j in range(20)],
"metadata": {
"description": "This is a very long description " * 5,
"tags": ["tag1", "tag2", "tag3", "tag4", "tag5"]
}
}
} for i in range(10)
]
}
logger.info("Large JSON payload:\n%s", json.dumps(large_json, indent=2))
print("\nOpen your browser at http://localhost:6020 to view logs")
print("Press Ctrl+C to exit.")
# Keep the main thread alive
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nRestoring original logging")
ml3log.restore_logging()
print("Exiting...")
if __name__ == "__main__":
main()