11 func TestStdlibWriter(t *testing.T) {
12 buf := &bytes.Buffer{}
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)
23 func TestStdlibAdapterUsage(t *testing.T) {
24 buf := &bytes.Buffer{}
25 logger := NewLogfmtLogger(buf)
26 writer := NewStdlibAdapter(logger)
27 stdlog := log.New(writer, "", 0)
30 date := now.Format("2006/01/02")
31 time := now.Format("15:04:05")
33 for flag, want := range map[int]string{
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",
45 if have := buf.String(); want != have {
46 t.Errorf("flag=%d: want %#v, have %#v", flag, want, have)
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",
68 fmt.Fprint(writer, input)
69 if have := buf.String(); want != have {
70 t.Errorf("%q: want %#v, have %#v", input, want, have)
75 func TestStdlibAdapterSubexps(t *testing.T) {
76 for input, wantMap := range map[string]map[string]string{
83 "2009/01/23: hello world": {
89 "2009/01/23 01:23:23: hello world": {
95 "01:23:23: hello world": {
101 "2009/01/23 01:23:23.123123: hello world": {
102 "date": "2009/01/23",
103 "time": "01:23:23.123123",
105 "msg": "hello world",
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",
113 "01:23:23.123123 /a/b/c/d.go:23: hello world": {
115 "time": "01:23:23.123123",
116 "file": "/a/b/c/d.go:23",
117 "msg": "hello world",
119 "2009/01/23 01:23:23 /a/b/c/d.go:23: hello world": {
120 "date": "2009/01/23",
122 "file": "/a/b/c/d.go:23",
123 "msg": "hello world",
125 "2009/01/23 /a/b/c/d.go:23: hello world": {
126 "date": "2009/01/23",
128 "file": "/a/b/c/d.go:23",
129 "msg": "hello world",
131 "/a/b/c/d.go:23: hello world": {
134 "file": "/a/b/c/d.go:23",
135 "msg": "hello world",
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",
143 "01:23:23.123123 C:/a/b/c/d.go:23: hello world": {
145 "time": "01:23:23.123123",
146 "file": "C:/a/b/c/d.go:23",
147 "msg": "hello world",
149 "2009/01/23 01:23:23 C:/a/b/c/d.go:23: hello world": {
150 "date": "2009/01/23",
152 "file": "C:/a/b/c/d.go:23",
153 "msg": "hello world",
155 "2009/01/23 C:/a/b/c/d.go:23: hello world": {
156 "date": "2009/01/23",
158 "file": "C:/a/b/c/d.go:23",
159 "msg": "hello world",
161 "C:/a/b/c/d.go:23: hello world": {
164 "file": "C:/a/b/c/d.go:23",
165 "msg": "hello world",
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": ":.;<>_#{[]}\"\\",
173 "01:23:23.123123 C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
175 "time": "01:23:23.123123",
176 "file": "C:/a/b/c/d.go:23",
177 "msg": ":.;<>_#{[]}\"\\",
179 "2009/01/23 01:23:23 C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
180 "date": "2009/01/23",
182 "file": "C:/a/b/c/d.go:23",
183 "msg": ":.;<>_#{[]}\"\\",
185 "2009/01/23 C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
186 "date": "2009/01/23",
188 "file": "C:/a/b/c/d.go:23",
189 "msg": ":.;<>_#{[]}\"\\",
191 "C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
194 "file": "C:/a/b/c/d.go:23",
195 "msg": ":.;<>_#{[]}\"\\",
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)