Skip to content

Commit 0bce70d

Browse files
authored
refactor(v2): use non global logger (#1037)
1 parent b3c241c commit 0bce70d

16 files changed

+200
-153
lines changed

v2/CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
3+
## Unreleased
4+
5+
* [#1037](https://github.com/cosmos/iavl/pull/1037) Swap `zerolog` for internal logger interface
6+
7+
## [v2.0.0-alpha.4](https://github.com/cosmos/iavl/releases/tag/v2.0.0-alpha.4)
8+
9+
* Remove debug prints
10+
11+
## [v2.0.0-alpha.3](https://github.com/cosmos/iavl/releases/tag/v2.0.0-alpha.3)
12+
13+
* Initial tag.

v2/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# IAVL v2

v2/cmd/gen/gen.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@ import (
1212
"github.com/dustin/go-humanize"
1313
"github.com/kocubinski/costor-api/compact"
1414
"github.com/kocubinski/costor-api/core"
15-
"github.com/rs/zerolog"
16-
zlog "github.com/rs/zerolog/log"
1715
"github.com/spf13/cobra"
1816
)
1917

20-
var log = zlog.Output(zerolog.ConsoleWriter{
21-
Out: os.Stderr,
22-
TimeFormat: time.Stamp,
23-
})
18+
var log = iavl.NewTestLogger()
2419

2520
func Command() *cobra.Command {
2621
cmd := &cobra.Command{
@@ -75,9 +70,10 @@ func emitCommand() *cobra.Command {
7570
go func() {
7671
stats, err := stream.Compact()
7772
if err != nil {
78-
log.Fatal().Err(err).Msg("failed to compact")
73+
log.Error("failed to compact", "error", err)
74+
os.Exit(1)
7975
}
80-
log.Info().Msgf(stats.Report())
76+
log.Info(stats.Report())
8177
wg.Done()
8278
}()
8379

@@ -95,13 +91,13 @@ func emitCommand() *cobra.Command {
9591

9692
if itr.Version() < int64(start) {
9793
if cnt%5_000_000 == 0 {
98-
log.Info().Msgf("fast forward version=%d nodes=%s", itr.Version(), humanize.Comma(cnt))
94+
log.Info(fmt.Sprintf("fast forward version=%d nodes=%s", itr.Version(), humanize.Comma(cnt)))
9995
}
10096
continue
10197
}
10298

10399
if cnt%500_000 == 0 {
104-
log.Info().Msgf("version=%d nodes=%s", itr.Version(), humanize.Comma(cnt))
100+
log.Info(fmt.Sprintf("version=%d nodes=%s", itr.Version(), humanize.Comma(cnt)))
105101
}
106102

107103
select {
@@ -149,11 +145,11 @@ func treeCommand() *cobra.Command {
149145
Use: "tree",
150146
Short: "build and save a Tree to disk, taking generated changesets as input",
151147
RunE: func(cmd *cobra.Command, args []string) error {
152-
multiTree := iavl.NewMultiTree(dbPath, iavl.TreeOptions{StateStorage: true})
148+
multiTree := iavl.NewMultiTree(iavl.NewTestLogger(), dbPath, iavl.TreeOptions{StateStorage: true})
153149
defer func(mt *iavl.MultiTree) {
154150
err := mt.Close()
155151
if err != nil {
156-
log.Error().Err(err).Msg("failed to close db")
152+
log.Error("failed to close db", "error", err)
157153
}
158154
}(multiTree)
159155

@@ -203,12 +199,12 @@ func treeCommand() *cobra.Command {
203199

204200
i++
205201
if i%100_000 == 0 {
206-
log.Info().Msgf("leaves=%s dur=%s rate=%s version=%d",
202+
log.Info(fmt.Sprintf("leaves=%s dur=%s rate=%s version=%d",
207203
humanize.Comma(i),
208204
time.Since(start),
209205
humanize.Comma(int64(100_000/time.Since(start).Seconds())),
210206
itr.Version(),
211-
)
207+
))
212208
start = time.Now()
213209
}
214210
}
@@ -219,7 +215,7 @@ func treeCommand() *cobra.Command {
219215
}
220216
}
221217

222-
log.Info().Msgf("last version=%d hash=%x", lastVersion, lastHash)
218+
log.Info(fmt.Sprintf("last version=%d hash=%x", lastVersion, lastHash))
223219

224220
return nil
225221
},

v2/cmd/rollback/rollback.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
package rollback
22

33
import (
4-
"os"
5-
"time"
4+
"fmt"
65

76
"github.com/cosmos/iavl/v2"
8-
"github.com/rs/zerolog"
9-
zlog "github.com/rs/zerolog/log"
107
"github.com/spf13/cobra"
118
)
129

13-
var log = zlog.Output(zerolog.ConsoleWriter{
14-
Out: os.Stderr,
15-
TimeFormat: time.Stamp,
16-
})
10+
var log = iavl.NewTestLogger()
1711

1812
func Command() *cobra.Command {
1913
var (
@@ -29,7 +23,7 @@ func Command() *cobra.Command {
2923
return err
3024
}
3125
for _, dbPath := range dbPaths {
32-
log.Info().Msgf("revert db %s to version %d", dbPath, version)
26+
log.Info(fmt.Sprintf("revert db %s to version %d", dbPath, version))
3327
sql, err := iavl.NewSqliteDb(iavl.NewNodePool(), iavl.SqliteDbOptions{Path: dbPath})
3428
if err != nil {
3529
return err

v2/cmd/snapshot/snapshot.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
package snapshot
22

33
import (
4-
"os"
5-
"time"
4+
"fmt"
65

76
"github.com/cosmos/iavl/v2"
8-
"github.com/rs/zerolog"
9-
zlog "github.com/rs/zerolog/log"
107
"github.com/spf13/cobra"
118
)
129

13-
var log = zlog.Output(zerolog.ConsoleWriter{
14-
Out: os.Stderr,
15-
TimeFormat: time.Stamp,
16-
})
10+
var log = iavl.NewTestLogger()
1711

1812
func Command() *cobra.Command {
1913
var (
@@ -28,7 +22,7 @@ func Command() *cobra.Command {
2822
if err != nil {
2923
return err
3024
}
31-
log.Info().Msgf("found db paths: %v", paths)
25+
log.Info(fmt.Sprintf("found db paths: %v", paths))
3226

3327
var (
3428
pool = iavl.NewNodePool()

v2/go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ require (
1010
github.com/emicklei/dot v1.6.0
1111
github.com/kocubinski/costor-api v1.1.1
1212
github.com/prometheus/client_golang v1.16.0
13-
github.com/rs/zerolog v1.30.0
1413
github.com/spf13/cobra v1.7.0
1514
github.com/stretchr/testify v1.8.4
1615
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
@@ -23,14 +22,15 @@ require (
2322
github.com/golang/protobuf v1.5.3 // indirect
2423
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2524
github.com/mattn/go-colorable v0.1.13 // indirect
26-
github.com/mattn/go-isatty v0.0.19 // indirect
25+
github.com/mattn/go-isatty v0.0.20 // indirect
2726
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
2827
github.com/pmezard/go-difflib v1.0.0 // indirect
2928
github.com/prometheus/client_model v0.3.0 // indirect
3029
github.com/prometheus/common v0.42.0 // indirect
3130
github.com/prometheus/procfs v0.10.1 // indirect
31+
github.com/rs/zerolog v1.33.0 // indirect
3232
github.com/spf13/pflag v1.0.5 // indirect
33-
golang.org/x/sys v0.13.0 // indirect
33+
golang.org/x/sys v0.22.0 // indirect
3434
google.golang.org/protobuf v1.33.0 // indirect
3535
gopkg.in/yaml.v3 v3.0.1 // indirect
3636
)

v2/go.sum

+7-9
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ github.com/kocubinski/costor-api v1.1.1 h1:sgfJA7T/8IfZ59zxiMrED0xdjerAFuPNBTqyO
3030
github.com/kocubinski/costor-api v1.1.1/go.mod h1:ESMBMDkKfN+9vvvhhNVdKLhbOmzI3O/i16iXvRM9Tuc=
3131
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
3232
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
33-
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
3433
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
3534
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
36-
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
3735
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
38-
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
3936
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
37+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
38+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
4039
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
4140
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
4241
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -52,8 +51,8 @@ github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+Pymzi
5251
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
5352
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
5453
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
55-
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
56-
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
54+
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
55+
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
5756
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5857
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
5958
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
@@ -64,12 +63,11 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
6463
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
6564
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
6665
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
67-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
68-
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6966
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7067
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
71-
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
72-
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
68+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
69+
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
70+
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7371
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
7472
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
7573
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=

v2/logger.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package iavl
2+
3+
import "log/slog"
4+
5+
// Logger defines basic logger that IAVL expects.
6+
// It is a subset of the cosmossdk.io/core/log.Logger interface.
7+
// cosmossdk.io/log/log.Logger implements this interface.
8+
type Logger interface {
9+
// Info takes a message and a set of key/value pairs and logs with level INFO.
10+
// The key of the tuple must be a string.
11+
Info(msg string, keyVals ...any)
12+
13+
// Warn takes a message and a set of key/value pairs and logs with level WARN.
14+
// The key of the tuple must be a string.
15+
Warn(msg string, keyVals ...any)
16+
17+
// Error takes a message and a set of key/value pairs and logs with level ERR.
18+
// The key of the tuple must be a string.
19+
Error(msg string, keyVals ...any)
20+
21+
// Debug takes a message and a set of key/value pairs and logs with level DEBUG.
22+
// The key of the tuple must be a string.
23+
Debug(msg string, keyVals ...any)
24+
}
25+
26+
// NewNopLogger returns a new logger that does nothing.
27+
func NewNopLogger() Logger {
28+
return &noopLogger{}
29+
}
30+
31+
type noopLogger struct{}
32+
33+
func (l *noopLogger) Info(string, ...any) {}
34+
func (l *noopLogger) Warn(string, ...any) {}
35+
func (l *noopLogger) Error(string, ...any) {}
36+
func (l *noopLogger) Debug(string, ...any) {}
37+
38+
func NewTestLogger() Logger {
39+
return &testLogger{}
40+
}
41+
42+
type testLogger struct{}
43+
44+
func (l *testLogger) Info(msg string, keys ...any) {
45+
slog.Info(msg, keys...)
46+
}
47+
func (l *testLogger) Warn(msg string, keys ...any) {
48+
slog.Warn(msg, keys...)
49+
}
50+
func (l *testLogger) Error(msg string, keys ...any) {
51+
slog.Error(msg, keys...)
52+
}
53+
func (l *testLogger) Debug(msg string, keys ...any) {
54+
slog.Debug(msg, keys...)
55+
}

v2/multitree.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
// MultiTree encapsulates multiple IAVL trees, each with its own "store key" in the context of the Cosmos SDK.
1616
// Within IAVL v2 is only used to test the IAVL v2 implementation, and for import/export of IAVL v2 state.
1717
type MultiTree struct {
18+
logger Logger
19+
1820
Trees map[string]*Tree
1921

2022
pool *NodePool
@@ -26,7 +28,7 @@ type MultiTree struct {
2628
errorCh chan error
2729
}
2830

29-
func NewMultiTree(rootPath string, opts TreeOptions) *MultiTree {
31+
func NewMultiTree(logger Logger, rootPath string, opts TreeOptions) *MultiTree {
3032
return &MultiTree{
3133
Trees: make(map[string]*Tree),
3234
doneCh: make(chan saveVersionResult, 1000),
@@ -37,8 +39,8 @@ func NewMultiTree(rootPath string, opts TreeOptions) *MultiTree {
3739
}
3840
}
3941

40-
func ImportMultiTree(pool *NodePool, version int64, path string, treeOpts TreeOptions) (*MultiTree, error) {
41-
mt := NewMultiTree(path, treeOpts)
42+
func ImportMultiTree(logger Logger, pool *NodePool, version int64, path string, treeOpts TreeOptions) (*MultiTree, error) {
43+
mt := NewMultiTree(logger, path, treeOpts)
4244
paths, err := FindDbsInPath(path)
4345
if err != nil {
4446
return nil, err
@@ -78,7 +80,7 @@ func ImportMultiTree(pool *NodePool, version int64, path string, treeOpts TreeOp
7880
return nil, err
7981
case res := <-done:
8082
prefix := filepath.Base(res.path)
81-
log.Info().Msgf("imported %s", prefix)
83+
logger.Info(fmt.Sprintf("imported %s", prefix))
8284
mt.Trees[prefix] = res.tree
8385
}
8486
}
@@ -108,7 +110,6 @@ func (mt *MultiTree) MountTrees() error {
108110
prefix := filepath.Base(dbPath)
109111
sqlOpts := defaultSqliteDbOptions(SqliteDbOptions{})
110112
sqlOpts.Path = dbPath
111-
log.Info().Msgf("mounting %s; opts %v", prefix, sqlOpts)
112113
sql, err := NewSqliteDb(mt.pool, sqlOpts)
113114
if err != nil {
114115
return err
@@ -173,7 +174,7 @@ func (mt *MultiTree) SaveVersionConcurrently() ([]byte, int64, error) {
173174
for i := 0; i < treeCount; i++ {
174175
select {
175176
case err := <-mt.errorCh:
176-
log.Error().Err(err).Msg("failed to save version")
177+
mt.logger.Error("failed to save version", "error", err)
177178
errs = append(errs, err)
178179
case result := <-mt.doneCh:
179180
if version != -1 && version != result.version {
@@ -218,7 +219,7 @@ func (mt *MultiTree) SnapshotConcurrently() error {
218219
for i := 0; i < treeCount; i++ {
219220
select {
220221
case err := <-mt.errorCh:
221-
log.Error().Err(err).Msg("failed to snapshot")
222+
mt.logger.Error("failed to snapshot", "error", err)
222223
errs = append(errs, err)
223224
case <-mt.doneCh:
224225
}
@@ -271,7 +272,7 @@ func (mt *MultiTree) WarmLeaves() error {
271272
for i := 0; i < cnt; i++ {
272273
select {
273274
case err := <-mt.errorCh:
274-
log.Error().Err(err).Msg("failed to warm leaves")
275+
mt.logger.Error("failed to warm leaves", "error", err)
275276
return err
276277
case <-mt.doneCh:
277278
}

0 commit comments

Comments
 (0)