Codebase list golang-github-go-kit-kit / 23bfe49
Document concurrent use criteria & improve unit test. Chris Hines 8 years ago
2 changed file(s) with 8 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
1010 }
1111
1212 // NewLogfmtLogger returns a logger that encodes keyvals to the Writer in
13 // logfmt format.
13 // logfmt format. The passed Writer must be safe for concurrent use by
14 // multiple goroutines if the returned Logger will be used concurrently.
1415 func NewLogfmtLogger(w io.Writer) Logger {
1516 return &logfmtLogger{w}
1617 }
1718
1819 func (l logfmtLogger) Log(keyvals ...interface{}) error {
20 // The Logger interface requires implementations to be safe for concurrent
21 // use by multiple goroutines. For this implementation that means making
22 // only one call to l.w.Write() for each call to Log. We first collect all
23 // of the bytes into b, and then call l.w.Write(b).
1924 b, err := logfmt.MarshalKeyvals(keyvals...)
2025 if err != nil {
2126 return err
66 "testing"
77
88 "github.com/go-kit/kit/log"
9 "gopkg.in/logfmt.v0"
910 )
1011
1112 func TestLogfmtLogger(t *testing.T) {
3132 if err := logger.Log("std_map", map[int]int{1: 2}, "my_map", mymap{0: 0}); err != nil {
3233 t.Fatal(err)
3334 }
34 if want, have := "std_map=\"unsupported value type\" my_map=special_behavior\n", buf.String(); want != have {
35 if want, have := "std_map=\""+logfmt.ErrUnsupportedValueType.Error()+"\" my_map=special_behavior\n", buf.String(); want != have {
3536 t.Errorf("want %#v, have %#v", want, have)
3637 }
3738 }