ソースを参照

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
 func (n noopLogger) Named(_ string) mtglib.Logger          { return n }
7
 func (n noopLogger) Named(_ string) mtglib.Logger          { return n }
8
 func (n noopLogger) BindInt(_ string, _ int) mtglib.Logger { return n }
8
 func (n noopLogger) BindInt(_ string, _ int) mtglib.Logger { return n }
9
 func (n noopLogger) BindStr(_, _ string) mtglib.Logger     { return n }
9
 func (n noopLogger) BindStr(_, _ string) mtglib.Logger     { return n }
10
+func (n noopLogger) Printf(_ string, _ ...interface{})     {}
10
 func (n noopLogger) Info(_ string)                         {}
11
 func (n noopLogger) Info(_ string)                         {}
11
 func (n noopLogger) Warning(_ string)                      {}
12
 func (n noopLogger) Warning(_ string)                      {}
12
 func (n noopLogger) Debug(_ string)                        {}
13
 func (n noopLogger) Debug(_ string)                        {}

+ 1
- 0
logger/noop_test.go ファイルの表示

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

+ 6
- 0
logger/zerolog.go ファイルの表示

1
 package logger
1
 package logger
2
 
2
 
3
 import (
3
 import (
4
+	"fmt"
5
+
4
 	"github.com/9seconds/mtg/v2/mtglib"
6
 	"github.com/9seconds/mtg/v2/mtglib"
5
 	"github.com/rs/zerolog"
7
 	"github.com/rs/zerolog"
6
 )
8
 )
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
 func (z *zeroLogContext) Info(msg string) {
73
 func (z *zeroLogContext) Info(msg string) {
68
 	z.InfoError(msg, nil)
74
 	z.InfoError(msg, nil)
69
 }
75
 }

+ 6
- 1
logger/zerolog_test.go ファイルの表示

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

+ 1
- 0
mtglib/init.go ファイルの表示

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

読み込み中…
キャンセル
保存