diff --git a/lib/log/log.go b/lib/log/log.go index 3f66c4bb5..adf05570d 100644 --- a/lib/log/log.go +++ b/lib/log/log.go @@ -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. @@ -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. @@ -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 diff --git a/lib/log/logger.go b/lib/log/logger.go index 88ef77de3..de3f0e287 100644 --- a/lib/log/logger.go +++ b/lib/log/logger.go @@ -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. } diff --git a/relayer/app/ethlog.go b/relayer/app/ethlog.go index ff6d665a4..220ad0419 100644 --- a/relayer/app/ethlog.go +++ b/relayer/app/ethlog.go @@ -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 ðLogger{ + 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 ðLogger{ + 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 -}