-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging_test.go
218 lines (178 loc) · 5.07 KB
/
logging_test.go
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package logger_test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/nextmicro/logger"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)
func TestMain(t *testing.M) {
exporter, err := stdouttrace.New()
if err != nil {
panic(err)
}
options := make([]sdktrace.TracerProviderOption, 0)
options = append(options, sdktrace.WithBatcher(exporter))
provider := sdktrace.NewTracerProvider(options...)
otel.SetTracerProvider(provider)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{}, propagation.Baggage{}))
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) {
logger.Errorf("[otel] error: %v", err)
}))
t.Run()
}
func TestLogging_WithContext(t *testing.T) {
logging := logger.New()
ctx, span := otel.Tracer("gokit").Start(context.TODO(), "HTTP Client Get /api/get")
defer span.End()
spanContext := trace.SpanContextFromContext(ctx)
t.Logf("trace_id: %s", spanContext.TraceID().String())
t.Logf("span_id: %s", spanContext.SpanID().String())
logging.WithContext(ctx).WithFields(map[string]any{
"11": 222,
}).Info("TestDefault_WithContext")
}
func TestLogging_WithFields(t *testing.T) {
logging := logger.New()
logging.WithFields(map[string]interface{}{
"age": 22,
"order": 100,
}).Info("TestDefault_WithFields")
}
func TestLogging_Debug(t *testing.T) {
logging := logger.New()
logging.WithFields(map[string]interface{}{
"age": 22,
"order": 100,
}).Debug("TestDefault_WithFields")
logging.SetLevel(logger.DebugLevel)
logging.WithFields(map[string]interface{}{
"age": 22,
"order": 100,
}).Debug("TestDefault_WithFields")
}
func TestLogger(t *testing.T) {
// Create a new logger for testing
log := logger.New()
// Test logging at different levels
log.Debug("Debug message")
log.Info("Info message")
log.Warn("Warning message")
log.Error("Error message")
//log.Fatal("Fatal message")
// Test formatting
log.Debugf("Debug message with format: %s", "formatted")
log.Infof("Info message with format: %s", "formatted")
log.Warnf("Warning message with format: %s", "formatted")
log.Errorf("Error message with format: %s", "formatted")
//log.Fatalf("Fatal message with format: %s", "formatted")
// Test log with fields
fields := map[string]interface{}{
"user": "john_doe",
"status": "failed",
}
log.WithFields(fields).Info("User login failed")
// Test setting log level
log.SetLevel(logger.DebugLevel)
log.Debug("Debug message should be visible")
log.SetLevel(logger.InfoLevel)
log.Debug("Debug message should not be visible")
// Test syncing
err := logger.Sync()
if err != nil {
t.Errorf("Error syncing logger: %v", err)
}
// Add more test cases as needed
}
func TestLoggerWithContext(t *testing.T) {
// Create a context with some values for testing
ctx := context.WithValue(context.Background(), "spanId", "123")
ctx = context.WithValue(ctx, "traceId", "456")
// Create a logger with context
log := logger.DefaultLogger.WithContext(ctx)
// Test logging with context-based fields
log.Info("Logging with context")
// Add more test cases as needed
}
func TestLoggerWithFields(t *testing.T) {
// Create a logger with some initial fields
fields := map[string]interface{}{
"app": "my_app",
"version": "1.0",
}
logger := logger.DefaultLogger.WithFields(fields)
// Test logging with additional fields
additionalFields := map[string]interface{}{
"user": "alice",
}
logger.WithFields(additionalFields).Info("Additional fields")
// Add more test cases as needed
}
func TestFilename(t *testing.T) {
logger.DefaultLogger = logger.New(
logger.WithLevel(logger.InfoLevel),
logger.WithFilename("stat.log"),
logger.WithRotation("hour"),
logger.WithNamespace("metrics"),
logger.WithMode(logger.FileMode),
logger.WithMaxSize(1),
logger.WithMaxBackups(3),
logger.WithKeepHours(1),
logger.WithCompress(false),
)
for i := 0; i < 10000; i++ {
logger.WithFields(map[string]interface{}{
"a": 123,
"v": 1,
}).Info("test msg")
}
logger.Sync()
}
func TestLogs(t *testing.T) {
logger.DefaultLogger = logger.New(
logger.WithLevel(logger.InfoLevel),
logger.WithMode(logger.FileMode),
logger.WithMaxSize(1),
logger.WithMaxBackups(1),
logger.WithCompress(false),
logger.WithRotation("size"),
)
defer logger.Sync()
for i := 0; i < 10000; i++ {
logger.Debug("test msg")
logger.Info("test msg")
logger.Warn("test msg")
logger.Error("test msg")
}
}
type CustomOutput struct {
}
func (c *CustomOutput) Write(p []byte) (n int, err error) {
msg := strings.Replace(string(p), "\n", "", 1)
fmt.Println(msg)
return len(p), nil
}
func (c *CustomOutput) Sync() error {
fmt.Println("sync")
return nil
}
// 自定义输出源
func TestCustomOutput(t *testing.T) {
logger.DefaultLogger = logger.New(
logger.WithMode("custom"),
logger.WithWriter(&CustomOutput{}),
)
defer logger.Sync()
for i := 0; i < 1000; i++ {
logger.Info("test msg")
}
}
func TestInfo(t *testing.T) {
logger.Info("test msg")
}