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

Added logging config #59

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func StartCmdWithOptions[C any](
RunE: func(cmd *cobra.Command, args []string) error {
// Create a context that will be cancelled when the user presses Ctrl+C
// (process receives termination signal).
logger := log.NewBlankLogger(cmd.OutOrStdout())
ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)

configPath, err := cmd.Flags().GetString(flags.ConfigPath)
Expand Down Expand Up @@ -67,6 +66,7 @@ func StartCmdWithOptions[C any](

ab := baseapp.NewAppBuilder(app.Name())

logger := log.NewWithCfg(cmd.OutOrStdout(), app.Name(), cfg.Log)
// // Maybe move this to BuildApp?
// ethClient := eth.NewHealthCheckedClient(&cfg.Eth)
// ab.RegisterEthClient(ethClient)
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"github.com/berachain/offchain-sdk/client/eth"
"github.com/berachain/offchain-sdk/log"
"github.com/berachain/offchain-sdk/server"
)

Expand All @@ -19,4 +20,7 @@ type Config[C any] struct {

// Server Config
Server server.Config

// Log Config
Log log.Config
}
25 changes: 25 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"io"
"time"

"github.com/rs/zerolog"

"cosmossdk.io/log"
)

Expand Down Expand Up @@ -34,6 +36,11 @@ type Logger interface {
Impl() any
}

type Config struct {
Format string
Level string
}

// loggerImpl is the implementation of the Logger interface.
type loggerImpl struct {
log.Logger
Expand All @@ -45,6 +52,24 @@ func (l *loggerImpl) With(keyVals ...any) Logger {
return &loggerImpl{logger}
}

func NewWithCfg(dst io.Writer, runner string, cfg Config) Logger {
level, err := zerolog.ParseLevel(cfg.Level)
if err != nil {
level = zerolog.DebugLevel
}
opts := []log.Option{
log.LevelOption(level),
log.TimeFormatOption(time.RFC3339),
}
if cfg.Format == "json" {
opts = append(opts, log.OutputJSONOption())
} else {
opts = append(opts, log.ColorOption(true))
}
logger := log.NewLogger(dst, opts...)
return &loggerImpl{logger.With("namespace", runner)}
}

// NewLogger creates a new logger with the given writer and runner name.
// The logger is a wrapper around cosmossdk logger.
func NewLogger(dst io.Writer, runner string) Logger {
Expand Down
Loading