-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
153 lines (131 loc) · 3.4 KB
/
Copy pathmain.go
File metadata and controls
153 lines (131 loc) · 3.4 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
package main
import (
"time"
"github.com/willibrandon/mtlog"
)
type User struct {
ID int
Username string
Email string `log:"email"`
Password string `log:"-"` // Exclude from logs
CreatedAt time.Time
Profile UserProfile
Tags []string
Settings map[string]any
}
type UserProfile struct {
FirstName string
LastName string
Age int
Address Address
}
type Address struct {
Street string
City string
Country string
ZipCode string
}
func main() {
// Example 1: Basic capturing
log1 := mtlog.New(
mtlog.WithConsoleProperties(),
mtlog.WithCapturing(),
)
user := User{
ID: 123,
Username: "alice",
Email: "alice@example.com",
Password: "secret123", // This won't be logged
CreatedAt: time.Now(),
Profile: UserProfile{
FirstName: "Alice",
LastName: "Smith",
Age: 28,
Address: Address{
Street: "123 Main St",
City: "Anytown",
Country: "USA",
ZipCode: "12345",
},
},
Tags: []string{"premium", "verified", "developer"},
Settings: map[string]any{
"theme": "dark",
"notifications": true,
"language": "en",
},
}
log1.Information("User logged in: {@User}", user)
// Example 2: Capturing with limits
log2 := mtlog.New(
mtlog.WithConsoleProperties(),
mtlog.WithCustomCapturing(2, 50, 5), // Max depth 2, strings truncated at 50 chars, max 5 items in collections
)
// Create a large dataset
largeData := struct {
LongText string
Numbers []int
Nested map[string]any
}{
LongText: "This is a very long text that should be truncated because it exceeds the maximum string length limit",
Numbers: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, // Will be truncated to 5 items
Nested: map[string]any{
"level1": map[string]any{
"level2": map[string]any{
"level3": "This won't be fully captured due to depth limit",
},
},
},
}
log2.Information("Large data: {@Data}", largeData)
// Example 3: Without capturing (default behavior)
log3 := mtlog.New(
mtlog.WithConsoleProperties(),
// No capturing - complex objects will use default Go formatting
)
log3.Information("User without capturing: {User}", user)
// Example 4: Capturing with errors and special types
log4 := mtlog.New(
mtlog.WithConsoleProperties(),
mtlog.WithCapturing(),
)
type Response struct {
StatusCode int
Headers map[string]string
Body []byte
Error error
Duration time.Duration
Timestamp time.Time
}
resp := Response{
StatusCode: 200,
Headers: map[string]string{
"Content-Type": "application/json",
"X-Request-ID": "abc123",
},
Body: []byte(`{"status": "ok"}`),
Error: nil,
Duration: 150 * time.Millisecond,
Timestamp: time.Now(),
}
log4.Information("API response: {@Response}", resp)
// Example 5: Circular references (capturer should handle gracefully)
type Node struct {
Value int
Next *Node
}
// Create a simple linked list
node1 := &Node{Value: 1}
node2 := &Node{Value: 2}
node3 := &Node{Value: 3}
node1.Next = node2
node2.Next = node3
// node3.Next = node1 // Uncomment for circular reference
log4.Information("Linked list: {@List}", node1)
// Example 6: Interface values and nil handling
var items []any
items = append(items, "string", 123, true, nil, 3.14)
items = append(items, map[string]int{"a": 1, "b": 2})
items = append(items, struct{ Name string }{"test"})
log4.Information("Mixed types: {@Items}", items)
}