Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime): implement custom logging handler that print function name #1825

Merged
merged 4 commits into from
Oct 5, 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
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