Skip to content

Commit

Permalink
fix(relay): make logger code more go-idiomatic (#206)
Browse files Browse the repository at this point in the history
Stop passing context around as struct fields make the code a little bit
more Go-idiomatic.

task: none

---------

Co-authored-by: corver <corver@omni.network>
  • Loading branch information
pavelnikolov and corverroos authored Jan 30, 2024
1 parent db2bd1a commit ceed962
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
8 changes: 4 additions & 4 deletions lib/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func WithCtx(ctx context.Context, attrs ...any) context.Context {

// Debug logs the message and attributes at default level.
func Debug(ctx context.Context, msg string, attrs ...any) {
getLogger(ctx).DebugContext(ctx, msg, mergeAttrs(ctx, attrs)...)
GetLogger(ctx).DebugContext(ctx, msg, mergeAttrs(ctx, attrs)...)
}

// Info logs the message and attributes at info level.
func Info(ctx context.Context, msg string, attrs ...any) {
getLogger(ctx).InfoContext(ctx, msg, mergeAttrs(ctx, attrs)...)
GetLogger(ctx).InfoContext(ctx, msg, mergeAttrs(ctx, attrs)...)
}

// Warn logs the message and error and attributes at warning level.
Expand All @@ -39,7 +39,7 @@ func Warn(ctx context.Context, msg string, err error, attrs ...any) {
attrs = append(attrs, errAttrs(err)...)
}

getLogger(ctx).WarnContext(ctx, msg, mergeAttrs(ctx, attrs)...)
GetLogger(ctx).WarnContext(ctx, msg, mergeAttrs(ctx, attrs)...)
}

// Error logs the message and error and arguments at error level.
Expand All @@ -49,7 +49,7 @@ func Error(ctx context.Context, msg string, err error, attrs ...any) {
attrs = append(attrs, "err", err)
attrs = append(attrs, errAttrs(err)...)
}
getLogger(ctx).ErrorContext(ctx, msg, mergeAttrs(ctx, attrs)...)
GetLogger(ctx).ErrorContext(ctx, msg, mergeAttrs(ctx, attrs)...)
}

// errFields is similar to z.Err and returns the structured error fields and
Expand Down
3 changes: 2 additions & 1 deletion lib/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func WithLogger(ctx context.Context, logger *slog.Logger) context.Context {
return context.WithValue(ctx, loggerKey{}, logger)
}

func getLogger(ctx context.Context) *slog.Logger {
// GetLogger returns the logger from the context, or the global logger if not present.
func GetLogger(ctx context.Context) *slog.Logger {
if l := ctx.Value(loggerKey{}); l != nil {
return l.(*slog.Logger) //nolint:forcetypeassert,revive // We know the type.
}
Expand Down
71 changes: 32 additions & 39 deletions relayer/app/ethlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,83 @@ package relayer

import (
"context"
"log/slog"

"github.com/omni-network/omni/lib/log"

ethlog "github.com/ethereum/go-ethereum/log"

"golang.org/x/exp/slog"
eslog "golang.org/x/exp/slog"
)

var _ ethlog.Logger = (*ethLogger)(nil)

type ethLogger struct {
ctx context.Context //nolint:containedctx // This is a wrapper around the omni logger which is context based.
log *slog.Logger
}

// WrapLogger wraps the logger inside the context and returns a new logger compatible with the
// Ethereum logger interface.
func WrapLogger(ctx context.Context) ethlog.Logger {
return &ethLogger{
log: log.GetLogger(ctx),
}
}

func (e ethLogger) With(ctx ...any) ethlog.Logger {
return ethLogger{
ctx: log.WithCtx(e.ctx, ctx...),
log: e.log.With(ctx...),
}
}

func (e ethLogger) New(ctx ...any) ethlog.Logger {
return e.With(ctx...)
return &ethLogger{
log: e.log.With(ctx...),
}
}

func (e ethLogger) Log(level slog.Level, msg string, ctx ...any) {
func (e ethLogger) Log(level eslog.Level, msg string, ctx ...any) {
e.Write(level, msg, ctx...)
}

func (e ethLogger) Trace(msg string, ctx ...any) {
log.Debug(e.ctx, msg, ctx...)
e.log.Debug(msg, ctx...)
}

func (e ethLogger) Debug(msg string, ctx ...any) {
log.Debug(e.ctx, msg, ctx...)
e.log.Debug(msg, ctx...)
}

func (e ethLogger) Info(msg string, ctx ...any) {
log.Info(e.ctx, msg, ctx...)
e.log.Info(msg, ctx...)
}

func (e ethLogger) Warn(msg string, ctx ...any) {
keyVals, err := splitOutError(ctx)
log.Warn(e.ctx, msg, err, keyVals...)
e.log.Warn(msg, ctx...)
}

func (e ethLogger) Error(msg string, ctx ...any) {
keyVals, err := splitOutError(ctx)
log.Error(e.ctx, msg, err, keyVals...)
e.log.Error(msg, ctx...)
}

func (e ethLogger) Crit(msg string, ctx ...any) {
// I don't want to do os.exit here
keyVals, err := splitOutError(ctx)
log.Error(e.ctx, msg, err, keyVals...)
e.log.Error(msg, ctx...)
}

func (e ethLogger) Write(level slog.Level, msg string, attrs ...any) {
func (e ethLogger) Write(level eslog.Level, msg string, attrs ...any) {
switch level {
case slog.LevelInfo:
e.Info(msg, attrs...)
case slog.LevelWarn:
e.Warn(msg, attrs...)
case slog.LevelError:
e.Error(msg, attrs...)
case slog.LevelDebug:
e.Debug(msg, attrs...)
case eslog.LevelInfo:
e.log.Info(msg, attrs...)
case eslog.LevelWarn:
e.log.Warn(msg, attrs...)
case eslog.LevelError:
e.log.Error(msg, attrs...)
case eslog.LevelDebug:
e.log.Debug(msg, attrs...)
}
}

func (ethLogger) Enabled(context.Context, slog.Level) bool {
func (ethLogger) Enabled(context.Context, eslog.Level) bool {
return true
}

// splitOutError splits the keyvals into a slice of keyvals without the error and the error.
func splitOutError(keyvals []any) ([]any, error) {
var remaining []any
var err error
for i := 0; i < len(keyvals); i += 2 {
if keyErr, ok := keyvals[i+1].(error); ok {
err = keyErr
} else {
remaining = append(remaining, keyvals[i], keyvals[i+1])
}
}

return remaining, err
}

0 comments on commit ceed962

Please sign in to comment.