add caller pkg to log

This commit is contained in:
Umputun
2019-01-22 18:30:22 -06:00
parent e82a8f9824
commit 32503395ea
4 changed files with 32 additions and 17 deletions
+3 -3
View File
@@ -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"
+1 -1
View File
@@ -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
+4 -3
View File
@@ -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`
+24 -10
View File
@@ -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