Skip to content

Commit d076e15

Browse files
committed
Merge branch 'phu/klog'
2 parents e60177d + 660c1c6 commit d076e15

File tree

4 files changed

+165
-15
lines changed

4 files changed

+165
-15
lines changed

batcher.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
"sync/atomic"
99
"time"
1010

11-
"github.com/KyberNetwork/logger"
1211
"github.com/pkg/errors"
12+
13+
"github.com/KyberNetwork/kutils/klog"
1314
)
1415

1516
//go:generate mockgen -source=batcher.go -destination mocks/mocks.go -package mocks
@@ -77,7 +78,8 @@ func (c *ChanTask[R]) Result() (R, error) {
7778
func (c *ChanTask[R]) Resolve(ret R, err error) {
7879
select {
7980
case <-c.done:
80-
logger.Errorf("ChanTask.Resolve|called twice, ignored|c.Ret=%v,c.Err=%v|Ret=%v,Err=%v", c.Ret, c.Err, ret, err)
81+
klog.Errorf(c.ctx, "ChanTask.Resolve|called twice, ignored|c.Ret=%v,c.Err=%v|Ret=%v,Err=%v",
82+
c.Ret, c.Err, ret, err)
8183
default:
8284
c.Ret, c.Err = ret, err
8385
close(c.done)
@@ -143,7 +145,8 @@ func (b *ChanBatcher[T, R]) batchFnWithRecover(tasks []T) {
143145
if p == nil {
144146
return
145147
}
146-
logger.Errorf("ChanBatcher.goBatchFn|recovered from panic: %v\n%s", p, string(debug.Stack()))
148+
klog.Errorf(context.Background(), "ChanBatcher.goBatchFn|recovered from panic: %v\n%s",
149+
p, string(debug.Stack()))
147150
var ret R
148151
err, ok := p.(error)
149152
if ok {
@@ -166,7 +169,8 @@ func (b *ChanBatcher[T, R]) batchFnWithRecover(tasks []T) {
166169
func (b *ChanBatcher[T, R]) worker() {
167170
defer func() {
168171
if p := recover(); p != nil {
169-
logger.Errorf("ChanBatcher.worker|recovered from panic: %v\n%s", p, string(debug.Stack()))
172+
klog.Errorf(context.Background(), "ChanBatcher.worker|recovered from panic: %v\n%s",
173+
p, string(debug.Stack()))
170174
}
171175
}()
172176
var tasks []T
@@ -178,29 +182,30 @@ func (b *ChanBatcher[T, R]) worker() {
178182
if len(tasks) == 0 {
179183
break
180184
}
181-
logger.Debugf("ChanBatcher.worker|timer|%d tasks", len(tasks))
185+
klog.Debugf(tasks[0].Ctx(), "ChanBatcher.worker|timer|%d tasks", len(tasks))
182186
go b.batchFnWithRecover(tasks)
183187
tasks = tasks[:0:0]
184188
case task, ok := <-b.taskCh:
189+
ctx := task.Ctx()
185190
if !ok {
186-
logger.Debugf("ChanBatcher.worker|closed|%d tasks", len(tasks))
191+
klog.Debugf(ctx, "ChanBatcher.worker|closed|%d tasks", len(tasks))
187192
if len(tasks) > 0 {
188193
go b.batchFnWithRecover(tasks)
189194
}
190195
return
191196
}
192197
if !task.IsDone() {
193198
select {
194-
case <-task.Ctx().Done():
195-
logger.Infof("ChanBatcher.worker|skip|task=%v", task)
196-
task.Resolve(*new(R), task.Ctx().Err())
199+
case <-ctx.Done():
200+
klog.Infof(ctx, "ChanBatcher.worker|skip|task=%v", task)
201+
task.Resolve(*new(R), ctx.Err())
197202
continue
198203
default:
199204
}
200205
}
201206
duration, batchCount := b.batchCfg()
202207
if len(tasks) == 0 {
203-
logger.Debugf("ChanBatcher.worker|timer start|duration=%s", duration)
208+
klog.Debugf(ctx, "ChanBatcher.worker|timer start|duration=%s", duration)
204209
if !batchTimer.Stop() {
205210
select {
206211
case <-batchTimer.C:
@@ -211,7 +216,7 @@ func (b *ChanBatcher[T, R]) worker() {
211216
}
212217
tasks = append(tasks, task)
213218
if len(tasks) >= batchCount {
214-
logger.Debugf("ChanBatcher.worker|max|%d tasks", len(tasks))
219+
klog.Debugf(ctx, "ChanBatcher.worker|max|%d tasks", len(tasks))
215220
go b.batchFnWithRecover(tasks)
216221
tasks = tasks[:0:0]
217222
}

batcher_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"testing"
88
"time"
99

10-
"github.com/KyberNetwork/logger"
1110
"github.com/pkg/errors"
1211
"github.com/stretchr/testify/assert"
12+
13+
"github.com/KyberNetwork/kutils/klog"
1314
)
1415

1516
func TestChanBatcher(t *testing.T) {
@@ -70,7 +71,7 @@ func TestChanBatcher(t *testing.T) {
7071
batcher.Batch(tasks[i])
7172
}
7273
// 1k: 2.561804ms; 1M: 2.62s - average overhead per task = 2.6µs
73-
logger.Warnf("done %d tasks in %v", taskCnt, time.Since(start))
74+
klog.Warnf(ctx, "done %d tasks in %v", taskCnt, time.Since(start))
7475
for i := 0; i < taskCnt; i++ {
7576
ret, err := tasks[i].Result()
7677
assert.NoError(t, err)

http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"net/http"
77
"time"
88

9-
"github.com/KyberNetwork/kutils/internal/json"
10-
119
"github.com/go-resty/resty/v2"
1210
"github.com/hashicorp/go-retryablehttp"
11+
12+
"github.com/KyberNetwork/kutils/internal/json"
1313
)
1414

1515
// HttpCfg is the resty http client configs

klog/klog.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package klog
2+
3+
import (
4+
"context"
5+
6+
"github.com/KyberNetwork/logger"
7+
)
8+
9+
func DefaultLogger() Logger {
10+
return logger.DefaultLogger()
11+
}
12+
13+
type Logger logger.Logger
14+
15+
var log logger.Logger
16+
17+
type Configuration struct {
18+
EnableConsole bool
19+
EnableJSONFormat bool
20+
ConsoleLevel string
21+
EnableFile bool
22+
FileJSONFormat bool
23+
FileLevel string
24+
FileLocation string
25+
}
26+
27+
type LoggerBackend logger.LoggerBackend
28+
29+
const (
30+
// LoggerBackendZap logging using Uber's zap backend
31+
LoggerBackendZap = LoggerBackend(logger.LoggerBackendZap)
32+
// LoggerBackendLogrus logging using logrus backend
33+
LoggerBackendLogrus = LoggerBackend(logger.LoggerBackendLogrus)
34+
)
35+
36+
func InitLogger(config Configuration, backend LoggerBackend) (Logger, error) {
37+
var err error
38+
log, err = logger.InitLogger(logger.Configuration{
39+
EnableConsole: config.EnableConsole,
40+
EnableJSONFormat: config.EnableJSONFormat,
41+
ConsoleLevel: config.ConsoleLevel,
42+
EnableFile: config.EnableFile,
43+
FileJSONFormat: config.FileJSONFormat,
44+
FileLevel: config.FileLevel,
45+
FileLocation: config.FileLocation,
46+
}, logger.LoggerBackend(backend))
47+
return log, err
48+
}
49+
50+
func Log() Logger {
51+
if log == nil {
52+
log = DefaultLogger()
53+
}
54+
return log
55+
}
56+
57+
func NewLogger(config Configuration, backend LoggerBackend) (Logger, error) {
58+
return logger.NewLogger(logger.Configuration{
59+
EnableConsole: config.EnableConsole,
60+
EnableJSONFormat: config.EnableJSONFormat,
61+
ConsoleLevel: config.ConsoleLevel,
62+
EnableFile: config.EnableFile,
63+
FileJSONFormat: config.FileJSONFormat,
64+
FileLevel: config.FileLevel,
65+
FileLocation: config.FileLocation,
66+
}, logger.LoggerBackend(backend))
67+
}
68+
69+
type CtxKeyLogger struct{}
70+
71+
var ctxKeyLogger CtxKeyLogger
72+
73+
func LoggerFromCtx(ctx context.Context) Logger {
74+
if ctx == nil {
75+
return Log()
76+
}
77+
ctxLog, _ := ctx.Value(ctxKeyLogger).(Logger)
78+
if ctxLog != nil {
79+
return ctxLog
80+
}
81+
return Log()
82+
}
83+
84+
func CtxWithLogger(ctx context.Context, log Logger) context.Context {
85+
return context.WithValue(ctx, ctxKeyLogger, log)
86+
}
87+
88+
func Debug(ctx context.Context, msg string) {
89+
LoggerFromCtx(ctx).Debug(msg)
90+
}
91+
92+
func Debugf(ctx context.Context, format string, args ...any) {
93+
LoggerFromCtx(ctx).Debugf(format, args...)
94+
}
95+
96+
func Info(ctx context.Context, msg string) {
97+
LoggerFromCtx(ctx).Info(msg)
98+
}
99+
100+
func Infof(ctx context.Context, format string, args ...any) {
101+
LoggerFromCtx(ctx).Infof(format, args...)
102+
}
103+
104+
func Infoln(ctx context.Context, msg string) {
105+
LoggerFromCtx(ctx).Infoln(msg)
106+
}
107+
108+
func Warn(ctx context.Context, msg string) {
109+
LoggerFromCtx(ctx).Warn(msg)
110+
}
111+
112+
func Warnf(ctx context.Context, format string, args ...any) {
113+
LoggerFromCtx(ctx).Warnf(format, args...)
114+
}
115+
116+
func Error(ctx context.Context, msg string) {
117+
LoggerFromCtx(ctx).Error(msg)
118+
}
119+
120+
func Errorf(ctx context.Context, format string, args ...any) {
121+
LoggerFromCtx(ctx).Errorf(format, args...)
122+
}
123+
124+
func Fatal(ctx context.Context, msg string) {
125+
LoggerFromCtx(ctx).Fatal(msg)
126+
}
127+
128+
func Fatalf(ctx context.Context, format string, args ...any) {
129+
LoggerFromCtx(ctx).Fatalf(format, args...)
130+
}
131+
132+
type Fields logger.Fields
133+
134+
func WithFields(ctx context.Context, keyValues Fields) Logger {
135+
return LoggerFromCtx(ctx).WithFields(logger.Fields(keyValues))
136+
}
137+
138+
func GetDelegate(ctx context.Context) any {
139+
return LoggerFromCtx(ctx).GetDelegate()
140+
}
141+
142+
func SetLogLevel(ctx context.Context, level string) error {
143+
return LoggerFromCtx(ctx).SetLogLevel(level)
144+
}

0 commit comments

Comments
 (0)