-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
188 lines (160 loc) · 4.81 KB
/
main.go
File metadata and controls
188 lines (160 loc) · 4.81 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
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"time"
"github.com/willibrandon/mtlog"
"github.com/willibrandon/mtlog/core"
"github.com/willibrandon/mtlog/sinks"
)
// Order represents a sample order
type Order struct {
ID string
CustomerID int
Total float64
Items []OrderItem
Status string
CreatedAt time.Time
}
// OrderItem represents an item in an order
type OrderItem struct {
ProductID string
ProductName string
Quantity int
Price float64
}
func main() {
// Example 1: Basic Seq integration
fmt.Println("=== Basic Seq Integration ===")
basicExample()
// Example 2: Seq with API key and batching
fmt.Println("\n=== Seq with API Key ===")
apiKeyExample()
// Example 3: Advanced Seq configuration
fmt.Println("\n=== Advanced Seq Configuration ===")
advancedExample()
// Example 4: Structured logging to Seq
fmt.Println("\n=== Structured Logging to Seq ===")
structuredExample()
}
func basicExample() {
// Create logger with Seq sink
log := mtlog.New(
mtlog.WithConsoleProperties(), // Also log to console
mtlog.WithSeq("http://localhost:5341"),
mtlog.WithMinimumLevel(core.DebugLevel),
mtlog.WithMachineName(),
mtlog.WithTimestamp(),
)
// Simple logging
log.Information("Application started")
log.Debug("Debug information: {DebugValue}", 42)
log.Warning("This is a warning with {Count} items", 5)
// Ensure logs are flushed
time.Sleep(100 * time.Millisecond)
}
func apiKeyExample() {
// Create logger with authenticated Seq sink
log := mtlog.New(
mtlog.WithSeqAPIKey("http://localhost:5341", "your-api-key-here"),
mtlog.WithConsoleProperties(),
mtlog.WithProperty("Application", "mtlog-example"),
mtlog.WithProperty("Environment", "development"),
mtlog.WithMachineName(),
)
// Log with context
requestLogger := log.ForContext("RequestId", "req-123")
requestLogger.Information("Processing request")
requestLogger.Information("Request completed in {Duration}ms", 145)
}
func advancedExample() {
// Create logger with advanced Seq configuration
log := mtlog.New(
mtlog.WithSeqAdvanced("http://localhost:5341",
sinks.WithSeqAPIKey("your-api-key"),
sinks.WithSeqBatchSize(50),
sinks.WithSeqBatchTimeout(2*time.Second),
sinks.WithSeqCompression(true),
sinks.WithSeqRetry(3, time.Second),
),
mtlog.WithConsoleProperties(),
mtlog.WithEnricher(&customEnricher{}),
mtlog.WithCapturing(),
)
// Log some events
for i := 0; i < 10; i++ {
log.Information("Event {EventNumber} of {Total}", i+1, 10)
time.Sleep(50 * time.Millisecond)
}
// Wait for batch to flush
time.Sleep(3 * time.Second)
}
func structuredExample() {
// Create logger for structured data
log := mtlog.New(
mtlog.WithSeq("http://localhost:5341"),
mtlog.WithConsoleProperties(),
mtlog.WithCapturing(),
mtlog.WithTimestamp(),
mtlog.WithCallersInfo(),
)
// Create sample order
order := Order{
ID: "ORD-12345",
CustomerID: 789,
Total: 299.99,
Status: "pending",
CreatedAt: time.Now(),
Items: []OrderItem{
{
ProductID: "PROD-001",
ProductName: "Widget Pro",
Quantity: 2,
Price: 49.99,
},
{
ProductID: "PROD-002",
ProductName: "Gadget Max",
Quantity: 1,
Price: 199.99,
},
},
}
// Log structured data - Seq will index all properties
log.Information("Order placed: {@Order}", order)
// Log with multiple properties
log.Information("Processing order {OrderId} for customer {CustomerId} with {ItemCount} items",
order.ID, order.CustomerID, len(order.Items))
// Error scenario
err := processOrder(order)
if err != nil {
log.Error("Failed to process order {OrderId}: {Error}",
order.ID, err)
}
// Performance logging
start := time.Now()
// Simulate work
time.Sleep(150 * time.Millisecond)
duration := time.Since(start)
log.Information("Order {OrderId} processed in {Duration}",
order.ID, duration)
// Using different log levels
log.Verbose("Detailed order information: {@Order}", order)
log.Debug("Order status: {Status}", order.Status)
log.Warning("Order {OrderId} total {Total} exceeds threshold",
order.ID, order.Total)
// Wait for final flush
time.Sleep(time.Second)
}
// processOrder simulates order processing
func processOrder(order Order) error {
if order.Total > 1000 {
return fmt.Errorf("order total exceeds maximum allowed amount")
}
return nil
}
// customEnricher adds custom properties to all events
type customEnricher struct{}
func (e *customEnricher) Enrich(event *core.LogEvent, factory core.LogEventPropertyFactory) {
event.AddPropertyIfAbsent(factory.CreateProperty("Version", "1.0.0"))
event.AddPropertyIfAbsent(factory.CreateProperty("Component", "seq-example"))
}