-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (62 loc) · 2.23 KB
/
main.go
File metadata and controls
77 lines (62 loc) · 2.23 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
package main
import (
"context"
"os"
"github.com/willibrandon/mtlog"
"github.com/willibrandon/mtlog/core"
"github.com/willibrandon/mtlog/internal/enrichers"
)
func main() {
// Set some environment variables for demonstration
os.Setenv("ENVIRONMENT", "production")
os.Setenv("SERVICE_NAME", "api-gateway")
os.Setenv("SERVICE_VERSION", "v1.2.3")
// Create logger with various enrichers
log := mtlog.New(
mtlog.WithConsoleProperties(), // Use console sink that shows properties
mtlog.WithCommonEnvironment(),
mtlog.WithMachineName(),
mtlog.WithProcess(),
mtlog.WithThreadId(),
mtlog.WithCallers(9), // Skip 9 frames to get to the actual caller
mtlog.Debug(),
)
log.Information("Application started with all enrichers")
// Demonstrate context-based logging
ctx := context.Background()
ctx = enrichers.WithCorrelationId(ctx, "req-123-456")
ctx = enrichers.WithRequestId(ctx, "req-789")
ctx = enrichers.WithUserId(ctx, "user-42")
ctx = enrichers.WithSessionId(ctx, "session-xyz")
// Create a logger with context
ctxLog := log.WithContext(ctx)
ctxLog.Information("Processing request with context")
// Simulate a service call
processOrder(ctxLog, "ORD-001")
// Demonstrate correlation ID enricher
correlatedLog := mtlog.New(
mtlog.WithConsoleProperties(),
mtlog.WithCorrelationId("batch-job-123"),
mtlog.Information(),
)
correlatedLog.Information("Starting batch job")
correlatedLog.Information("Processing item {ItemId}", "ITEM-001")
correlatedLog.Information("Batch job completed")
// Demonstrate custom context values
type contextKey string
const tenantKey contextKey = "tenant"
tenantCtx := context.WithValue(context.Background(), tenantKey, "acme-corp")
tenantLog := mtlog.New(
mtlog.WithConsoleProperties(),
mtlog.WithEnricher(enrichers.NewContextValueEnricher(tenantCtx, tenantKey, "TenantId")),
mtlog.Information(),
)
tenantLog.Information("Processing tenant-specific operation")
}
func processOrder(log core.Logger, orderId string) {
log.Information("Processing order {OrderId}", orderId)
// Simulate some work
log.Debug("Validating order {OrderId}", orderId)
log.Debug("Checking inventory for order {OrderId}", orderId)
log.Information("Order {OrderId} processed successfully", orderId)
}