...

Source file src/github.com/go-kit/log/stdlib_test.go

Documentation: github.com/go-kit/log

     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 TestStdLibAdapterPrefixedExtraction(t *testing.T) {
    76  	buf := &bytes.Buffer{}
    77  	logger := NewLogfmtLogger(buf)
    78  	writer := NewStdlibAdapter(logger, Prefix("some prefix ", false))
    79  	for input, want := range map[string]string{
    80  		"some prefix hello":                                            "msg=hello\n",
    81  		"some prefix 2009/01/23: hello":                                "ts=2009/01/23 msg=hello\n",
    82  		"some prefix 2009/01/23 01:23:23: hello":                       "ts=\"2009/01/23 01:23:23\" msg=hello\n",
    83  		"some prefix 01:23:23: hello":                                  "ts=01:23:23 msg=hello\n",
    84  		"some prefix 2009/01/23 01:23:23.123123: hello":                "ts=\"2009/01/23 01:23:23.123123\" msg=hello\n",
    85  		"some prefix 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",
    86  		"some prefix 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",
    87  		"some prefix 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",
    88  		"some prefix 2009/01/23 /a/b/c/d.go:23: hello":                 "ts=2009/01/23 caller=/a/b/c/d.go:23 msg=hello\n",
    89  		"some prefix /a/b/c/d.go:23: hello":                            "caller=/a/b/c/d.go:23 msg=hello\n",
    90  		"/a/b/c/d.go:23: some prefix hello":                            "caller=/a/b/c/d.go:23 msg=hello\n",
    91  	} {
    92  		buf.Reset()
    93  		fmt.Fprint(writer, input)
    94  		if have := buf.String(); want != have {
    95  			t.Errorf("%q: want %#v, have %#v", input, want, have)
    96  		}
    97  	}
    98  }
    99  
   100  func TestStdLibAdapterPrefixedExtractionWithJoinToMessage(t *testing.T) {
   101  	buf := &bytes.Buffer{}
   102  	logger := NewLogfmtLogger(buf)
   103  	writer := NewStdlibAdapter(logger, Prefix("some prefix ", true))
   104  	for input, want := range map[string]string{
   105  		"some prefix hello":                                            "msg=\"some prefix hello\"\n",
   106  		"some prefix 2009/01/23: hello":                                "ts=2009/01/23 msg=\"some prefix hello\"\n",
   107  		"some prefix 2009/01/23 01:23:23: hello":                       "ts=\"2009/01/23 01:23:23\" msg=\"some prefix hello\"\n",
   108  		"some prefix 01:23:23: hello":                                  "ts=01:23:23 msg=\"some prefix hello\"\n",
   109  		"some prefix 2009/01/23 01:23:23.123123: hello":                "ts=\"2009/01/23 01:23:23.123123\" msg=\"some prefix hello\"\n",
   110  		"some prefix 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=\"some prefix hello\"\n",
   111  		"some prefix 01:23:23.123123 /a/b/c/d.go:23: hello":            "ts=01:23:23.123123 caller=/a/b/c/d.go:23 msg=\"some prefix hello\"\n",
   112  		"some prefix 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=\"some prefix hello\"\n",
   113  		"some prefix 2009/01/23 /a/b/c/d.go:23: hello":                 "ts=2009/01/23 caller=/a/b/c/d.go:23 msg=\"some prefix hello\"\n",
   114  		"some prefix /a/b/c/d.go:23: hello":                            "caller=/a/b/c/d.go:23 msg=\"some prefix hello\"\n",
   115  		"/a/b/c/d.go:23: some prefix hello":                            "caller=/a/b/c/d.go:23 msg=\"some prefix hello\"\n",
   116  	} {
   117  		buf.Reset()
   118  		fmt.Fprint(writer, input)
   119  		if have := buf.String(); want != have {
   120  			t.Errorf("%q: want %#v, have %#v", input, want, have)
   121  		}
   122  	}
   123  }
   124  
   125  func TestStdlibAdapterSubexps(t *testing.T) {
   126  	for input, wantMap := range map[string]map[string]string{
   127  		"hello world": {
   128  			"date": "",
   129  			"time": "",
   130  			"file": "",
   131  			"msg":  "hello world",
   132  		},
   133  		"hello\nworld": {
   134  			"date": "",
   135  			"time": "",
   136  			"file": "",
   137  			"msg":  "hello\nworld",
   138  		},
   139  		"2009/01/23: hello world": {
   140  			"date": "2009/01/23",
   141  			"time": "",
   142  			"file": "",
   143  			"msg":  "hello world",
   144  		},
   145  		"2009/01/23 01:23:23: hello world": {
   146  			"date": "2009/01/23",
   147  			"time": "01:23:23",
   148  			"file": "",
   149  			"msg":  "hello world",
   150  		},
   151  		"01:23:23: hello world": {
   152  			"date": "",
   153  			"time": "01:23:23",
   154  			"file": "",
   155  			"msg":  "hello world",
   156  		},
   157  		"2009/01/23 01:23:23.123123: hello world": {
   158  			"date": "2009/01/23",
   159  			"time": "01:23:23.123123",
   160  			"file": "",
   161  			"msg":  "hello world",
   162  		},
   163  		"2009/01/23 01:23:23.123123 /a/b/c/d.go:23: hello world": {
   164  			"date": "2009/01/23",
   165  			"time": "01:23:23.123123",
   166  			"file": "/a/b/c/d.go:23",
   167  			"msg":  "hello world",
   168  		},
   169  		"01:23:23.123123 /a/b/c/d.go:23: hello world": {
   170  			"date": "",
   171  			"time": "01:23:23.123123",
   172  			"file": "/a/b/c/d.go:23",
   173  			"msg":  "hello world",
   174  		},
   175  		"2009/01/23 01:23:23 /a/b/c/d.go:23: hello world": {
   176  			"date": "2009/01/23",
   177  			"time": "01:23:23",
   178  			"file": "/a/b/c/d.go:23",
   179  			"msg":  "hello world",
   180  		},
   181  		"2009/01/23 /a/b/c/d.go:23: hello world": {
   182  			"date": "2009/01/23",
   183  			"time": "",
   184  			"file": "/a/b/c/d.go:23",
   185  			"msg":  "hello world",
   186  		},
   187  		"/a/b/c/d.go:23: hello world": {
   188  			"date": "",
   189  			"time": "",
   190  			"file": "/a/b/c/d.go:23",
   191  			"msg":  "hello world",
   192  		},
   193  		"2009/01/23 01:23:23.123123 C:/a/b/c/d.go:23: hello world": {
   194  			"date": "2009/01/23",
   195  			"time": "01:23:23.123123",
   196  			"file": "C:/a/b/c/d.go:23",
   197  			"msg":  "hello world",
   198  		},
   199  		"01:23:23.123123 C:/a/b/c/d.go:23: hello world": {
   200  			"date": "",
   201  			"time": "01:23:23.123123",
   202  			"file": "C:/a/b/c/d.go:23",
   203  			"msg":  "hello world",
   204  		},
   205  		"2009/01/23 01:23:23 C:/a/b/c/d.go:23: hello world": {
   206  			"date": "2009/01/23",
   207  			"time": "01:23:23",
   208  			"file": "C:/a/b/c/d.go:23",
   209  			"msg":  "hello world",
   210  		},
   211  		"2009/01/23 C:/a/b/c/d.go:23: hello world": {
   212  			"date": "2009/01/23",
   213  			"time": "",
   214  			"file": "C:/a/b/c/d.go:23",
   215  			"msg":  "hello world",
   216  		},
   217  		"C:/a/b/c/d.go:23: hello world": {
   218  			"date": "",
   219  			"time": "",
   220  			"file": "C:/a/b/c/d.go:23",
   221  			"msg":  "hello world",
   222  		},
   223  		"2009/01/23 01:23:23.123123 C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
   224  			"date": "2009/01/23",
   225  			"time": "01:23:23.123123",
   226  			"file": "C:/a/b/c/d.go:23",
   227  			"msg":  ":.;<>_#{[]}\"\\",
   228  		},
   229  		"01:23:23.123123 C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
   230  			"date": "",
   231  			"time": "01:23:23.123123",
   232  			"file": "C:/a/b/c/d.go:23",
   233  			"msg":  ":.;<>_#{[]}\"\\",
   234  		},
   235  		"2009/01/23 01:23:23 C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
   236  			"date": "2009/01/23",
   237  			"time": "01:23:23",
   238  			"file": "C:/a/b/c/d.go:23",
   239  			"msg":  ":.;<>_#{[]}\"\\",
   240  		},
   241  		"2009/01/23 C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
   242  			"date": "2009/01/23",
   243  			"time": "",
   244  			"file": "C:/a/b/c/d.go:23",
   245  			"msg":  ":.;<>_#{[]}\"\\",
   246  		},
   247  		"C:/a/b/c/d.go:23: :.;<>_#{[]}\"\\": {
   248  			"date": "",
   249  			"time": "",
   250  			"file": "C:/a/b/c/d.go:23",
   251  			"msg":  ":.;<>_#{[]}\"\\",
   252  		},
   253  	} {
   254  		haveMap := subexps([]byte(input))
   255  		for key, want := range wantMap {
   256  			if have := haveMap[key]; want != have {
   257  				t.Errorf("%q: %q: want %q, have %q", input, key, want, have)
   258  			}
   259  		}
   260  	}
   261  }
   262  

View as plain text