Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Feb 25, 2023
1 parent 747c29e commit fba6762
Show file tree
Hide file tree
Showing 61 changed files with 171 additions and 208 deletions.
5 changes: 2 additions & 3 deletions baseapp/grpcrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package baseapp_test

import (
"context"
"os"
"sync"
"testing"

"github.com/cometbft/cometbft/libs/log"
"cosmossdk.io/log"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -59,7 +58,7 @@ func TestRegisterQueryServiceTwice(t *testing.T) {
err := depinject.Inject(makeMinimalConfig(), &appBuilder)
require.NoError(t, err)
db := dbm.NewMemDB()
app := appBuilder.Build(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil)
app := appBuilder.Build(log.NewLogger(), db, nil)

// First time registering service shouldn't panic.
require.NotPanics(t, func() {
Expand Down
7 changes: 3 additions & 4 deletions baseapp/msg_service_router_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package baseapp_test

import (
"os"
"testing"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"

"cosmossdk.io/depinject"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -29,7 +28,7 @@ func TestRegisterMsgService(t *testing.T) {
)
err := depinject.Inject(makeMinimalConfig(), &appBuilder, &registry)
require.NoError(t, err)
app := appBuilder.Build(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), dbm.NewMemDB(), nil)
app := appBuilder.Build(log.NewLogger(), dbm.NewMemDB(), nil)

require.Panics(t, func() {
testdata.RegisterMsgServer(
Expand Down Expand Up @@ -58,7 +57,7 @@ func TestRegisterMsgServiceTwice(t *testing.T) {
err := depinject.Inject(makeMinimalConfig(), &appBuilder, &registry)
require.NoError(t, err)
db := dbm.NewMemDB()
app := appBuilder.Build(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil)
app := appBuilder.Build(log.NewLogger(), db, nil)
testdata.RegisterInterfaces(registry)

// First time registering service shouldn't panic.
Expand Down
3 changes: 1 addition & 2 deletions baseapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"net/url"
"os"
"reflect"
"strconv"
"testing"
Expand Down Expand Up @@ -55,7 +54,7 @@ var ParamStoreKey = []byte("paramstore")

func defaultLogger() log.Logger {
if testing.Verbose() {
return log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "baseapp/test")
return log.NewLoggerWithKV("module", "baseapp/test")
}

return log.NewNopLogger()
Expand Down
5 changes: 2 additions & 3 deletions client/pruning/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package pruning

import (
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"cosmossdk.io/log"
pruningtypes "cosmossdk.io/store/pruning/types"
"cosmossdk.io/store/rootmulti"
"github.com/cometbft/cometbft/libs/log"
dbm "github.com/cosmos/cosmos-db"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -62,7 +61,7 @@ func Cmd(appCreator servertypes.AppCreator) *cobra.Command {
return err
}

logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger := log.NewLogger()
app := appCreator(logger, db, nil, vp)
cms := app.CommitMultiStore()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ require (
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/common v0.40.0
github.com/rakyll/statik v0.1.7
github.com/rs/zerolog v1.29.0
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -136,7 +137,6 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/rs/cors v1.8.3 // indirect
github.com/rs/zerolog v1.29.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion log/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Log

The `cosmossdk.io/log` provides simple logging implementations for the Cosmos SDK and Cosmos SDK modules.
The `cosmossdk.io/log` provides a zerolog logging implementation for the Cosmos SDK and Cosmos SDK modules.
8 changes: 1 addition & 7 deletions log/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ module cosmossdk.io/log

go 1.19

require (
github.com/cometbft/cometbft v0.37.0-alpha.3
github.com/rs/zerolog v1.29.0
)
require github.com/rs/zerolog v1.29.0

require (
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/sys v0.5.0 // indirect
)
11 changes: 0 additions & 11 deletions log/go.sum
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
github.com/cometbft/cometbft v0.37.0-alpha.3 h1:74F+cMr4pd1a2lFn/h4TxXmO8VWi3A2dxyoMcjlMWuQ=
github.com/cometbft/cometbft v0.37.0-alpha.3/go.mod h1:dUGbIGYoLM11xUruTTJY4Xp9FHh6Nfu3Nots8/+UNSo=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
65 changes: 59 additions & 6 deletions log/logger.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
package log

import cmtlog "github.com/cometbft/cometbft/libs/log"
import (
"os"
"time"

"github.com/rs/zerolog"
)

// Defines commons keys for logging
const ModuleKey = "module"

// ContextKey is used to store the logger in the context
var ContextKey struct{}

// Logger is the interface that wraps the basic logging methods.
type Logger interface {
Debug(msg string, keyvals ...interface{})
Info(msg string, keyvals ...interface{})
Error(msg string, keyvals ...interface{})
Info(msg string, keyVals ...interface{})
Error(msg string, keyVals ...interface{})
Debug(msg string, keyVals ...interface{})
With(keyVals ...interface{}) Logger
}

type ZeroLogWrapper struct {
zerolog.Logger
}

func NewNopLogger() Logger {
logger := zerolog.Nop()
return &ZeroLogWrapper{logger}
}

func NewLogger() Logger {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.Kitchen}
logger := zerolog.New(output).With().Timestamp().Logger()
return &ZeroLogWrapper{logger}
}

func NewLoggerWithKV(key, value string) Logger {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.Kitchen}
logger := zerolog.New(output).With().Str(key, value).Timestamp().Logger()
return &ZeroLogWrapper{logger}
}

// Info takes a message and a set of key/value pairs and logs with level INFO.
// The key of the tuple must be a string.
func (l ZeroLogWrapper) Info(msg string, keyVals ...interface{}) {
l.Logger.Info().Fields(keyVals).Msg(msg)
}

// Error takes a message and a set of key/value pairs and logs with level DEBUG.
// The key of the tuple must be a string.
func (l ZeroLogWrapper) Error(msg string, keyVals ...interface{}) {
l.Logger.Error().Fields(keyVals).Msg(msg)
}

// Debug takes a message and a set of key/value pairs and logs with level ERR.
// The key of the tuple must be a string.
func (l ZeroLogWrapper) Debug(msg string, keyVals ...interface{}) {
l.Logger.Debug().Fields(keyVals).Msg(msg)
}

With(keyvals ...interface{}) cmtlog.Logger
// With returns a new wrapped logger with additional context provided by a set
func (l ZeroLogWrapper) With(keyVals ...interface{}) Logger {
return ZeroLogWrapper{l.Logger.With().Fields(keyVals).Logger()}
}
19 changes: 0 additions & 19 deletions log/noop_logger.go

This file was deleted.

60 changes: 0 additions & 60 deletions log/zerolog.go

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/mockgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $mockgen_cmd -source=types/module/module.go -package mock -destination testutil/
$mockgen_cmd -source=types/module/mock_appmodule_test.go -package mock -destination testutil/mock/types_mock_appmodule.go
$mockgen_cmd -source=types/invariant.go -package mock -destination testutil/mock/types_invariant.go
$mockgen_cmd -package mock -destination testutil/mock/grpc_server.go github.com/cosmos/gogoproto/grpc Server
$mockgen_cmd -package mock -destination testutil/mock/cometbft_log.go github.com/cometbft/cometbft/libs/log Logger
$mockgen_cmd -package mock -destination testutil/mock/logger.go cosmossdk.io/log Logger
$mockgen_cmd -source=orm/model/ormtable/hooks.go -package ormmocks -destination orm/testing/ormmocks/hooks.go
$mockgen_cmd -source=x/nft/expected_keepers.go -package testutil -destination x/nft/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/feegrant/expected_keepers.go -package testutil -destination x/feegrant/testutil/expected_keepers_mocks.go
Expand Down
21 changes: 21 additions & 0 deletions server/cmt_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package server

import (
"cosmossdk.io/log"
cmtlog "github.com/cometbft/cometbft/libs/log"
)

var _ cmtlog.Logger = (*CometZerologWrapper)(nil)

// CometZerologWrapper provides a wrapper around a zerolog.Logger instance.
// It implements CometBFT's Logger interface.
type CometZerologWrapper struct {
log.ZeroLogWrapper
}

// With returns a new wrapped logger with additional context provided by a set
// of key/value tuples. The number of tuples must be even and the key of the
// tuple must be a string.
func (cmt CometZerologWrapper) With(keyVals ...interface{}) cmtlog.Logger {
return cmt.With(keyVals...)
}
2 changes: 1 addition & 1 deletion server/mock/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"path/filepath"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
db "github.com/cosmos/cosmos-db"
"google.golang.org/grpc"

"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"

bam "github.com/cosmos/cosmos-sdk/baseapp"
Expand Down
8 changes: 4 additions & 4 deletions server/mock/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"os"
"testing"

"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/abci/types"
cmtlog "github.com/cometbft/cometbft/libs/log"
)

// SetupApp returns an application as well as a clean-up function to be used to
// quickly setup a test case with an app.
func SetupApp() (abci.Application, func(), error) {
var logger cmtlog.Logger
var logger log.Logger
if testing.Verbose() {
logger = cmtlog.NewTMLogger(cmtlog.NewSyncWriter(os.Stdout)).With("module", "mock")
logger = log.NewLoggerWithKV("module", "mock")
} else {
logger = cmtlog.NewNopLogger()
logger = log.NewNopLogger()
}

rootDir, err := os.MkdirTemp("", "mock-sdk")
Expand Down
Loading

0 comments on commit fba6762

Please sign in to comment.