-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
170 lines (129 loc) · 4.57 KB
/
Copy pathmain.go
File metadata and controls
170 lines (129 loc) · 4.57 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
// Package main demonstrates using LogContext for scoped properties.
package main
import (
"context"
"fmt"
"time"
"github.com/willibrandon/mtlog"
"github.com/willibrandon/mtlog/core"
)
func main() {
// Create a logger with console output that shows properties
logger := mtlog.New(
mtlog.WithConsoleProperties(),
mtlog.WithMinimumLevel(core.DebugLevel),
)
fmt.Println("=== LogContext Example ===")
fmt.Println()
// Simulate a web service handling requests
simulateWebService(logger)
fmt.Println()
fmt.Println("=== Nested Context Example ===")
fmt.Println()
// Demonstrate nested contexts
demonstrateNestedContexts(logger)
}
func simulateWebService(logger core.Logger) {
// Simulate handling an HTTP request
ctx := context.Background()
// Add request-level properties that will be included in all logs
ctx = mtlog.PushProperty(ctx, "RequestId", "req-12345")
ctx = mtlog.PushProperty(ctx, "ClientIP", "192.168.1.100")
ctx = mtlog.PushProperty(ctx, "Method", "POST")
ctx = mtlog.PushProperty(ctx, "Path", "/api/users")
// Create a logger that includes context properties
log := logger.WithContext(ctx)
log.Information("Received HTTP request")
// Simulate user authentication
userId := authenticateUser(ctx, log)
if userId != "" {
// Add user context
ctx = mtlog.PushProperty(ctx, "UserId", userId)
log = logger.WithContext(ctx) // Recreate logger with updated context
log.Information("User authenticated successfully")
// Process the request
processUserRequest(ctx, log, userId)
}
log.Information("Request completed")
}
func authenticateUser(ctx context.Context, log core.Logger) string {
log.Debug("Authenticating user")
// Simulate authentication delay
time.Sleep(50 * time.Millisecond)
// Simulate successful authentication
return "user-789"
}
func processUserRequest(ctx context.Context, log core.Logger, userId string) {
// Add operation-specific context
ctx = mtlog.PushProperty(ctx, "Operation", "UpdateProfile")
opLog := log.WithContext(ctx)
opLog.Debug("Loading user profile")
time.Sleep(30 * time.Millisecond)
opLog.Information("Updating user profile")
time.Sleep(50 * time.Millisecond)
// Simulate validation
if err := validateProfileUpdate(ctx, opLog); err != nil {
opLog.Warning("Profile validation failed: {Error}", err)
return
}
opLog.Information("Profile updated successfully")
}
func validateProfileUpdate(ctx context.Context, log core.Logger) error {
// Add validation context
ctx = mtlog.PushProperty(ctx, "ValidationStep", "EmailFormat")
valLog := log.WithContext(ctx)
valLog.Debug("Validating email format")
// Simulate validation with a 30% chance of failure for demonstration
if time.Now().UnixNano()%10 < 3 {
err := fmt.Errorf("invalid email format: missing @ symbol")
valLog.Error("Validation failed: {Error}", err)
return err
}
valLog.Debug("Email format validation passed")
return nil
}
func demonstrateNestedContexts(logger core.Logger) {
// Base context with tenant information
ctx := context.Background()
ctx = mtlog.PushProperty(ctx, "TenantId", "acme-corp")
ctx = mtlog.PushProperty(ctx, "Environment", "production")
// Simulate processing jobs for a tenant
processJobs(ctx, logger)
}
func processJobs(ctx context.Context, logger core.Logger) {
// Add job batch context
ctx = mtlog.PushProperty(ctx, "BatchId", "batch-001")
ctx = mtlog.PushProperty(ctx, "BatchSize", 3)
batchLog := logger.WithContext(ctx)
batchLog.Information("Starting job batch processing")
// Process individual jobs
for i := 1; i <= 3; i++ {
// Each job gets its own context with job-specific properties
jobCtx := mtlog.PushProperty(ctx, "JobId", fmt.Sprintf("job-%03d", i))
jobCtx = mtlog.PushProperty(jobCtx, "JobType", getJobType(i))
jobLog := logger.WithContext(jobCtx)
processJob(jobCtx, jobLog, i)
}
batchLog.Information("Job batch completed")
}
func getJobType(jobNum int) string {
types := []string{"DataImport", "ReportGeneration", "EmailNotification"}
return types[(jobNum-1)%len(types)]
}
func processJob(ctx context.Context, log core.Logger, jobNum int) {
log.Information("Starting job")
// Simulate job processing
time.Sleep(time.Duration(20+jobNum*10) * time.Millisecond)
// Add progress information
ctx = mtlog.PushProperty(ctx, "Progress", "50%")
midLog := log.WithContext(ctx)
midLog.Debug("Job halfway complete")
// Complete the job
time.Sleep(time.Duration(20+jobNum*10) * time.Millisecond)
// Some jobs might have warnings
if jobNum == 2 {
log.Warning("Job completed with warnings: {WarningCount}", 3)
} else {
log.Information("Job completed successfully")
}
}