From f61e48bb8e7cd84be2602880c97e0c5952827973 Mon Sep 17 00:00:00 2001 From: Jiang Xin Date: Tue, 22 Jan 2019 20:08:19 +0800 Subject: [PATCH] logger: fix wrong callback method Fix wrong callback in `logger.go`, and add test cases: 1. `logger.Warningln` should call `logger.Warnln`, not `logger.Warn`. 2. It's ok for `logger.Print` to call `entry.Info`, but calling `entry.Print` is better. Signed-off-by: Jiang Xin --- logger.go | 4 ++-- logger_test.go | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/logger.go b/logger.go index 9bf64e2..4b95ab1 100644 --- a/logger.go +++ b/logger.go @@ -200,7 +200,7 @@ func (logger *Logger) Info(args ...interface{}) { func (logger *Logger) Print(args ...interface{}) { entry := logger.newEntry() - entry.Info(args...) + entry.Print(args...) logger.releaseEntry(entry) } @@ -256,7 +256,7 @@ func (logger *Logger) Warnln(args ...interface{}) { } func (logger *Logger) Warningln(args ...interface{}) { - logger.Warn(args...) + logger.Warnln(args...) } func (logger *Logger) Errorln(args ...interface{}) { diff --git a/logger_test.go b/logger_test.go index 73ba450..50433e6 100644 --- a/logger_test.go +++ b/logger_test.go @@ -6,6 +6,7 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -40,3 +41,25 @@ func TestNoFieldValueError(t *testing.T) { _, ok := data[FieldKeyLogrusError] require.False(t, ok) } + +func TestWarninglnNotEqualToWarning(t *testing.T) { + buf := &bytes.Buffer{} + bufln := &bytes.Buffer{} + + formatter := new(TextFormatter) + formatter.DisableTimestamp = true + formatter.DisableLevelTruncation = true + + l := &Logger{ + Out: buf, + Formatter: formatter, + Hooks: make(LevelHooks), + Level: DebugLevel, + } + l.Warning("hello,", "world") + + l.SetOutput(bufln) + l.Warningln("hello,", "world") + + assert.NotEqual(t, buf.String(), bufln.String(), "Warning() and Wantingln() should not be equal") +}