This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
12 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters