Skip to content

Commit

Permalink
feat(runtime): implement custom logging handler that print function n…
Browse files Browse the repository at this point in the history
…ame (ChainSafe#1825)

* implement custom logging handler that print function name

* move log handler to runtime package
  • Loading branch information
edwardmack authored and timwu20 committed Dec 6, 2021
1 parent 953d6f4 commit 5301ce4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions lib/runtime/log_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2019 ChainSafe Systems (ON) Corp.
// This file is part of gossamer.
//
// The gossamer library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gossamer library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.

package runtime

import (
"fmt"
"path/filepath"
"strings"

log "github.com/ChainSafe/log15"
)

// CustomFileHandler returns a Handler that adds the name of the calling function to the context with key "func"
// and the line number and file of the calling function to the context with key "caller".
func CustomFileHandler(h log.Handler) log.Handler {
return log.FuncHandler(func(r *log.Record) error {
r.Ctx = append(r.Ctx, "func", strings.TrimLeft(filepath.Ext(r.Call.Frame().Function), "."), "caller", fmt.Sprint(r.Call))
return h.Log(r)
})
}
2 changes: 1 addition & 1 deletion lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func newInstance(code []byte, cfg *Config) (*Instance, error) {
// if cfg.LogLvl set to < 0, then don't change package log level
if cfg.LogLvl >= 0 {
h := log.StreamHandler(os.Stdout, log.TerminalFormat())
h = log.CallerFileHandler(h)
h = runtime.CustomFileHandler(h)
logger.SetHandler(log.LvlFilterHandler(cfg.LogLvl, h))
}

Expand Down

0 comments on commit 5301ce4

Please sign in to comment.