Add Printf method to logger

This commit is contained in:
9seconds
2021-03-18 10:15:22 +03:00
parent 60d5b3cadd
commit b258581f47
5 changed files with 15 additions and 1 deletions
+1
View File
@@ -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) {}
+1
View File
@@ -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")
+6
View File
@@ -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)
}
+6 -1
View File
@@ -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)
+1
View File
@@ -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)