Skip to content

Commit 5301ce4

Browse files
edwardmacktimwu20
authored andcommitted
feat(runtime): implement custom logging handler that print function name (ChainSafe#1825)
* implement custom logging handler that print function name * move log handler to runtime package
1 parent 953d6f4 commit 5301ce4

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/runtime/log_handler.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2019 ChainSafe Systems (ON) Corp.
2+
// This file is part of gossamer.
3+
//
4+
// The gossamer library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The gossamer library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package runtime
18+
19+
import (
20+
"fmt"
21+
"path/filepath"
22+
"strings"
23+
24+
log "github.com/ChainSafe/log15"
25+
)
26+
27+
// CustomFileHandler returns a Handler that adds the name of the calling function to the context with key "func"
28+
// and the line number and file of the calling function to the context with key "caller".
29+
func CustomFileHandler(h log.Handler) log.Handler {
30+
return log.FuncHandler(func(r *log.Record) error {
31+
r.Ctx = append(r.Ctx, "func", strings.TrimLeft(filepath.Ext(r.Call.Frame().Function), "."), "caller", fmt.Sprint(r.Call))
32+
return h.Log(r)
33+
})
34+
}

lib/runtime/wasmer/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func newInstance(code []byte, cfg *Config) (*Instance, error) {
108108
// if cfg.LogLvl set to < 0, then don't change package log level
109109
if cfg.LogLvl >= 0 {
110110
h := log.StreamHandler(os.Stdout, log.TerminalFormat())
111-
h = log.CallerFileHandler(h)
111+
h = runtime.CustomFileHandler(h)
112112
logger.SetHandler(log.LvlFilterHandler(cfg.LogLvl, h))
113113
}
114114

0 commit comments

Comments
 (0)