-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
186 lines (148 loc) · 5.52 KB
/
example_test.go
File metadata and controls
186 lines (148 loc) · 5.52 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
package olog_test
import (
"context"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/global"
"github.com/pellared/olog"
)
func ExampleLogger() {
ctx := context.Background()
// Create a logger for events
logger := olog.New(olog.Options{Provider: global.GetLoggerProvider(), Name: "example"})
// Log structured events at different severity levels
logger.Info(ctx, "user.login",
"user.id", "12345",
"user.email", "user@example.com",
"session.id", "sess-abc123",
"client.ip", "192.168.1.100")
logger.Warn(ctx, "rate.limit.approached",
"client.ip", "192.168.1.100",
"requests_per_minute", 85,
"limit", 100)
logger.Error(ctx, "payment.failed",
"payment.id", "pay-xyz789",
"payment.amount", 99.99,
"payment.currency", "USD",
"user.id", "12345",
"error", "insufficient_funds")
// Check if debug event logging is enabled before expensive operations
if logger.DebugEnabled(ctx, "debug.session.details") {
logger.Debug(ctx, "debug.session.details",
"session.data", computeSessionDebugInfo(),
"trace.id", "trace-abc123")
}
}
func ExampleLogger_With() {
ctx := context.Background()
// Create a base logger
logger := olog.New(olog.Options{Provider: global.GetLoggerProvider(), Name: "example"})
// Pre-configure logger with common attributes for better performance
requestLogger := logger.With(
"service", "api-server",
"version", "1.2.3",
"request_id", generateRequestID(),
)
// Use the pre-configured logger for all request-scoped events
requestLogger.Info(ctx, "request.started", "method", "GET", "path", "/api/users")
requestLogger.Info(ctx, "database.query", "table", "users", "duration_ms", 23)
requestLogger.Info(ctx, "request.completed", "status", 200, "total_duration_ms", 145)
}
func computeSessionDebugInfo() string {
// Simulate expensive session debug data computation
return "session debug information"
}
func generateRequestID() string {
return "req-12345"
}
func ExampleNew_withOptions() {
ctx := context.Background()
// Create a logger using the new Options API
logger := olog.New(olog.Options{
Name: "my-service",
Version: "1.2.3",
Attributes: attribute.NewSet(
attribute.String("service.name", "user-service"),
attribute.String("deployment.environment", "production"),
attribute.Int("service.port", 8080),
),
})
// All event records will include the pre-configured attributes
logger.Info(ctx, "service.started", "status", "ready")
logger.Warn(ctx, "resource.high_usage", "memory_percent", 85.5)
logger.Error(ctx, "database.connection_failed", "retry_count", 3)
}
func ExampleNew_withGlobalProvider() {
ctx := context.Background()
// Create a logger that uses the global provider (provider is nil)
logger := olog.New(olog.Options{
Name: "global-logger",
Attributes: attribute.NewSet(
attribute.String("component", "authentication"),
attribute.String("version", "2.0.0"),
),
})
logger.Info(ctx, "logger.initialized", "uses_global_provider", true)
}
func ExampleNew_minimal() {
ctx := context.Background()
// Minimal configuration.
// The logger name is the caller's full package name.
logger := olog.New(olog.Options{})
logger.Info(ctx, "minimal.example")
}
func ExampleLogger_InfoAttr() {
ctx := context.Background()
// Create a logger for structured events
logger := olog.New(olog.Options{Provider: global.GetLoggerProvider(), Name: "example"})
// Log events at different severity levels using the attribute-based methods
logger.InfoAttr(ctx, "user.signup",
log.String("user.id", "user-789"),
log.String("user.email", "newuser@example.com"),
log.String("signup.method", "email"),
log.Bool("email.verified", false))
logger.WarnAttr(ctx, "api.deprecated.usage",
log.String("api.endpoint", "/v1/users"),
log.String("client.id", "client-123"),
log.String("replacement", "/v2/users"))
logger.ErrorAttr(ctx, "payment.failed",
log.String("payment.id", "pay-abc123"),
log.Float64("payment.amount", 49.99),
log.String("payment.currency", "USD"),
log.String("payment.method", "credit_card"),
log.String("error.code", "card_declined"))
logger.DebugAttr(ctx, "file.uploaded",
log.String("file.id", "file-456"),
log.String("file.name", "document.pdf"),
log.Int64("file.size_bytes", 2048576),
log.String("user.id", "user-123"))
}
func ExampleLogger_withAttr() {
ctx := context.Background()
// Create a base logger
baseLogger := olog.New(olog.Options{Provider: global.GetLoggerProvider(), Name: "example"})
// Create a logger with common attributes using WithAttr
serviceLogger := baseLogger.WithAttr(
log.String("service.name", "user-service"),
log.String("service.version", "2.1.0"),
log.String("deployment.environment", "production"))
// All subsequent events will include the service attributes
serviceLogger.InfoAttr(ctx, "service.started",
log.Int64("port", 8080),
log.String("build", "abc1234"))
// Chain additional attributes for request-scoped logging
requestLogger := serviceLogger.WithAttr(
log.String("request.id", "req-789"),
log.String("user.id", "user-456"))
requestLogger.InfoAttr(ctx, "request.processing",
log.String("http.method", "POST"),
log.String("http.route", "/api/users"))
requestLogger.ErrorAttr(ctx, "validation.failed",
log.String("field", "email"),
log.String("error", "invalid format"))
// Mix WithAttr and With methods
mixedLogger := requestLogger.With("trace.id", "trace-xyz").WithAttr(log.Bool("debug.enabled", true))
mixedLogger.DebugAttr(ctx, "processing.detail",
log.Int64("processing.step", 3),
log.Float64("processing.duration_ms", 12.5))
}