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
21 changes: 18 additions & 3 deletions core/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ const ModuleKey = "module"
// cosmossdk.io/log is the implementation provided by the Cosmos SDK
// All functionalities of the logger are available through the Impl() method.
type Logger interface {
LoggerBase

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

// LoggerBase defines basic logger functionality.
type LoggerBase 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)
Expand All @@ -23,11 +31,18 @@ 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.
Impl() any
}

// LoggerV2 is the newer Cosmos SDK logger interface. It can be used in libraries without any need to import
// the core or log packages directly.
type LoggerV2 interface {
LoggerBase

// WithV2 returns a type which can be cast to LoggerV2 with additional context provided by a set.
// any is returned instead of LoggerV2 to avoid the need for log users to import the log package directly.
WithV2(keyVals ...any) any
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not attached to this naming if someone can think of something better.

Copy link
Member

@julienrbrt julienrbrt Jul 24, 2024

Choose a reason for hiding this comment

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

AH! I was scratching my head yesterday about how to achieve the same thing main...julien/log-dep :D

We quite need log to have no core dep as well actually.

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually don't like WithV2 in a function name if this is going to be long lived. How about WithContext?

25 changes: 20 additions & 5 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ var ContextKey struct{}
// All functionalities of the logger are available through the Impl() method.
type Logger = corelog.Logger

// LoggerV2 is the new type that logger's implement which supports the core Logger and LoggerV2 interfaces.
type LoggerV2 = interface {
Logger
corelog.LoggerV2
}

// WithJSONMarshal configures zerolog global json encoding.
func WithJSONMarshal(marshaler func(v any) ([]byte, error)) {
zerolog.InterfaceMarshalFunc = func(i any) ([]byte, error) {
Expand Down Expand Up @@ -68,7 +74,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.
func NewLogger(dst io.Writer, options ...Option) Logger {
func NewLogger(dst io.Writer, options ...Option) LoggerV2 {
logCfg := defaultConfig
for _, opt := range options {
opt(&logCfg)
Expand Down Expand Up @@ -110,7 +116,7 @@ func NewLogger(dst io.Writer, options ...Option) Logger {
}

// NewCustomLogger returns a new logger with the given zerolog logger.
func NewCustomLogger(logger zerolog.Logger) Logger {
func NewCustomLogger(logger zerolog.Logger) LoggerV2 {
return zeroLogWrapper{&logger}
}

Expand Down Expand Up @@ -139,7 +145,12 @@ func (l zeroLogWrapper) Debug(msg string, keyVals ...interface{}) {
}

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

func (l zeroLogWrapper) WithV2(keyVals ...interface{}) any {
logger := l.Logger.With().Fields(keyVals).Logger()
return zeroLogWrapper{&logger}
}
Expand All @@ -159,15 +170,19 @@ type LogWrapper struct {
corelog.Logger
}

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

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

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

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

Expand Down
8 changes: 4 additions & 4 deletions log/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type TestingT zerolog.TestingLog
// If the logs may help debug a test failure,
// you may want to use NewTestLogger(t) in your test.
// Otherwise, use NewNopLogger().
func NewTestLogger(t TestingT) Logger {
func NewTestLogger(t TestingT) LoggerV2 {
return newTestLogger(t, zerolog.DebugLevel)
}

Expand All @@ -25,7 +25,7 @@ func NewTestLogger(t TestingT) Logger {
//
// This is primarily helpful during active debugging of a test
// with verbose logs.
func NewTestLoggerInfo(t TestingT) Logger {
func NewTestLoggerInfo(t TestingT) LoggerV2 {
return newTestLogger(t, zerolog.InfoLevel)
}

Expand All @@ -34,11 +34,11 @@ func NewTestLoggerInfo(t TestingT) Logger {
//
// This is primarily helpful during active debugging of a test
// with verbose logs.
func NewTestLoggerError(t TestingT) Logger {
func NewTestLoggerError(t TestingT) LoggerV2 {
return newTestLogger(t, zerolog.ErrorLevel)
}

func newTestLogger(t TestingT, lvl zerolog.Level) Logger {
func newTestLogger(t TestingT, lvl zerolog.Level) LoggerV2 {
cw := zerolog.NewConsoleWriter()
cw.Out = zerolog.TestWriter{
T: t,
Expand Down
Loading