-
Notifications
You must be signed in to change notification settings - Fork 402
/
logger.go
91 lines (76 loc) · 2.7 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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package storelogger
import (
"context"
"strconv"
"sync/atomic"
"github.com/spacemonkeygo/monkit/v3"
"go.uber.org/zap"
"storj.io/storj/private/kvstore"
)
var mon = monkit.Package()
var id int64
// Logger implements a zap.Logger for kvstore.Store.
type Logger struct {
log *zap.Logger
store kvstore.Store
}
// New creates a new Logger with log and store.
func New(log *zap.Logger, store kvstore.Store) *Logger {
loggerid := atomic.AddInt64(&id, 1)
name := strconv.Itoa(int(loggerid))
return &Logger{log.Named(name), store}
}
// Put adds a value to store.
func (store *Logger) Put(ctx context.Context, key kvstore.Key, value kvstore.Value) (err error) {
defer mon.Task()(&ctx)(&err)
store.log.Debug("Put", zap.ByteString("key", key), zap.Int("value length", len(value)), zap.Binary("truncated value", truncate(value)))
return store.store.Put(ctx, key, value)
}
// Get gets a value to store.
func (store *Logger) Get(ctx context.Context, key kvstore.Key) (_ kvstore.Value, err error) {
defer mon.Task()(&ctx)(&err)
store.log.Debug("Get", zap.ByteString("key", key))
return store.store.Get(ctx, key)
}
// Delete deletes key and the value.
func (store *Logger) Delete(ctx context.Context, key kvstore.Key) (err error) {
defer mon.Task()(&ctx)(&err)
store.log.Debug("Delete", zap.ByteString("key", key))
return store.store.Delete(ctx, key)
}
// Range iterates over all items in unspecified order.
func (store *Logger) Range(ctx context.Context, fn func(context.Context, kvstore.Key, kvstore.Value) error) (err error) {
defer mon.Task()(&ctx)(&err)
store.log.Debug("Range")
return store.store.Range(ctx, func(ctx context.Context, key kvstore.Key, value kvstore.Value) error {
store.log.Debug(" ",
zap.ByteString("key", key),
zap.Int("value length", len(value)),
zap.Binary("truncated value", truncate(value)),
)
return fn(ctx, key, value)
})
}
// Close closes the store.
func (store *Logger) Close() error {
store.log.Debug("Close")
return store.store.Close()
}
// CompareAndSwap atomically compares and swaps oldValue with newValue.
func (store *Logger) CompareAndSwap(ctx context.Context, key kvstore.Key, oldValue, newValue kvstore.Value) (err error) {
defer mon.Task()(&ctx)(&err)
store.log.Debug("CompareAndSwap", zap.ByteString("key", key),
zap.Int("old value length", len(oldValue)), zap.Int("new value length", len(newValue)),
zap.Binary("truncated old value", truncate(oldValue)), zap.Binary("truncated new value", truncate(newValue)))
return store.store.CompareAndSwap(ctx, key, oldValue, newValue)
}
func truncate(v kvstore.Value) (t []byte) {
if len(v)-1 < 10 {
t = []byte(v)
} else {
t = v[:10]
}
return t
}