Skip to content

Commit

Permalink
add caller pkg options
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 23, 2019
1 parent 8f56fcc commit b954e9f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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`
Expand Down
34 changes: 24 additions & 10 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"path"
"runtime"
"strings"
"sync"
Expand All @@ -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
Expand Down Expand Up @@ -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 := ""
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ func TestLoggerWithDbg(t *testing.T) {
assert.Equal(t, "2018/01/07 13:02:34 DEBUG {lgr.TestLoggerWithDbg} something 123 err\n", rout.String())
}

func TestLoggerWithPkg(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Debug, Out(rout), Err(rerr), CallerPkg, Msec)
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
l.Logf("[DEBUG] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34.123 DEBUG {lgr} something 123 err\n", rout.String())

l = New(Debug, Out(rout), Err(rerr), CallerPkg, CallerFile, Msec)
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
rout.Reset()
rerr.Reset()
l.Logf("[DEBUG] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34.123 DEBUG {lgr/logger_test.go:126} something 123 err\n", rout.String())

l = New(Debug, Out(rout), Err(rerr), CallerPkg, CallerFunc, Msec)
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
rout.Reset()
rerr.Reset()
l.Logf("[DEBUG] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34.123 DEBUG {lgr.TestLoggerWithPkg} something 123 err\n", rout.String())
}

func TestLoggerWithLevelBraces(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Debug, Out(rout), Err(rerr), LevelBraces, Msec)
Expand Down

0 comments on commit b954e9f

Please sign in to comment.