From c9bfdc572f0715926af8bb51b6f3a66201f27367 Mon Sep 17 00:00:00 2001 From: Vladimir Skipor Date: Sun, 19 Feb 2017 15:23:42 +0300 Subject: [PATCH 1/2] AddCallerSkip in RedirectStdLog (#320) Make entries logged through log.Print have correct caller. --- global.go | 5 +++++ global_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/global.go b/global.go index e069cd99d..ca51c9ef9 100644 --- a/global.go +++ b/global.go @@ -63,6 +63,11 @@ func RedirectStdLog(l *Logger) func() { prefix := log.Prefix() log.SetFlags(0) log.SetPrefix("") + if l.addCaller { + const stdLogDefaultDepth = 4 + const loggerWriterDepth = 1 + l = l.WithOptions(AddCallerSkip(stdLogDefaultDepth + loggerWriterDepth)) + } log.SetOutput(&loggerWriter{l}) return func() { log.SetFlags(flags) diff --git a/global_test.go b/global_test.go index e10ccb141..36eb99efd 100644 --- a/global_test.go +++ b/global_test.go @@ -25,6 +25,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.uber.org/zap/internal/observer" "go.uber.org/zap/zapcore" @@ -76,3 +77,14 @@ func TestRedirectStdLog(t *testing.T) { assert.Equal(t, initialFlags, log.Flags(), "Expected to reset initial flags.") assert.Equal(t, initialPrefix, log.Prefix(), "Expected to reset initial prefix.") } + +func TestRedirectStdLogCaller(t *testing.T) { + withLogger(t, DebugLevel, nil, func(l *Logger, logs *observer.ObservedLogs) { + l = l.WithOptions(AddCaller()) + defer RedirectStdLog(l)() + log.Print("redirected") + entries := logs.All() + require.Len(t, entries, 1) + assert.Contains(t, entries[0].Entry.Caller.File, "global_test.go") + }) +} From a62c9842a2a6a389778f59d561d580d437094075 Mon Sep 17 00:00:00 2001 From: Akshay Shah Date: Sun, 19 Feb 2017 10:02:38 -0800 Subject: [PATCH 2/2] Small cleanups Add some test assertion methods and restructure the caller skipping a bit. --- global.go | 13 +++++++------ global_test.go | 7 +++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/global.go b/global.go index ca51c9ef9..7a905289f 100644 --- a/global.go +++ b/global.go @@ -59,16 +59,17 @@ func ReplaceGlobals(logger *Logger) func() { // It returns a function to restore the original prefix and flags and reset the // standard library's output to os.Stdout. func RedirectStdLog(l *Logger) func() { + const ( + stdLogDefaultDepth = 4 + loggerWriterDepth = 1 + ) flags := log.Flags() prefix := log.Prefix() log.SetFlags(0) log.SetPrefix("") - if l.addCaller { - const stdLogDefaultDepth = 4 - const loggerWriterDepth = 1 - l = l.WithOptions(AddCallerSkip(stdLogDefaultDepth + loggerWriterDepth)) - } - log.SetOutput(&loggerWriter{l}) + log.SetOutput(&loggerWriter{l.WithOptions( + AddCallerSkip(stdLogDefaultDepth + loggerWriterDepth), + )}) return func() { log.SetFlags(flags) log.SetPrefix(prefix) diff --git a/global_test.go b/global_test.go index 36eb99efd..d6e0713bd 100644 --- a/global_test.go +++ b/global_test.go @@ -79,12 +79,11 @@ func TestRedirectStdLog(t *testing.T) { } func TestRedirectStdLogCaller(t *testing.T) { - withLogger(t, DebugLevel, nil, func(l *Logger, logs *observer.ObservedLogs) { - l = l.WithOptions(AddCaller()) + withLogger(t, DebugLevel, []Option{AddCaller()}, func(l *Logger, logs *observer.ObservedLogs) { defer RedirectStdLog(l)() log.Print("redirected") entries := logs.All() - require.Len(t, entries, 1) - assert.Contains(t, entries[0].Entry.Caller.File, "global_test.go") + require.Len(t, entries, 1, "Unexpected number of logs.") + assert.Contains(t, entries[0].Entry.Caller.File, "global_test.go", "Unexpected caller annotation.") }) }