Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Problem: fix eth log override #343 #371

Merged
merged 1 commit into from
Aug 19, 2021
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
70 changes: 12 additions & 58 deletions log/handler.go
Original file line number Diff line number Diff line change
@@ -1,76 +1,30 @@
package log

import (
"github.com/rs/zerolog"
"fmt"

"github.com/cosmos/cosmos-sdk/server"
ethlog "github.com/ethereum/go-ethereum/log"
tmlog "github.com/tendermint/tendermint/libs/log"
)

var _ ethlog.Handler = &Handler{}
var ethermintLogger *tmlog.Logger = nil

// Logger wraps the zero log Wrapper and extends it to support the ethereum logger interface.
type Handler struct {
*server.ZeroLogWrapper
func FuncHandler(fn func(r *ethlog.Record) error) ethlog.Handler {
return funcHandler(fn)
}

func NewHandler(logger tmlog.Logger) ethlog.Handler {
zerologger, ok := logger.(*server.ZeroLogWrapper)
if !ok {
// default to Stdout if not an SDK logger wrapper
return ethlog.StdoutHandler
}
type funcHandler func(r *ethlog.Record) error

return &Handler{
ZeroLogWrapper: zerologger,
}
func (h funcHandler) Log(r *ethlog.Record) error {
return h(r)
}

// Log implements the go-ethereum Logger Handler interface
func (h *Handler) Log(r *ethlog.Record) error {
lvl := EthLogLvlToZerolog(r.Lvl)

h.ZeroLogWrapper.
WithLevel(lvl).
Fields(getLogFields(r.Ctx...)).
Time(r.KeyNames.Time, r.Time).
Msg(r.Msg)
return nil
}

func EthLogLvlToZerolog(lvl ethlog.Lvl) zerolog.Level {
var level zerolog.Level

switch lvl {
case ethlog.LvlCrit:
level = zerolog.FatalLevel
case ethlog.LvlDebug:
level = zerolog.DebugLevel
case ethlog.LvlError:
level = zerolog.ErrorLevel
case ethlog.LvlInfo:
level = zerolog.InfoLevel
case ethlog.LvlTrace:
level = zerolog.TraceLevel
case ethlog.LvlWarn:
level = zerolog.WarnLevel
default:
level = zerolog.NoLevel
}
func NewHandler(logger tmlog.Logger) ethlog.Handler {

return level
}
ethermintLogger = &logger

func getLogFields(keyVals ...interface{}) map[string]interface{} {
if len(keyVals)%2 != 0 {
return FuncHandler(func(r *ethlog.Record) error {
(*ethermintLogger).Debug(fmt.Sprintf("[EVM] %v", r))
return nil
}

fields := make(map[string]interface{})
for i := 0; i < len(keyVals); i += 2 {
fields[keyVals[i].(string)] = keyVals[i+1]
}

return fields
})
}
1 change: 0 additions & 1 deletion server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

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

ethdebug "github.com/tharsis/ethermint/ethereum/rpc/namespaces/debug"
"github.com/tharsis/ethermint/log"
"github.com/tharsis/ethermint/server/config"
Expand Down