浏览代码

Add Printf method to logger

tags/v2.0.0-rc1
9seconds 5 年前
父节点
当前提交
b258581f47
共有 5 个文件被更改,包括 15 次插入1 次删除
  1. 1
    0
      logger/noop.go
  2. 1
    0
      logger/noop_test.go
  3. 6
    0
      logger/zerolog.go
  4. 6
    1
      logger/zerolog_test.go
  5. 1
    0
      mtglib/init.go

+ 1
- 0
logger/noop.go 查看文件

@@ -7,6 +7,7 @@ type noopLogger struct{}
7 7
 func (n noopLogger) Named(_ string) mtglib.Logger          { return n }
8 8
 func (n noopLogger) BindInt(_ string, _ int) mtglib.Logger { return n }
9 9
 func (n noopLogger) BindStr(_, _ string) mtglib.Logger     { return n }
10
+func (n noopLogger) Printf(_ string, _ ...interface{})     {}
10 11
 func (n noopLogger) Info(_ string)                         {}
11 12
 func (n noopLogger) Warning(_ string)                      {}
12 13
 func (n noopLogger) Debug(_ string)                        {}

+ 1
- 0
logger/noop_test.go 查看文件

@@ -18,6 +18,7 @@ func (suite *NoopLoggerTestSuite) TestLog() {
18 18
 		suite.Empty(testlib.CaptureStderr(func() {
19 19
 			log := logger.NewNoopLogger().Named("name")
20 20
 
21
+			log.BindInt("int", 1).BindStr("str", "1").Printf("info", 1, 2)
21 22
 			log.BindInt("int", 1).BindStr("str", "1").Info("info")
22 23
 			log.BindInt("int", 1).BindStr("str", "1").Warning("info")
23 24
 			log.BindInt("int", 1).BindStr("str", "1").Debug("info")

+ 6
- 0
logger/zerolog.go 查看文件

@@ -1,6 +1,8 @@
1 1
 package logger
2 2
 
3 3
 import (
4
+	"fmt"
5
+
4 6
 	"github.com/9seconds/mtg/v2/mtglib"
5 7
 	"github.com/rs/zerolog"
6 8
 )
@@ -64,6 +66,10 @@ func (z *zeroLogContext) BindStr(name, value string) mtglib.Logger {
64 66
 	}
65 67
 }
66 68
 
69
+func (z *zeroLogContext) Printf(format string, args ...interface{}) {
70
+	z.Debug(fmt.Sprintf(format, args...))
71
+}
72
+
67 73
 func (z *zeroLogContext) Info(msg string) {
68 74
 	z.InfoError(msg, nil)
69 75
 }

+ 6
- 1
logger/zerolog_test.go 查看文件

@@ -41,6 +41,7 @@ func (suite *ZeroLoggerTestSuite) TestLog() {
41 41
 	testData := map[string]func(mtglib.Logger){
42 42
 		"info":        func(l mtglib.Logger) { l.Info("hello") },
43 43
 		"warn":        func(l mtglib.Logger) { l.Warning("hello") },
44
+		"printf":      func(l mtglib.Logger) { l.Printf("hello") },
44 45
 		"debug":       func(l mtglib.Logger) { l.Debug("hello") },
45 46
 		"info-error":  func(l mtglib.Logger) { l.InfoError("hello", io.EOF) },
46 47
 		"warn-error":  func(l mtglib.Logger) { l.WarningError("hello", io.EOF) },
@@ -64,13 +65,17 @@ func (suite *ZeroLoggerTestSuite) TestLog() {
64 65
 			timestamp := time.Unix(msg.Timestamp/1000, (msg.Timestamp%1000)*1_000_000)
65 66
 			assert.WithinDuration(t, time.Now(), timestamp, 100*time.Millisecond)
66 67
 
68
+			if level == "printf" {
69
+				level = "debug"
70
+			}
71
+
67 72
 			assert.Equal(t, level, msg.Level)
68 73
 			assert.Equal(t, name, msg.StrParam)
69 74
 			assert.EqualValues(t, 1, msg.IntParam)
70 75
 			assert.Equal(t, "name", msg.Logger)
71 76
 			assert.Equal(t, "hello", msg.Message)
72 77
 
73
-			if level != name {
78
+			if level != name && name != "printf" {
74 79
 				assert.Equal(t, io.EOF.Error(), msg.Error)
75 80
 			} else {
76 81
 				assert.Empty(t, msg.Error)

+ 1
- 0
mtglib/init.go 查看文件

@@ -52,6 +52,7 @@ type Logger interface {
52 52
 	BindInt(name string, value int) Logger
53 53
 	BindStr(name, value string) Logger
54 54
 
55
+	Printf(format string, args ...interface{})
55 56
 	Info(msg string)
56 57
 	InfoError(msg string, err error)
57 58
 	Warning(msg string)

正在加载...
取消
保存