mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 14:54:01 +03:00
Add Printf method to logger
This commit is contained in:
@@ -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) {}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user