-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
54 lines (47 loc) · 1.84 KB
/
options.go
File metadata and controls
54 lines (47 loc) · 1.84 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
// #############################################################################
// # File: options.go #
// # Project: zlog #
// # Created Date: 2024/11/21 17:15:14 #
// # Author: realjf #
// # ----- #
// # Last Modified: 2025/06/05 00:08:10 #
// # Modified By: realjf #
// # ----- #
// # #
// #############################################################################
package zlog
import (
"context"
"github.com/realjf/zlog/trace"
"go.uber.org/zap"
)
type Option func(*zLog) (*zLog, error)
func WithTrace(ctx context.Context) Option {
return func(z *zLog) (*zLog, error) {
if tc, ok := trace.FromContext(ctx); ok {
cfgs := make([]*ZLogConfig, 0)
for _, cfg := range z.cfgs {
cfgs = append(cfgs, cfg)
}
newZlog := newZLog(cfgs, z.options...)
newZlog.usedLoggers = make(map[string]*zap.Logger, 0)
for name, _ := range z.loggers {
logger := z.loggers[name].With(
zap.String("traceID", tc.TraceID),
zap.String("spanID", tc.SpanID),
zap.String("parentSpanID", tc.ParentSpanID),
)
newZlog.loggers[name] = logger
if newZlog.cfgs[name].Default {
newZlog.usedLoggers[name] = logger
}
}
if len(newZlog.usedLoggers) == 0 {
newZlog.cfgs[cfgs[0].Name].Default = true
newZlog.usedLoggers[cfgs[0].Name] = newZlog.loggers[cfgs[0].Name]
}
return newZlog, nil
}
return z, nil
}
}