-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_log.go
116 lines (93 loc) · 2.61 KB
/
client_log.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
package flow
import (
"context"
"fmt"
"time"
"github.com/vine-io/flow/api"
vclient "github.com/vine-io/vine/core/client"
verrs "github.com/vine-io/vine/lib/errors"
log "github.com/vine-io/vine/lib/logger"
)
type LoggerOption func(options *LoggerOptions)
func WithLoggerContext(ctx *PipeSessionCtx) LoggerOption {
return func(opts *LoggerOptions) {
opts.ctx = ctx
}
}
type LoggerOptions struct {
ctx *PipeSessionCtx
}
func NewLoggerOptions(opts ...LoggerOption) LoggerOptions {
var options LoggerOptions
for _, opt := range opts {
opt(&options)
}
return options
}
type Logger struct {
LoggerOptions
}
func NewLogger(opts ...LoggerOption) *Logger {
options := NewLoggerOptions(opts...)
return &Logger{LoggerOptions: options}
}
func (lg *Logger) log(ctx context.Context, level api.TraceLevel, text string, opts ...vclient.CallOption) error {
if len(lg.ctx.wid) == 0 {
return nil
}
traceLog := &api.TraceLog{
Level: level,
Wid: lg.ctx.wid,
InstanceId: lg.ctx.instanceId,
Sid: lg.ctx.sid,
Text: text,
Timestamp: time.Now().UnixNano(),
}
in := &api.StepTraceRequest{TraceLog: traceLog}
opts = append(lg.ctx.c.cfg.callOptions(), opts...)
_, err := lg.ctx.c.s.StepTrace(ctx, in, opts...)
if err != nil {
return verrs.FromErr(err)
}
return nil
}
func (lg *Logger) Trace(text string) {
_ = lg.log(lg.ctx, api.TraceLevel_TL_TRACE, text)
log.Trace(text)
}
func (lg *Logger) Tracef(format string, args ...any) {
_ = lg.log(lg.ctx, api.TraceLevel_TL_TRACE, fmt.Sprintf(format, args...))
log.Tracef(format, args...)
}
func (lg *Logger) Debug(text string) {
_ = lg.log(lg.ctx, api.TraceLevel_TL_DEBUG, text)
log.Debug(text)
}
func (lg *Logger) Debugf(format string, args ...any) {
_ = lg.log(lg.ctx, api.TraceLevel_TL_DEBUG, fmt.Sprintf(format, args...))
log.Debugf(format, args...)
}
func (lg *Logger) Info(text string) {
_ = lg.log(lg.ctx, api.TraceLevel_TL_INFO, text)
log.Info(text)
}
func (lg *Logger) Infof(format string, args ...any) {
_ = lg.log(lg.ctx, api.TraceLevel_TL_INFO, fmt.Sprintf(format, args...))
log.Infof(format, args...)
}
func (lg *Logger) Warn(text string) {
_ = lg.log(lg.ctx, api.TraceLevel_TL_WARN, text)
log.Warn(text)
}
func (lg *Logger) Warnf(format string, args ...any) {
_ = lg.log(lg.ctx, api.TraceLevel_TL_WARN, fmt.Sprintf(format, args...))
log.Warnf(format, args...)
}
func (lg *Logger) Error(text string) {
_ = lg.log(lg.ctx, api.TraceLevel_TL_ERROR, text)
log.Error(text)
}
func (lg *Logger) Errorf(format string, args ...any) {
_ = lg.log(lg.ctx, api.TraceLevel_TL_ERROR, fmt.Sprintf(format, args...))
log.Errorf(format, args...)
}