add mlog package to stack filter list (#717)

This commit is contained in:
Doug Lauder 2021-07-15 11:42:53 -04:00 committed by GitHub
parent 2ea4a85495
commit f317574a6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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
}