OSDN Git Service

delete miner
[bytom/vapor.git] / vendor / github.com / go-kit / kit / log / stdlib_test.go
1 package log
2
3 import (
4         "bytes"
5         "fmt"
6         "log"
7         "testing"
8         "time"
9 )
10
11 func TestStdlibWriter(t *testing.T) {
12         buf := &bytes.Buffer{}
13         log.SetOutput(buf)
14         log.SetFlags(log.LstdFlags)
15         logger := NewLogfmtLogger(StdlibWriter{})
16         logger.Log("key", "val")
17         timestamp := time.Now().Format("2006/01/02 15:04:05")
18         if want, have := timestamp+" key=val\n", buf.String(); want != have {
19                 t.Errorf("want %q, have %q", want, have)
20         }
21 }
22
23 func TestStdlibAdapterUsage(t *testing.T) {
24         buf := &bytes.Buffer{}
25         logger := NewLogfmtLogger(buf)
26         writer := NewStdlibAdapter(logger)
27         stdlog := log.New(writer, "", 0)
28
29         now := time.Now()
30         date := now.Format("2006/01/02")
31         time := now.Format("15:04:05")
32
33         for flag, want := range map[int]string{
34                 0:                                      "msg=hello\n",
35                 log.Ldate:                              "ts=" + date + " msg=hello\n",
36                 log.Ltime:                              "ts=" + time + " msg=hello\n",
37                 log.Ldate | log.Ltime:                  "ts=\"" + date + " " + time + "\" msg=hello\n",
38                 log.Lshortfile:                         "caller=stdlib_test.go:44 msg=hello\n",
39                 log.Lshortfile | log.Ldate:             "ts=" + date + " caller=stdlib_test.go:44 msg=hello\n",
40                 log.Lshortfile | log.Ldate | log.Ltime: "ts=\"" + date + " " + time + "\" caller=stdlib_test.go:44 msg=hello\n",
41         } {
42                 buf.Reset()
43                 stdlog.SetFlags(flag)
44                 stdlog.Print("hello")
45                 if have := buf.String(); want != have {
46                         t.Errorf("flag=%d: want %#v, have %#v", flag, want, have)
47                 }
48         }
49 }
50
51 func TestStdLibAdapterExtraction(t *testing.T) {
52         buf := &bytes.Buffer{}
53         logger := NewLogfmtLogger(buf)
54         writer := NewStdlibAdapter(logger)
55         for input, want := range map[string]string{
56                 "hello":                                            "msg=hello\n",
57                 "2009/01/23: hello":                                "ts=2009/01/23 msg=hello\n",
58                 "2009/01/23 01:23:23: hello":                       "ts=\"2009/01/23 01:23:23\" msg=hello\n",
59                 "01:23:23: hello":                                  "ts=01:23:23 msg=hello\n",
60                 "2009/01/23 01:23:23.123123: hello":                "ts=\"2009/01/23 01:23:23.123123\" msg=hello\n",
61                 "2009/01/23 01:23:23.123123 /a/b/c/d.go:23: hello": "ts=\"2009/01/23 01:23:23.123123\" caller=/a/b/c/d.go:23 msg=hello\n",
62                 "01:23:23.123123 /a/b/c/d.go:23: hello":            "ts=01:23:23.123123 caller=/a/b/c/d.go:23 msg=hello\n",
63                 "2009/01/23 01:23:23 /a/b/c/d.go:23: hello":        "ts=\"2009/01/23 01:23:23\" caller=/a/b/c/d.go:23 msg=hello\n",
64                 "2009/01/23 /a/b/c/d.go:23: hello":                 "ts=2009/01/23 caller=/a/b/c/d.go:23 msg=hello\n",
65                 "/a/b/c/d.go:23: hello":                            "caller=/a/b/c/d.go:23 msg=hello\n",
66         } {
67                 buf.Reset()
68                 fmt.Fprint(writer, input)
69                 if have := buf.String(); want != have {
70                         t.Errorf("%q: want %#v, have %#v", input, want, have)
71                 }
72         }
73 }
74
75 func TestStdlibAdapterSubexps(t *testing.T) {
76         for input, wantMap := range map[string]map[string]string{
77                 "hello world": {
78                         "date": "",
79                         "time": "",
80                         "file": "",
81                         "msg":  "hello world",
82                 },
83                 "2009/01/23: hello world": {
84                         "date": "2009/01/23",
85                         "time": "",
86                         "file": "",
87                         "msg":  "hello world",
88                 },
89                 "2009/01/23 01:23:23: hello world": {
90                         "date": "2009/01/23",
91                         "time": "01:23:23",
92                         "file": "",
93                         "msg":  "hello world",
94                 },
95                 "01:23:23: hello world": {
96                         "date": "",
97                         "time": "01:23:23",
98                         "file": "",
99                         "msg":  "hello world",
100                 },
101                 "2009/01/23 01:23:23.123123: hello world": {
102                         "date": "2009/01/23",
103                         "time": "01:23:23.123123",
104                         "file": "",
105                         "msg":  "hello world",
106                 },
107                 "2009/01/23 01:23:23.123123 /a/b/c/d.go:23: hello world": {
108                         "date": "2009/01/23",
109                         "time": "01:23:23.123123",
110                         "file": "/a/b/c/d.go:23",
111                         "msg":  "hello world",
112                 },
113                 "01:23:23.123123 /a/b/c/d.go:23: hello world": {
114                         "date": "",
115                         "time": "01:23:23.123123",
116                         "file": "/a/b/c/d.go:23",
117                         "msg":  "hello world",
118                 },
119                 "2009/01/23 01:23:23 /a/b/c/d.go:23: hello world": {
120                         "date": "2009/01/23",
121                         "time": "01:23:23",
122                         "file": "/a/b/c/d.go:23",
123                         "msg":  "hello world",
124                 },
125                 "2009/01/23 /a/b/c/d.go:23: hello world": {
126                         "date": "2009/01/23",
127                         "time": "",
128                         "file": "/a/b/c/d.go:23",
129                         "msg":  "hello world",
130                 },
131                 "/a/b/c/d.go:23: hello world": {
132                         "date": "",
133                         "time": "",
134                         "file": "/a/b/c/d.go:23",
135                         "msg":  "hello world",
136                 },
137                 "2009/01/23 01:23:23.123123 C:/a/b/c/d.go:23: hello world": {
138                         "date": "2009/01/23",
139                         "time": "01:23:23.123123",
140                         "file": "C:/a/b/c/d.go:23",
141                         "msg":  "hello world",
142                 },
143                 "01:23:23.123123 C:/a/b/c/d.go:23: hello world": {
144                         "date": "",
145                         "time": "01:23:23.123123",
146                         "file": "C:/a/b/c/d.go:23",
147                         "msg":  "hello world",
148                 },
149                 "2009/01/23 01:23:23 C:/a/b/c/d.go:23: hello world": {
150                         "date": "2009/01/23",
151                         "time": "01:23:23",
152                         "file": "C:/a/b/c/d.go:23",
153                         "msg":  "hello world",
154                 },
155                 "2009/01/23 C:/a/b/c/d.go:23: hello world": {
156                         "date": "2009/01/23",
157                         "time": "",
158                         "file": "C:/a/b/c/d.go:23",
159                         "msg":  "hello world",
160                 },
161                 "C:/a/b/c/d.go:23: hello world": {
162                         "date": "",
163                         "time": "",
164                         "file": "C:/a/b/c/d.go:23",
165                         "msg":  "hello world",
166                 },
167                 "2009/01/23 01:23:23.123123 C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
168                         "date": "2009/01/23",
169                         "time": "01:23:23.123123",
170                         "file": "C:/a/b/c/d.go:23",
171                         "msg":  ":.;<>_#{[]}\"\\",
172                 },
173                 "01:23:23.123123 C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
174                         "date": "",
175                         "time": "01:23:23.123123",
176                         "file": "C:/a/b/c/d.go:23",
177                         "msg":  ":.;<>_#{[]}\"\\",
178                 },
179                 "2009/01/23 01:23:23 C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
180                         "date": "2009/01/23",
181                         "time": "01:23:23",
182                         "file": "C:/a/b/c/d.go:23",
183                         "msg":  ":.;<>_#{[]}\"\\",
184                 },
185                 "2009/01/23 C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
186                         "date": "2009/01/23",
187                         "time": "",
188                         "file": "C:/a/b/c/d.go:23",
189                         "msg":  ":.;<>_#{[]}\"\\",
190                 },
191                 "C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
192                         "date": "",
193                         "time": "",
194                         "file": "C:/a/b/c/d.go:23",
195                         "msg":  ":.;<>_#{[]}\"\\",
196                 },
197         } {
198                 haveMap := subexps([]byte(input))
199                 for key, want := range wantMap {
200                         if have := haveMap[key]; want != have {
201                                 t.Errorf("%q: %q: want %q, have %q", input, key, want, have)
202                         }
203                 }
204         }
205 }