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!: Provide logger through depinject #15818

Merged
merged 41 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
99ef23e
init
facundomedica Apr 12, 2023
07ad3de
implement + adr
facundomedica Apr 12, 2023
4fd390a
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Apr 12, 2023
f709e1a
update
facundomedica Apr 12, 2023
55dfec4
use cosmossdk.io\/log
facundomedica Apr 12, 2023
1b72a66
progress
facundomedica Apr 12, 2023
e64afbf
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Apr 12, 2023
c9cafd3
fix
facundomedica Apr 12, 2023
0262fcd
small fixes
facundomedica Apr 12, 2023
65b6a86
small fixes
facundomedica Apr 12, 2023
0998943
small fixes
facundomedica Apr 12, 2023
5e1ca87
small fixes
facundomedica Apr 12, 2023
f7e41da
small fixes
facundomedica Apr 12, 2023
db7b086
cl++
facundomedica Apr 13, 2023
e3b7073
fix app v1
facundomedica Apr 13, 2023
e58ea40
fix more tests
facundomedica Apr 13, 2023
1f9c0bf
more tests
facundomedica Apr 13, 2023
db2be7f
merge
facundomedica Apr 17, 2023
c7b25a3
tests
facundomedica Apr 18, 2023
c277e3a
update bank
facundomedica Apr 18, 2023
54bb8f6
update bank
facundomedica Apr 18, 2023
39c6030
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Apr 18, 2023
87bc493
fix more test
facundomedica Apr 18, 2023
9c4c448
fix tests
facundomedica Apr 18, 2023
63bcdad
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 18, 2023
601e89f
fix
facundomedica Apr 18, 2023
7024218
Merge branch 'facu/coreapi-logger' of https://github.com/cosmos/cosmo…
facundomedica Apr 18, 2023
7312bda
fix
facundomedica Apr 18, 2023
607401a
fix conflicts
facundomedica Apr 19, 2023
cf98c07
fix some more tests
facundomedica Apr 19, 2023
046c918
im tired
facundomedica Apr 19, 2023
089e775
hopefully the last
facundomedica Apr 19, 2023
ef11a15
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 19, 2023
6261eeb
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 19, 2023
8c2cf2b
lint
facundomedica Apr 20, 2023
5b90bf4
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 20, 2023
f14a89d
Update x/bank/keeper/keeper.go
facundomedica Apr 20, 2023
c038a61
adr++ and upgrading++
facundomedica Apr 20, 2023
3a856a5
cl++
facundomedica Apr 20, 2023
f462163
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 20, 2023
a4c1aca
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 21, 2023
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
25 changes: 25 additions & 0 deletions core/log/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package log

// Service is the same as the Cosmos SDK logger interface (copied from cosmossdk.io/log).
// All functionalities of the logger are available through the Impl() method.
type Service 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)

// 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) Service

// 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
}
41 changes: 41 additions & 0 deletions docs/architecture/adr-063-core-module-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,47 @@ the block or app hash is left to the runtime to specify).
Events emitted by `EmitKVEvent` and `EmitProtoEventNonConsensus` are not considered to be part of consensus and cannot be observed
by other modules. If there is a client-side need to add events in patch releases, these methods can be used.

#### Logger Service

The logger `Service` will be defined in the `cosmossdk.io/core/log` package and it implements the full Cosmos SDK logger interface.

```go
package log

// Service is the same as the Cosmos SDK logger interface (copied from cosmossdk.io/log).
// All functionalities of the logger are available through the Impl() method.
type Service 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)

// 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) Service

// 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
}
```

Modules using this service should not need to add any context related to the module name,
as the runtime will do this when providing, following the current pattern in the SDK:

```go
func ProvideLoggerService(key depinject.ModuleKey, app *AppBuilder) log.Service {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
return LoggerService{app.app.Logger().With("module", fmt.Sprintf("x/%s", key.Name()))}
}
```

### Core `AppModule` extension interfaces


Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ replace (
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.8.1
// Downgraded to avoid bugs in following commits which caused simulations to fail.
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
cosmossdk.io/core => ./core
)

retract (
Expand Down
30 changes: 30 additions & 0 deletions runtime/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package runtime

import (
corelog "cosmossdk.io/core/log"
"cosmossdk.io/log"
)

type LoggerService struct {
logger log.Logger
}

func (ls LoggerService) Info(msg string, keyVals ...any) {
ls.logger.Info(msg, keyVals...)
}

func (ls LoggerService) Error(msg string, keyVals ...any) {
ls.logger.Error(msg, keyVals...)
}

func (ls LoggerService) Debug(msg string, keyVals ...any) {
ls.logger.Debug(msg, keyVals...)
}

func (ls LoggerService) With(keyVals ...any) corelog.Service {
return LoggerService{logger: ls.logger.With(keyVals...)}
}

func (ls LoggerService) Impl() any {
return ls.logger
}
6 changes: 6 additions & 0 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"cosmossdk.io/core/log"
"cosmossdk.io/core/store"
"github.com/cosmos/gogoproto/proto"
"google.golang.org/protobuf/reflect/protodesc"
Expand Down Expand Up @@ -65,6 +66,7 @@ func init() {
ProvideMemoryStoreService,
ProvideTransientStoreService,
ProvideEventService,
ProvideLoggerService,
),
appmodule.Invoke(SetupAppBuilder),
)
Expand Down Expand Up @@ -209,3 +211,7 @@ func ProvideTransientStoreService(key depinject.ModuleKey, app *AppBuilder) stor
func ProvideEventService() event.Service {
return EventService{}
}

func ProvideLoggerService(key depinject.ModuleKey, app *AppBuilder) log.Service {
return LoggerService{app.app.Logger().With("module", fmt.Sprintf("x/%s", key.Name()))}
}