From 69447564b8dedd3da5368e901b0afe9d76e2e9a3 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Mon, 6 Nov 2017 15:44:21 -0500 Subject: [PATCH] encode complex types as "%+v" (Refs #18) --- log/tmfmt_logger.go | 8 ++++++-- log/tmfmt_logger_test.go | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/log/tmfmt_logger.go b/log/tmfmt_logger.go index 2b464a6b0..d03979718 100644 --- a/log/tmfmt_logger.go +++ b/log/tmfmt_logger.go @@ -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 @@ -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 } } diff --git a/log/tmfmt_logger_test.go b/log/tmfmt_logger_test.go index 62eb32a03..a07b323c6 100644 --- a/log/tmfmt_logger_test.go +++ b/log/tmfmt_logger_test.go @@ -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 {