From f317574a6d848e993b9f3d508fa8e0febbd7548c Mon Sep 17 00:00:00 2001 From: Doug Lauder Date: Thu, 15 Jul 2021 11:42:53 -0400 Subject: [PATCH] add mlog package to stack filter list (#717) --- server/services/mlog/mlog.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/server/services/mlog/mlog.go b/server/services/mlog/mlog.go index 04b9b8c1e..ee0cb6fbc 100644 --- a/server/services/mlog/mlog.go +++ b/server/services/mlog/mlog.go @@ -8,6 +8,8 @@ import ( "io/ioutil" "log" "os" + "runtime" + "strings" "time" "github.com/mattermost/logr/v2" @@ -18,6 +20,18 @@ const ( ShutdownTimeout = time.Second * 15 ) +var ( + mlogPkg string +) + +func init() { + // Calc current package name + pcs := make([]uintptr, 2) + _ = runtime.Callers(0, pcs) + tmp := runtime.FuncForPC(pcs[1]).Name() + mlogPkg = GetPackageName(tmp) +} + // Type and function aliases from Logr to limit the spread of dependencies throughout Focalboard. type Field = logr.Field type Level = logr.Level @@ -108,6 +122,8 @@ type Logger struct { // NewLogger creates a new Logger instance which can be configured via `(*Logger).Configure`. func NewLogger(options ...Option) *Logger { + options = append(options, logr.StackFilter(GetPackageName(mlogPkg))) + lgr, _ := logr.New(options...) log := lgr.NewLogger() @@ -248,3 +264,18 @@ func (l *Logger) Shutdown() error { defer cancel() return l.log.Logr().ShutdownWithTimeout(ctx) } + +// GetPackageName reduces a fully qualified function name to the package name +// By sirupsen: https://github.com/sirupsen/logrus/blob/master/entry.go +func GetPackageName(f string) string { + for { + lastPeriod := strings.LastIndex(f, ".") + lastSlash := strings.LastIndex(f, "/") + if lastPeriod > lastSlash { + f = f[:lastPeriod] + } else { + break + } + } + return f +}