Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(log): remove core dependency and update core interface to be dependency free #21045

Merged
merged 15 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/core/header"
"cosmossdk.io/core/log"
errorsmod "cosmossdk.io/errors"
sdklog "cosmossdk.io/log"
"cosmossdk.io/log"
"cosmossdk.io/store"
storemetrics "cosmossdk.io/store/metrics"
"cosmossdk.io/store/snapshots"
Expand Down Expand Up @@ -198,7 +197,7 @@ func NewBaseApp(
logger: logger.With(log.ModuleKey, "baseapp"),
name: name,
db: db,
cms: store.NewCommitMultiStore(db, sdklog.LogWrapper{Logger: logger}, storemetrics.NewNoOpMetrics()), // by default we use a no-op metric gather in store
cms: store.NewCommitMultiStore(db, logger, storemetrics.NewNoOpMetrics()), // by default we use a no-op metric gather in store
storeLoader: DefaultStoreLoader,
grpcQueryRouter: NewGRPCQueryRouter(),
msgServiceRouter: NewMsgServiceRouter(),
Expand Down
2 changes: 1 addition & 1 deletion baseapp/oe/optimistic_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"

"cosmossdk.io/core/log"
"cosmossdk.io/log"
)

// FinalizeBlockFunc is the function that is called by the OE to finalize the
Expand Down
5 changes: 2 additions & 3 deletions baseapp/oe/optimistic_execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import (
"errors"
"testing"

"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
"github.com/stretchr/testify/assert"

coretesting "cosmossdk.io/core/testing"
)

func testFinalizeBlock(_ context.Context, _ *abci.FinalizeBlockRequest) (*abci.FinalizeBlockResponse, error) {
return nil, errors.New("test error")
}

func TestOptimisticExecution(t *testing.T) {
oe := NewOptimisticExecution(coretesting.NewNopLogger(), testFinalizeBlock)
oe := NewOptimisticExecution(log.NewNopLogger(), testFinalizeBlock)
assert.True(t, oe.Enabled())
oe.Execute(&abci.ProcessProposalRequest{
Hash: []byte("test"),
Expand Down
10 changes: 3 additions & 7 deletions core/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package log

const ModuleKey = "module"

// Logger is the Cosmos SDK logger interface.
// It maintains as much backward compatibility with the CometBFT logger as possible.
// cosmossdk.io/log is the implementation provided by the Cosmos SDK
// All functionalities of the logger are available through the Impl() method.
// Logger defines basic logger functionality that all previous versions of the Logger interface should
// support. Library users should prefer to use this interface when possible, then type case to Logger
// to see if WithContext is supported.
type Logger interface {
// 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.
Expand All @@ -23,9 +22,6 @@ type Logger interface {
// The key of the tuple must be a string.
Debug(msg string, keyVals ...any)

// With returns a new wrapped logger with additional context provided by a set.
With(keyVals ...any) Logger

// Impl returns the underlying logger implementation.
// It is used to access the full functionalities of the underlying logger.
// Advanced users can type cast the returned value to the actual logger.
Expand Down
2 changes: 1 addition & 1 deletion core/testing/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ func (nopLogger) Info(string, ...any) {}
func (nopLogger) Warn(string, ...any) {}
func (nopLogger) Error(string, ...any) {}
func (nopLogger) Debug(string, ...any) {}
func (nopLogger) With(...any) log.Logger { return nopLogger{} }
func (nopLogger) WithContext(...any) any { return nopLogger{} }
func (nopLogger) Impl() any { return nopLogger{} }
2 changes: 2 additions & 0 deletions log/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Each entry must include the Github issue reference in the following format:

## [Unreleased]

* [#21045](https://github.com/cosmos/cosmos-sdk/pull/21045) Add `WithContext` method implementations to make all returned loggers compatible with `cosmossdk.io/core/log.Logger` without a direct dependency.

## [v1.3.1](https://github.com/cosmos/cosmos-sdk/releases/tag/log/v1.3.0) - 2024-02-05

* [#19346](https://github.com/cosmos/cosmos-sdk/pull/19346) Upgrade zerolog to v1.32.0.
Expand Down
10 changes: 0 additions & 10 deletions log/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@ module cosmossdk.io/log
go 1.20

require (
cosmossdk.io/core v0.12.0
cosmossdk.io/core/testing v0.0.0-00010101000000-000000000000
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0
gotest.tools/v3 v3.5.1
)

require (
github.com/cosmos/gogoproto v1.5.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/tidwall/btree v1.7.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sys v0.22.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)

replace cosmossdk.io/core => ../core

replace cosmossdk.io/core/testing => ../core/testing
9 changes: 0 additions & 9 deletions log/go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cosmos/gogoproto v1.5.0 h1:SDVwzEqZDDBoslaeZg+dGE55hdzHfgUA40pEanMh52o=
github.com/cosmos/gogoproto v1.5.0/go.mod h1:iUM31aofn3ymidYG6bUR5ZFrk+Om8p5s754eMUcyp8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand All @@ -16,16 +13,10 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
96 changes: 53 additions & 43 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"

corelog "cosmossdk.io/core/log"
coretesting "cosmossdk.io/core/testing"
)

func init() {
Expand All @@ -30,15 +27,39 @@ func init() {
}

// ModuleKey defines a module logging key.
const ModuleKey = corelog.ModuleKey
const ModuleKey = "module"

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

// Logger is the Cosmos SDK logger interface.
// It maintains as much backward compatibility with the CometBFT logger as possible.
// All functionalities of the logger are available through the Impl() method.
type Logger = corelog.Logger
// Deprecated: Logger is the Cosmos SDK logger interface.
// Use cosmossdk.io/core/log.Logger instead. All constructor functions in this package
// return a type that can be cast to that interface.
type Logger interface {
// 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.
Info(msg string, keyVals ...any)

// Warn takes a message and a set of key/value pairs and logs with level WARN.
// The key of the tuple must be a string.
Warn(msg string, keyVals ...any)

// Error takes a message and a set of key/value pairs and logs with level ERR.
// The key of the tuple must be a string.
Error(msg string, keyVals ...any)

// Debug takes a message and a set of key/value pairs and logs with level DEBUG.
// The key of the tuple must be a string.
Debug(msg string, keyVals ...any)

// With returns a new wrapped logger with additional context provided by a set.
With(keyVals ...any) Logger

// Impl returns the underlying logger implementation.
// It is used to access the full functionalities of the underlying logger.
// Advanced users can type cast the returned value to the actual logger.
Impl() any
}

// WithJSONMarshal configures zerolog global json encoding.
func WithJSONMarshal(marshaler func(v any) ([]byte, error)) {
Expand Down Expand Up @@ -68,6 +89,7 @@ type zeroLogWrapper struct {
//
// Stderr is the typical destination for logs,
// so that any output from your application can still be piped to other processes.
// The returned value can be safely cast to cosmossdk.io/core/log.Logger.
func NewLogger(dst io.Writer, options ...Option) Logger {
logCfg := defaultConfig
for _, opt := range options {
Expand Down Expand Up @@ -144,45 +166,33 @@ func (l zeroLogWrapper) With(keyVals ...interface{}) Logger {
return zeroLogWrapper{&logger}
}

// WithContext returns a new wrapped logger with additional context provided by a set.
func (l zeroLogWrapper) WithContext(keyVals ...interface{}) any {
logger := l.Logger.With().Fields(keyVals).Logger()
return zeroLogWrapper{&logger}
}

// Impl returns the underlying zerolog logger.
// It can be used to used zerolog structured API directly instead of the wrapper.
func (l zeroLogWrapper) Impl() interface{} {
return l.Logger
}

// NewNopLogger returns a new logger that does nothing.
var NewNopLogger = coretesting.NewNopLogger

// LogWrapper wraps a Logger and implements the Logger interface.
// it is only meant to avoid breakage of legacy versions of the Logger interface.
type LogWrapper struct {
corelog.Logger
}

func NewLogWrapper(logger corelog.Logger) Logger {
return LogWrapper{logger}
}

func (l LogWrapper) Impl() interface{} {
return l.Logger
}

func (l LogWrapper) With(keyVals ...interface{}) Logger {
return NewLogWrapper(l.Logger.With(keyVals...))
}

func (l LogWrapper) Info(msg string, keyVals ...interface{}) {
l.Logger.Info(msg, keyVals...)
}

func (l LogWrapper) Warn(msg string, keyVals ...interface{}) {
l.Logger.Warn(msg, keyVals...)
}

func (l LogWrapper) Error(msg string, keyVals ...interface{}) {
l.Logger.Error(msg, keyVals...)
}

func (l LogWrapper) Debug(msg string, keyVals ...interface{}) {
l.Logger.Debug(msg, keyVals...)
}
func NewNopLogger() Logger {
// The custom nopLogger is about 3x faster than a zeroLogWrapper with zerolog.Nop().
return nopLogger{}
}

// nopLogger is a Logger that does nothing when called.
// See the "specialized nop logger" benchmark and compare with the "zerolog nop logger" benchmark.
// The custom implementation is about 3x faster.
type nopLogger struct{}

func (nopLogger) Info(string, ...any) {}
func (nopLogger) Warn(string, ...any) {}
func (nopLogger) Error(string, ...any) {}
func (nopLogger) Debug(string, ...any) {}
func (nopLogger) With(...any) Logger { return nopLogger{} }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename method With to WithContext.

The method name should be aligned with the new LoggerV2 interface.

- func (nopLogger) With(...any) Logger     { return nopLogger{} }
+ func (nopLogger) WithContext(...any) any { return nopLogger{} }

Committable suggestion was skipped due to low confidence.

func (nopLogger) WithContext(...any) any { return nopLogger{} }
func (nopLogger) Impl() any { return nopLogger{} }
2 changes: 1 addition & 1 deletion runtime/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/log"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
authtx "cosmossdk.io/x/auth/tx"

Expand Down
20 changes: 12 additions & 8 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
"cosmossdk.io/core/comet"
"cosmossdk.io/core/genesis"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/log"
corelog "cosmossdk.io/core/log"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -269,13 +270,16 @@ func ProvideEnvironment(
memKvService = memStoreService{key: memStoreKey}
}

return kvService, memKvService, NewEnvironment(
kvService,
logger.With(log.ModuleKey, fmt.Sprintf("x/%s", key.Name())),
EnvWithMsgRouterService(msgServiceRouter),
EnvWithQueryRouterService(queryServiceRouter),
EnvWithMemStoreService(memKvService),
)
if cl, ok := logger.With(log.ModuleKey, fmt.Sprintf("x/%s", key.Name())).(corelog.Logger); ok {
return kvService, memKvService, NewEnvironment(
kvService,
cl,
EnvWithMsgRouterService(msgServiceRouter),
EnvWithQueryRouterService(queryServiceRouter),
EnvWithMemStoreService(memKvService),
)
}
panic("unexpected logger type")
}

func ProvideTransientStoreService(
Expand Down
3 changes: 1 addition & 2 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"golang.org/x/sync/errgroup"

corectx "cosmossdk.io/core/context"
corelog "cosmossdk.io/core/log"
"cosmossdk.io/log"
"cosmossdk.io/store"
"cosmossdk.io/store/snapshots"
Expand All @@ -51,7 +50,7 @@ const ServerContextKey = sdk.ContextKey("server.context")
type Context struct {
Viper *viper.Viper
Config *cmtcfg.Config
Logger corelog.Logger
Logger log.Logger
}

func NewDefaultContext() *Context {
Expand Down
2 changes: 1 addition & 1 deletion server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
coreappmgr "cosmossdk.io/core/app"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/event"
"cosmossdk.io/core/log"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/cometbft/client/grpc/cmtservice"
"cosmossdk.io/server/v2/cometbft/handlers"
Expand Down
1 change: 0 additions & 1 deletion server/v2/cometbft/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ require (
require (
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/core/testing v0.0.0-00010101000000-000000000000 // indirect
cosmossdk.io/depinject v1.0.0 // indirect
cosmossdk.io/log v1.3.1 // indirect
cosmossdk.io/math v1.3.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion server/v2/cometbft/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package log
import (
cmtlog "github.com/cometbft/cometbft/libs/log"

"cosmossdk.io/core/log"
"cosmossdk.io/log"
)

var _ cmtlog.Logger = (*CometLoggerWrapper)(nil)
Expand Down
2 changes: 1 addition & 1 deletion server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"

"cosmossdk.io/core/log"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
serverv2 "cosmossdk.io/server/v2"
cometlog "cosmossdk.io/server/v2/cometbft/log"
"cosmossdk.io/server/v2/cometbft/types"
Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/client/v2/autocli"
clienthelpers "cosmossdk.io/client/v2/helpers"
"cosmossdk.io/core/log"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/accounts"
"cosmossdk.io/x/accounts/accountstd"
Expand Down
Loading
Loading