mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 16:01:55 +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) Named(_ string) mtglib.Logger { return n }
|
||||||
func (n noopLogger) BindInt(_ string, _ int) 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) BindStr(_, _ string) mtglib.Logger { return n }
|
||||||
|
func (n noopLogger) Printf(_ string, _ ...interface{}) {}
|
||||||
func (n noopLogger) Info(_ string) {}
|
func (n noopLogger) Info(_ string) {}
|
||||||
func (n noopLogger) Warning(_ string) {}
|
func (n noopLogger) Warning(_ string) {}
|
||||||
func (n noopLogger) Debug(_ string) {}
|
func (n noopLogger) Debug(_ string) {}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ func (suite *NoopLoggerTestSuite) TestLog() {
|
|||||||
suite.Empty(testlib.CaptureStderr(func() {
|
suite.Empty(testlib.CaptureStderr(func() {
|
||||||
log := logger.NewNoopLogger().Named("name")
|
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").Info("info")
|
||||||
log.BindInt("int", 1).BindStr("str", "1").Warning("info")
|
log.BindInt("int", 1).BindStr("str", "1").Warning("info")
|
||||||
log.BindInt("int", 1).BindStr("str", "1").Debug("info")
|
log.BindInt("int", 1).BindStr("str", "1").Debug("info")
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/v2/mtglib"
|
"github.com/9seconds/mtg/v2/mtglib"
|
||||||
"github.com/rs/zerolog"
|
"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) {
|
func (z *zeroLogContext) Info(msg string) {
|
||||||
z.InfoError(msg, nil)
|
z.InfoError(msg, nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ func (suite *ZeroLoggerTestSuite) TestLog() {
|
|||||||
testData := map[string]func(mtglib.Logger){
|
testData := map[string]func(mtglib.Logger){
|
||||||
"info": func(l mtglib.Logger) { l.Info("hello") },
|
"info": func(l mtglib.Logger) { l.Info("hello") },
|
||||||
"warn": func(l mtglib.Logger) { l.Warning("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") },
|
"debug": func(l mtglib.Logger) { l.Debug("hello") },
|
||||||
"info-error": func(l mtglib.Logger) { l.InfoError("hello", io.EOF) },
|
"info-error": func(l mtglib.Logger) { l.InfoError("hello", io.EOF) },
|
||||||
"warn-error": func(l mtglib.Logger) { l.WarningError("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)
|
timestamp := time.Unix(msg.Timestamp/1000, (msg.Timestamp%1000)*1_000_000)
|
||||||
assert.WithinDuration(t, time.Now(), timestamp, 100*time.Millisecond)
|
assert.WithinDuration(t, time.Now(), timestamp, 100*time.Millisecond)
|
||||||
|
|
||||||
|
if level == "printf" {
|
||||||
|
level = "debug"
|
||||||
|
}
|
||||||
|
|
||||||
assert.Equal(t, level, msg.Level)
|
assert.Equal(t, level, msg.Level)
|
||||||
assert.Equal(t, name, msg.StrParam)
|
assert.Equal(t, name, msg.StrParam)
|
||||||
assert.EqualValues(t, 1, msg.IntParam)
|
assert.EqualValues(t, 1, msg.IntParam)
|
||||||
assert.Equal(t, "name", msg.Logger)
|
assert.Equal(t, "name", msg.Logger)
|
||||||
assert.Equal(t, "hello", msg.Message)
|
assert.Equal(t, "hello", msg.Message)
|
||||||
|
|
||||||
if level != name {
|
if level != name && name != "printf" {
|
||||||
assert.Equal(t, io.EOF.Error(), msg.Error)
|
assert.Equal(t, io.EOF.Error(), msg.Error)
|
||||||
} else {
|
} else {
|
||||||
assert.Empty(t, msg.Error)
|
assert.Empty(t, msg.Error)
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ type Logger interface {
|
|||||||
BindInt(name string, value int) Logger
|
BindInt(name string, value int) Logger
|
||||||
BindStr(name, value string) Logger
|
BindStr(name, value string) Logger
|
||||||
|
|
||||||
|
Printf(format string, args ...interface{})
|
||||||
Info(msg string)
|
Info(msg string)
|
||||||
InfoError(msg string, err error)
|
InfoError(msg string, err error)
|
||||||
Warning(msg string)
|
Warning(msg string)
|
||||||
|
|||||||
Reference in New Issue
Block a user