From 32503395ea05dd82d5a734059d57e45346ca72fe Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 Jan 2019 18:30:22 -0600 Subject: [PATCH] add caller pkg to log --- backend/Gopkg.lock | 6 ++-- backend/app/main.go | 2 +- .../vendor/github.com/go-pkgz/lgr/README.md | 7 ++-- .../vendor/github.com/go-pkgz/lgr/logger.go | 34 +++++++++++++------ 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/backend/Gopkg.lock b/backend/Gopkg.lock index a1c44262..85a87893 100644 --- a/backend/Gopkg.lock +++ b/backend/Gopkg.lock @@ -127,12 +127,12 @@ version = "v0.4.0" [[projects]] - digest = "1:1933dabfb0e07548ed9684fdef857cc6f2413bbbb74ba838a7c1f77407b8fd90" + digest = "1:95ecc77bb6600d33106475c022d53b2f237b1570cbf58db17483b17367fa75b9" name = "github.com/go-pkgz/lgr" packages = ["."] pruneopts = "UT" - revision = "8f56fccc1a15dfe9c7f72e049fc87cc378205b97" - version = "v0.2.2" + revision = "b954e9fcc3ef4de2c07d443835834660fd11018e" + version = "v0.3.0" [[projects]] digest = "1:c509e3f646c48148f2239fa66d5cd62785dfd057b5d2b46972e45b60049c19ed" diff --git a/backend/app/main.go b/backend/app/main.go index c49963ab..b23bf9d3 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.Setup(log.Msec, log.LevelBraces, log.CallerPkg) } // getDump reads runtime stack and returns as a string diff --git a/backend/vendor/github.com/go-pkgz/lgr/README.md b/backend/vendor/github.com/go-pkgz/lgr/README.md index 875130db..7521ed3c 100644 --- a/backend/vendor/github.com/go-pkgz/lgr/README.md +++ b/backend/vendor/github.com/go-pkgz/lgr/README.md @@ -18,7 +18,7 @@ output looks like this: 2018/01/07 13:02:34.015 DEBUG {svc/handler.go:155 h.MyFunc2} some less important err message, file is too small` ``` -_Without `lgr.CallerFile` it will drop `{caller}` part_ +_Without `lgr.Caller*` it will drop `{caller}` part_ ## details @@ -33,8 +33,9 @@ _Without `lgr.CallerFile` it will drop `{caller}` part_ `lgr.New` call accepts functional options: - `lgr.Debug` - turn debug mode on. This allows messages with "DEBUG" level (filtered overwise) -- `lgr.CallerFile` - adds the caller file info each message -- `lgr.CallerFunc` - adds the caller function info each message +- `lgr.CallerFile` - adds the caller file info +- `lgr.CallerFunc` - adds the caller function info +- `lgr.CallerPkg` - adds the caller package - `lgr.LevelBraces` - wraps levels with "[" and "]" - `lgr.Msec` - adds milliseconds to timestamp - `lgr.Out(io.Writer)` - sets the output writer, default `os.Stdout` diff --git a/backend/vendor/github.com/go-pkgz/lgr/logger.go b/backend/vendor/github.com/go-pkgz/lgr/logger.go index 5d1c6e5a..a5f2a3f2 100644 --- a/backend/vendor/github.com/go-pkgz/lgr/logger.go +++ b/backend/vendor/github.com/go-pkgz/lgr/logger.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "path" "runtime" "strings" "sync" @@ -14,15 +15,17 @@ 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, callerFunc 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 + now nowFn + fatal panicFn + skipCallers int + levelBraces bool + msec bool } type nowFn func() time.Time @@ -67,7 +70,7 @@ func (l *Logger) Logf(format string, args ...interface{}) { bld.WriteString(l.formatLevel(lv)) bld.WriteString(" ") - if l.dbg && (l.callerFile || l.callerFunc) { + if l.dbg && (l.callerFile || l.callerFunc || l.callerPkg) { if pc, file, line, ok := runtime.Caller(l.skipCallers); ok { funcName := "" @@ -83,6 +86,12 @@ func (l *Logger) Logf(format string, args ...interface{}) { fileInfo += " " } } + if l.callerPkg && !l.callerFile && !l.callerFunc { + _, fileInfo = path.Split(path.Dir(file)) + if l.callerFunc { + fileInfo += " " + } + } srcFileInfo := fmt.Sprintf("{%s%s} ", fileInfo, funcName) bld.WriteString(srcFileInfo) } @@ -183,6 +192,11 @@ func CallerFunc(l *Logger) { l.callerFunc = true } +// Pkg adds caller's package name +func CallerPkg(l *Logger) { + l.callerPkg = true +} + // LevelBraces adds [] to level func LevelBraces(l *Logger) { l.levelBraces = true