From b258581f471a963704690979669cd60ff7d9b567 Mon Sep 17 00:00:00 2001 From: 9seconds Date: Thu, 18 Mar 2021 10:15:22 +0300 Subject: [PATCH] Add Printf method to logger --- logger/noop.go | 1 + logger/noop_test.go | 1 + logger/zerolog.go | 6 ++++++ logger/zerolog_test.go | 7 ++++++- mtglib/init.go | 1 + 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/logger/noop.go b/logger/noop.go index 6d7289c..d416542 100644 --- a/logger/noop.go +++ b/logger/noop.go @@ -7,6 +7,7 @@ type noopLogger struct{} func (n noopLogger) Named(_ string) mtglib.Logger { return n } func (n noopLogger) BindInt(_ string, _ int) mtglib.Logger { return n } func (n noopLogger) BindStr(_, _ string) mtglib.Logger { return n } +func (n noopLogger) Printf(_ string, _ ...interface{}) {} func (n noopLogger) Info(_ string) {} func (n noopLogger) Warning(_ string) {} func (n noopLogger) Debug(_ string) {} diff --git a/logger/noop_test.go b/logger/noop_test.go index add8464..ca05901 100644 --- a/logger/noop_test.go +++ b/logger/noop_test.go @@ -18,6 +18,7 @@ func (suite *NoopLoggerTestSuite) TestLog() { suite.Empty(testlib.CaptureStderr(func() { log := logger.NewNoopLogger().Named("name") + log.BindInt("int", 1).BindStr("str", "1").Printf("info", 1, 2) log.BindInt("int", 1).BindStr("str", "1").Info("info") log.BindInt("int", 1).BindStr("str", "1").Warning("info") log.BindInt("int", 1).BindStr("str", "1").Debug("info") diff --git a/logger/zerolog.go b/logger/zerolog.go index 4142453..d504afa 100644 --- a/logger/zerolog.go +++ b/logger/zerolog.go @@ -1,6 +1,8 @@ package logger import ( + "fmt" + "github.com/9seconds/mtg/v2/mtglib" "github.com/rs/zerolog" ) @@ -64,6 +66,10 @@ func (z *zeroLogContext) BindStr(name, value string) mtglib.Logger { } } +func (z *zeroLogContext) Printf(format string, args ...interface{}) { + z.Debug(fmt.Sprintf(format, args...)) +} + func (z *zeroLogContext) Info(msg string) { z.InfoError(msg, nil) } diff --git a/logger/zerolog_test.go b/logger/zerolog_test.go index 4b2308f..a3e9ae2 100644 --- a/logger/zerolog_test.go +++ b/logger/zerolog_test.go @@ -41,6 +41,7 @@ func (suite *ZeroLoggerTestSuite) TestLog() { testData := map[string]func(mtglib.Logger){ "info": func(l mtglib.Logger) { l.Info("hello") }, "warn": func(l mtglib.Logger) { l.Warning("hello") }, + "printf": func(l mtglib.Logger) { l.Printf("hello") }, "debug": func(l mtglib.Logger) { l.Debug("hello") }, "info-error": func(l mtglib.Logger) { l.InfoError("hello", io.EOF) }, "warn-error": func(l mtglib.Logger) { l.WarningError("hello", io.EOF) }, @@ -64,13 +65,17 @@ func (suite *ZeroLoggerTestSuite) TestLog() { timestamp := time.Unix(msg.Timestamp/1000, (msg.Timestamp%1000)*1_000_000) assert.WithinDuration(t, time.Now(), timestamp, 100*time.Millisecond) + if level == "printf" { + level = "debug" + } + assert.Equal(t, level, msg.Level) assert.Equal(t, name, msg.StrParam) assert.EqualValues(t, 1, msg.IntParam) assert.Equal(t, "name", msg.Logger) assert.Equal(t, "hello", msg.Message) - if level != name { + if level != name && name != "printf" { assert.Equal(t, io.EOF.Error(), msg.Error) } else { assert.Empty(t, msg.Error) diff --git a/mtglib/init.go b/mtglib/init.go index b3d53a3..3ea5e30 100644 --- a/mtglib/init.go +++ b/mtglib/init.go @@ -52,6 +52,7 @@ type Logger interface { BindInt(name string, value int) Logger BindStr(name, value string) Logger + Printf(format string, args ...interface{}) Info(msg string) InfoError(msg string, err error) Warning(msg string)