|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+package logger_test
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "bytes"
|
|
|
5
|
+ "encoding/json"
|
|
|
6
|
+ "io"
|
|
|
7
|
+ "strings"
|
|
|
8
|
+ "testing"
|
|
|
9
|
+ "time"
|
|
|
10
|
+
|
|
|
11
|
+ "github.com/9seconds/mtg/v2/logger"
|
|
|
12
|
+ "github.com/9seconds/mtg/v2/mtglib"
|
|
|
13
|
+ "github.com/rs/zerolog"
|
|
|
14
|
+ "github.com/stretchr/testify/assert"
|
|
|
15
|
+ "github.com/stretchr/testify/suite"
|
|
|
16
|
+)
|
|
|
17
|
+
|
|
|
18
|
+type zeroLoggerLogMessage struct {
|
|
|
19
|
+ Timestamp int64 `json:"timestamp"`
|
|
|
20
|
+ Level string `json:"level"`
|
|
|
21
|
+ StrParam string `json:"strparam"`
|
|
|
22
|
+ IntParam int `json:"intparam"`
|
|
|
23
|
+ Logger string `json:"logger"`
|
|
|
24
|
+ Error string `json:"error"`
|
|
|
25
|
+ Message string `json:"message"`
|
|
|
26
|
+}
|
|
|
27
|
+
|
|
|
28
|
+type ZeroLoggerTestSuite struct {
|
|
|
29
|
+ suite.Suite
|
|
|
30
|
+}
|
|
|
31
|
+
|
|
|
32
|
+func (suite *ZeroLoggerTestSuite) SetupSuite() {
|
|
|
33
|
+ zerolog.SetGlobalLevel(zerolog.TraceLevel)
|
|
|
34
|
+
|
|
|
35
|
+ zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
|
|
|
36
|
+ zerolog.TimestampFieldName = "timestamp"
|
|
|
37
|
+ zerolog.LevelFieldName = "level"
|
|
|
38
|
+}
|
|
|
39
|
+
|
|
|
40
|
+func (suite *ZeroLoggerTestSuite) TestLog() {
|
|
|
41
|
+ testData := map[string]func(mtglib.Logger){
|
|
|
42
|
+ "info": func(l mtglib.Logger) { l.Info("hello") },
|
|
|
43
|
+ "warn": func(l mtglib.Logger) { l.Warning("hello") },
|
|
|
44
|
+ "debug": func(l mtglib.Logger) { l.Debug("hello") },
|
|
|
45
|
+ "info-error": func(l mtglib.Logger) { l.InfoError("hello", io.EOF) },
|
|
|
46
|
+ "warn-error": func(l mtglib.Logger) { l.WarningError("hello", io.EOF) },
|
|
|
47
|
+ "debug-error": func(l mtglib.Logger) { l.DebugError("hello", io.EOF) },
|
|
|
48
|
+ }
|
|
|
49
|
+
|
|
|
50
|
+ for k, v := range testData {
|
|
|
51
|
+ name := k
|
|
|
52
|
+ callback := v
|
|
|
53
|
+ level := strings.TrimSuffix(name, "-error")
|
|
|
54
|
+
|
|
|
55
|
+ suite.T().Run(name, func(t *testing.T) {
|
|
|
56
|
+ buf := &bytes.Buffer{}
|
|
|
57
|
+ log := logger.NewZeroLogger(zerolog.New(buf).With().Timestamp().Logger())
|
|
|
58
|
+
|
|
|
59
|
+ callback(log.Named("name").BindInt("intparam", 1).BindStr("strparam", name))
|
|
|
60
|
+
|
|
|
61
|
+ msg := &zeroLoggerLogMessage{}
|
|
|
62
|
+ assert.NoError(t, json.Unmarshal(buf.Bytes(), msg))
|
|
|
63
|
+
|
|
|
64
|
+ timestamp := time.Unix(msg.Timestamp/1000, (msg.Timestamp%1000)*1_000_000)
|
|
|
65
|
+ assert.WithinDuration(t, time.Now(), timestamp, 100*time.Millisecond)
|
|
|
66
|
+
|
|
|
67
|
+ assert.Equal(t, level, msg.Level)
|
|
|
68
|
+ assert.Equal(t, name, msg.StrParam)
|
|
|
69
|
+ assert.EqualValues(t, 1, msg.IntParam)
|
|
|
70
|
+ assert.Equal(t, "name", msg.Logger)
|
|
|
71
|
+ assert.Equal(t, "hello", msg.Message)
|
|
|
72
|
+
|
|
|
73
|
+ if level != name {
|
|
|
74
|
+ assert.Equal(t, io.EOF.Error(), msg.Error)
|
|
|
75
|
+ } else {
|
|
|
76
|
+ assert.Empty(t, msg.Error)
|
|
|
77
|
+ }
|
|
|
78
|
+ })
|
|
|
79
|
+ }
|
|
|
80
|
+}
|
|
|
81
|
+
|
|
|
82
|
+func (suite *ZeroLoggerTestSuite) TestIndependence() {
|
|
|
83
|
+ buf := &bytes.Buffer{}
|
|
|
84
|
+ log := logger.NewZeroLogger(zerolog.New(buf).With().Timestamp().Logger())
|
|
|
85
|
+
|
|
|
86
|
+ log1 := log.Named("1")
|
|
|
87
|
+ log2 := log.Named("2")
|
|
|
88
|
+ log12 := log1.Named("2")
|
|
|
89
|
+
|
|
|
90
|
+ log1.BindInt("param", 1).Info("hello")
|
|
|
91
|
+
|
|
|
92
|
+ log1Output := buf.String()
|
|
|
93
|
+
|
|
|
94
|
+ buf.Reset()
|
|
|
95
|
+
|
|
|
96
|
+ log2.BindInt("lalala", 2).Info("hello")
|
|
|
97
|
+
|
|
|
98
|
+ log2Output := buf.String()
|
|
|
99
|
+
|
|
|
100
|
+ buf.Reset()
|
|
|
101
|
+
|
|
|
102
|
+ log12.BindStr("tttt", "qqq").Info("hello")
|
|
|
103
|
+
|
|
|
104
|
+ log12Output := buf.String()
|
|
|
105
|
+
|
|
|
106
|
+ suite.NotContains("lalala", log1Output)
|
|
|
107
|
+ suite.NotContains("tttt", log1Output)
|
|
|
108
|
+ suite.NotContains("param", log2Output)
|
|
|
109
|
+ suite.NotContains("tttt", log1Output)
|
|
|
110
|
+ suite.NotContains("param", log12Output)
|
|
|
111
|
+ suite.NotContains("lalala", log12Output)
|
|
|
112
|
+}
|
|
|
113
|
+
|
|
|
114
|
+func TestZeroLogger(t *testing.T) { // nolint: paralleltest
|
|
|
115
|
+ suite.Run(t, &ZeroLoggerTestSuite{})
|
|
|
116
|
+}
|