From 1761995482443977fe15d575e06acdf420df92bd Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 Jan 2019 23:21:36 -0600 Subject: [PATCH] skip logger pkg --- backend/Gopkg.lock | 6 +-- backend/app/main.go | 2 +- .../vendor/github.com/go-pkgz/lgr/logger.go | 48 +++++++++++++------ 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/backend/Gopkg.lock b/backend/Gopkg.lock index 63b1c0cf..7a9bd53c 100644 --- a/backend/Gopkg.lock +++ b/backend/Gopkg.lock @@ -127,12 +127,12 @@ version = "v0.4.0" [[projects]] - digest = "1:6722b5fcd930a31c3b7bcaca3b364a406395dda4d0d9319726f778efcbc1e739" + digest = "1:7b1f422f560b103f435f8501f854f238f1ce00d0f8146e76eacdfa71f2c8d8c0" name = "github.com/go-pkgz/lgr" packages = ["."] pruneopts = "UT" - revision = "5d893e7027218cc87667cf280a5894a2f4a5f170" - version = "v0.3.1" + revision = "dbd940fec9650260d15aa071cab658d0502ed92f" + version = "v0.3.2" [[projects]] digest = "1:c509e3f646c48148f2239fa66d5cd62785dfd057b5d2b46972e45b60049c19ed" diff --git a/backend/app/main.go b/backend/app/main.go index b23bf9d3..3bf6c72a 100644 --- a/backend/app/main.go +++ b/backend/app/main.go @@ -65,7 +65,7 @@ func setupLog(dbg bool) { log.Setup(log.Debug, log.CallerFile, log.Msec, log.LevelBraces) return } - log.Setup(log.Msec, log.LevelBraces, log.CallerPkg) + log.Setup(log.Msec, log.LevelBraces, log.CallerPkg, log.CallerIgnore("logger")) } // getDump reads runtime stack and returns as a string diff --git a/backend/vendor/github.com/go-pkgz/lgr/logger.go b/backend/vendor/github.com/go-pkgz/lgr/logger.go index 6d4de4b9..ad12b974 100644 --- a/backend/vendor/github.com/go-pkgz/lgr/logger.go +++ b/backend/vendor/github.com/go-pkgz/lgr/logger.go @@ -15,17 +15,18 @@ var levels = []string{"DEBUG", "INFO", "WARN", "ERROR", "PANIC", "FATAL"} // Logger provided simple logger with basic support of levels. Thread safe type Logger struct { - stdout, stderr io.Writer - dbg bool - lock sync.Mutex - callerFile bool - callerFunc bool - callerPkg bool - now nowFn - fatal panicFn - skipCallers int - levelBraces bool - msec bool + stdout, stderr io.Writer + dbg bool + lock sync.Mutex + callerFile bool + callerFunc bool + callerPkg bool + ignoredPkgCallers []string + now nowFn + fatal panicFn + skipCallers int + levelBraces bool + msec bool } type nowFn func() time.Time @@ -73,12 +74,13 @@ func (l *Logger) Logf(format string, args ...interface{}) { if l.callerFile || l.callerFunc || l.callerPkg { if pc, file, line, ok := runtime.Caller(l.skipCallers); ok { - funcName := "" + funcName, fileInfo := "", "" + if l.callerFunc { funcNameElems := strings.Split(runtime.FuncForPC(pc).Name(), "/") funcName = funcNameElems[len(funcNameElems)-1] } - fileInfo := "" + if l.callerFile { fnameElems := strings.Split(file, "/") fileInfo = fmt.Sprintf("%s:%d", strings.Join(fnameElems[len(fnameElems)-2:], "/"), line) @@ -86,7 +88,9 @@ func (l *Logger) Logf(format string, args ...interface{}) { fileInfo += " " } } + // callerPkg only if no other callers if l.callerPkg && !l.callerFile && !l.callerFunc { + file = l.ignoreCaller(file) _, fileInfo = path.Split(path.Dir(file)) if l.callerFunc { fileInfo += " " @@ -117,6 +121,15 @@ func (l *Logger) Logf(format string, args ...interface{}) { l.lock.Unlock() } +func (l *Logger) ignoreCaller(p string) string { + for _, s := range l.ignoredPkgCallers { + if strings.Contains(p, "/"+s+"/") { + return strings.Replace(p, "/"+s, "", 1) + } + } + return p +} + func (l *Logger) formatLevel(lv string) string { brace := func(b string) string { @@ -192,11 +205,18 @@ func CallerFunc(l *Logger) { l.callerFunc = true } -// Pkg adds caller's package name +// CallerPkg adds caller's package name func CallerPkg(l *Logger) { l.callerPkg = true } +// CallerIgnore sets packages skipped from logging caller +func CallerIgnore(ignores ...string) Option { + return func(l *Logger) { + l.ignoredPkgCallers = ignores + } +} + // LevelBraces adds [] to level func LevelBraces(l *Logger) { l.levelBraces = true