1
0
mirror of https://github.com/mattermost/focalboard.git synced 2025-01-05 14:50:29 +02:00

Merge branch 'main' of github.com:mattermost/focalboard

This commit is contained in:
Chen-I Lim 2021-07-15 13:55:51 -07:00
commit 70bdf41f28

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
}