From 5301ce47f40a3a8b94f4d0f0ee954abaa487bc4e Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 5 Oct 2021 15:38:28 -0400 Subject: [PATCH] feat(runtime): implement custom logging handler that print function name (#1825) * implement custom logging handler that print function name * move log handler to runtime package --- lib/runtime/log_handler.go | 34 ++++++++++++++++++++++++++++++++++ lib/runtime/wasmer/instance.go | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 lib/runtime/log_handler.go diff --git a/lib/runtime/log_handler.go b/lib/runtime/log_handler.go new file mode 100644 index 0000000000..e73f63f1d7 --- /dev/null +++ b/lib/runtime/log_handler.go @@ -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 . + +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) + }) +} diff --git a/lib/runtime/wasmer/instance.go b/lib/runtime/wasmer/instance.go index 85eabbe730..64720bdba1 100644 --- a/lib/runtime/wasmer/instance.go +++ b/lib/runtime/wasmer/instance.go @@ -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)) }