Skip to content
This repository has been archived by the owner on Jul 15, 2018. It is now read-only.

Commit

Permalink
Merge pull request #77 from tendermint/18-unsupported-value-type
Browse files Browse the repository at this point in the history
encode complex types as "%+v"
  • Loading branch information
melekes committed Nov 8, 2017
2 parents 7988483 + 6944756 commit 176c2ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions log/tmfmt_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type tmfmtLogger struct {
}

// NewTMFmtLogger returns a logger that encodes keyvals to the Writer in
// Tendermint custom format.
// Tendermint custom format. Note complex types (structs, maps, slices)
// formatted as "%+v".
//
// Each log event produces no more than one call to w.Write.
// The passed Writer must be safe for concurrent use by multiple goroutines if
Expand Down Expand Up @@ -103,7 +104,10 @@ KeyvalueLoop:
}
}

if err := enc.EncodeKeyval(keyvals[i], keyvals[i+1]); err != nil {
err := enc.EncodeKeyval(keyvals[i], keyvals[i+1])
if err == logfmt.ErrUnsupportedValueType {
enc.EncodeKeyval(keyvals[i], fmt.Sprintf("%+v", keyvals[i+1]))
} else if err != nil {
return err
}
}
Expand Down
6 changes: 4 additions & 2 deletions log/tmfmt_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ func TestTMFmtLogger(t *testing.T) {
assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ a=1 err=error\n$`), buf.String())

buf.Reset()
err := logger.Log("std_map", map[int]int{1: 2}, "my_map", mymap{0: 0})
assert.NotNil(t, err)
if err := logger.Log("std_map", map[int]int{1: 2}, "my_map", mymap{0: 0}); err != nil {
t.Fatal(err)
}
assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ std_map=map\[1:2\] my_map=special_behavior\n$`), buf.String())

buf.Reset()
if err := logger.Log("level", "error"); err != nil {
Expand Down

0 comments on commit 176c2ce

Please sign in to comment.