Skip to content

Commit

Permalink
fix: panic in case NewContext is used to initialize logger with Fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
rhnvrm committed Mar 1, 2019
1 parent 83c106f commit be0d2a9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (e ChainEntry) Write() {
e.Entry.enc.Release()

if e.exit {
e.Entry.l.ExitFn(1)
e.Entry.l.exit(1)
}
}

Expand Down
14 changes: 12 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func NewContext(w io.Writer, levels uint8, contextName string) *Logger {
w: w,
levels: levels,
contextName: contextName,
ExitFn: os.Exit,
}
}

Expand All @@ -93,6 +94,7 @@ func (l *Logger) copy(ctxName string) *Logger {
w: l.w,
hook: l.hook,
contextName: ctxName,
ExitFn: l.ExitFn,
}
if len(l.ctx) > 0 {
var ctx = make([]func(e Entry), len(l.ctx))
Expand Down Expand Up @@ -485,7 +487,7 @@ func (l *Logger) Fatal(msg string) {

e.enc.Release()

l.ExitFn(1)
l.exit(1)
}

// FatalWith returns a ChainEntry with FATAL level.
Expand Down Expand Up @@ -546,7 +548,7 @@ func (l *Logger) FatalWithFields(msg string, fields func(Entry)) {
l.finalizeIfContext(e)

e.enc.Release()
l.ExitFn(1)
l.exit(1)
}

func (l *Logger) openEntry(enc *Encoder) {
Expand Down Expand Up @@ -617,6 +619,14 @@ func (l *Logger) closeEntry(e Entry) {
}
}

func (l *Logger) exit(code int) {
if l.ExitFn == nil {
// fallback to os.Exit to prevent panic incase set as nil.
os.Exit(code)
}
l.ExitFn(code)
}

// Caller returns the caller in the stack trace, skipped n times.
func (l *Logger) Caller(n int) string {
_, f, fl, _ := runtime.Caller(n)
Expand Down
25 changes: 24 additions & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package onelog

import (
"errors"
"os"
"os/exec"
"strings"
"testing"

Expand Down Expand Up @@ -80,7 +82,7 @@ func TestOnelogFeature(t *testing.T) {
logger := New(nil, DEBUG|INFO|WARN|ERROR|FATAL)
str := logger.Caller(1)
strs := strings.Split(str, "/")
assert.Equal(t, "logger_test.go:81", strs[len(strs)-1], "file should be logger_test.go:81")
assert.Equal(t, "logger_test.go:83", strs[len(strs)-1], "file should be logger_test.go:81")
})
}
func TestOnelogWithoutFields(t *testing.T) {
Expand Down Expand Up @@ -1256,3 +1258,24 @@ func TestOnelogFieldsChainAndContext(t *testing.T) {
logger.Fatal("message")
})
}

func TestFatalActualOsExit(t *testing.T) {
if os.Getenv("FatalActualOsExit") == "1" {
parent := NewContext(os.Stdout, DEBUG|INFO|WARN|ERROR|FATAL, "params")
logger := parent.WithContext("")
logger.FatalWithFields("message", func(e Entry) {
e.String("userID", "123456")
e.String("action", "login")
e.String("result", "success")
e.Int64("int64", 120)
})
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFatalActualOsExit")
cmd.Env = append(os.Environ(), "FatalActualOsExit=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}

0 comments on commit be0d2a9

Please sign in to comment.