-
Notifications
You must be signed in to change notification settings - Fork 51
/
logger.go
135 lines (110 loc) · 2.53 KB
/
logger.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
package sdk
import (
"context"
"encoding/json"
"fmt"
"github.com/apex/log"
"github.com/cenkalti/backoff/v4"
grpc "github.com/crawlab-team/crawlab-grpc"
"github.com/crawlab-team/crawlab-sdk/entity"
"github.com/crawlab-team/crawlab-sdk/interfaces"
"github.com/crawlab-team/go-trace"
"time"
)
var L *Logger
type Logger struct {
log.Interface
// internals
sub grpc.TaskService_SubscribeClient
}
func (l *Logger) Log(s string) {
l.log("", s)
}
func (l *Logger) Debug(s string) {
l.log(LogLevelDebug, s)
}
func (l *Logger) Info(s string) {
l.log(LogLevelInfo, s)
}
func (l *Logger) Warn(s string) {
l.log(LogLevelWarn, s)
}
func (l *Logger) Error(s string) {
l.log(LogLevelError, s)
}
func (l *Logger) Fatal(s string) {
l.log(LogLevelFatal, s)
}
func (l *Logger) Logf(s string, i ...interface{}) {
l.log("", fmt.Sprintf(s, i...))
}
func (l *Logger) Debugf(s string, i ...interface{}) {
l.log(LogLevelDebug, fmt.Sprintf(s, i...))
}
func (l *Logger) Infof(s string, i ...interface{}) {
l.log(LogLevelInfo, fmt.Sprintf(s, i...))
}
func (l *Logger) Warnf(s string, i ...interface{}) {
l.log(LogLevelWarn, fmt.Sprintf(s, i...))
}
func (l *Logger) Errorf(s string, i ...interface{}) {
l.log(LogLevelError, fmt.Sprintf(s, i...))
}
func (l *Logger) Fatalf(s string, i ...interface{}) {
l.log(LogLevelFatal, fmt.Sprintf(s, i...))
}
func (l *Logger) log(level string, s string) {
if level != "" {
s = fmt.Sprintf("[%s] %s", level, s)
}
data, err := json.Marshal(&entity.StreamMessageTaskData{
TaskId: GetTaskId(),
Logs: []string{s},
})
if err != nil {
trace.PrintError(err)
return
}
if err := l.sub.Send(&grpc.StreamMessage{
Code: grpc.StreamMessageCode_INSERT_LOGS,
Data: data,
}); err != nil {
trace.PrintError(err)
return
}
}
func (l *Logger) init() (err error) {
op := l._init
b := backoff.NewExponentialBackOff()
notify := func(err error, duration time.Duration) {
log.Errorf("init logger error: %v, re-attempt in %.1f seconds", err, duration.Seconds())
}
if err := backoff.RetryNotify(op, b, notify); err != nil {
return trace.TraceError(err)
}
return nil
}
func (l *Logger) _init() (err error) {
l.sub, err = GetClient().GetTaskClient().Subscribe(context.Background())
if err != nil {
return trace.TraceError(err)
}
return nil
}
func GetLogger(opts ...LoggerOption) interfaces.Logger {
if L != nil {
return L
}
// logger
l := &Logger{Interface: log.Log}
// apply options
for _, opt := range opts {
opt(l)
}
// initialize
if err := l.init(); err != nil {
panic(err)
}
L = l
return l
}