Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: panic in case NewContext is used to initialize logger with Fatal #22

Merged
merged 1 commit into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
26 changes: 25 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,25 @@ 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.ExitFn = nil
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)
}