forked from ava-labs/subnet-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor logger & add json log format (ava-labs#198)
* refactor logger & add json log format * add fallback comment
- Loading branch information
Showing
4 changed files
with
85 additions
and
44 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
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
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// (c) 2019-2020, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package evm | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
type SubnetEVMLogger struct { | ||
log.Handler | ||
} | ||
|
||
// InitLogger initializes logger with alias and sets the log level and format with the original [os.StdErr] interface | ||
// along with the context logger. | ||
func InitLogger(alias string, level string, jsonFormat bool, writer io.Writer) (SubnetEVMLogger, error) { | ||
logFormat := SubnetEVMTermFormat(alias) | ||
if jsonFormat { | ||
logFormat = SubnetEVMJSONFormat(alias) | ||
} | ||
|
||
// Create handler | ||
logHandler := log.StreamHandler(writer, logFormat) | ||
c := SubnetEVMLogger{Handler: logHandler} | ||
|
||
if err := c.SetLogLevel(level); err != nil { | ||
return SubnetEVMLogger{}, err | ||
} | ||
return c, nil | ||
} | ||
|
||
// SetLogLevel sets the log level of initialized log handler. | ||
func (c *SubnetEVMLogger) SetLogLevel(level string) error { | ||
// Set log level | ||
logLevel, err := log.LvlFromString(level) | ||
if err != nil { | ||
return err | ||
} | ||
log.Root().SetHandler(log.LvlFilterHandler(logLevel, c)) | ||
return nil | ||
} | ||
|
||
func SubnetEVMTermFormat(alias string) log.Format { | ||
prefix := fmt.Sprintf("<%s Chain>", alias) | ||
return log.FormatFunc(func(r *log.Record) []byte { | ||
location := fmt.Sprintf("%+v", r.Call) | ||
newMsg := fmt.Sprintf("%s %s: %s", prefix, location, r.Msg) | ||
r.Msg = newMsg | ||
return log.TerminalFormat(false).Format(r) | ||
}) | ||
} | ||
|
||
func SubnetEVMJSONFormat(alias string) log.Format { | ||
prefix := fmt.Sprintf("%s Chain", alias) | ||
return log.FormatFunc(func(r *log.Record) []byte { | ||
location := fmt.Sprintf("%+v", r.Call) | ||
r.KeyNames.Lvl = "level" | ||
r.KeyNames.Time = "timestamp" | ||
r.Ctx = append(r.Ctx, "logger", prefix) | ||
r.Ctx = append(r.Ctx, "caller", location) | ||
|
||
return log.JSONFormat().Format(r) | ||
}) | ||
} |
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