add caller pkg to log
This commit is contained in:
+4
-3
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user