From ed79febec401eed741f5077dedef815a62c67be6 Mon Sep 17 00:00:00 2001 From: Vijay Samuel Date: Fri, 3 Mar 2017 10:48:29 -0800 Subject: [PATCH] @timestamp doesnt get printed when specfied in message codec --- libbeat/common/datetime.go | 5 +++++ libbeat/common/fmtstr/formatevents.go | 3 +++ libbeat/common/fmtstr/formatevents_test.go | 12 ++++++++++++ 3 files changed, 20 insertions(+) diff --git a/libbeat/common/datetime.go b/libbeat/common/datetime.go index b36d9170167..f464aa30304 100644 --- a/libbeat/common/datetime.go +++ b/libbeat/common/datetime.go @@ -43,6 +43,10 @@ func ParseTime(timespec string) (Time, error) { return Time(t), err } +func (t Time) String() string { + return time.Time(t).Format(TsLayout) +} + // MustParseTime is a convenience equivalent of the ParseTime function // that panics in case of errors. func MustParseTime(timespec string) Time { @@ -50,5 +54,6 @@ func MustParseTime(timespec string) Time { if err != nil { panic(err) } + return ts } diff --git a/libbeat/common/fmtstr/formatevents.go b/libbeat/common/fmtstr/formatevents.go index 494480d9136..06f2f58922d 100644 --- a/libbeat/common/fmtstr/formatevents.go +++ b/libbeat/common/fmtstr/formatevents.go @@ -408,6 +408,7 @@ func fieldString(event common.MapStr, field string) (string, error) { if err != nil { logp.Warn("Can not convert key '%v' value to string", v) } + return s, err } @@ -419,6 +420,8 @@ func tryConvString(v interface{}) (string, error) { switch s := v.(type) { case string: return s, nil + case common.Time: + return s.String(), nil case []byte: return string(s), nil case stringer: diff --git a/libbeat/common/fmtstr/formatevents_test.go b/libbeat/common/fmtstr/formatevents_test.go index 43e79a0c488..53eb11354d8 100644 --- a/libbeat/common/fmtstr/formatevents_test.go +++ b/libbeat/common/fmtstr/formatevents_test.go @@ -91,6 +91,18 @@ func TestEventFormatString(t *testing.T) { "timestamp: 2015.05.01", []string{"key"}, }, + { + "test timestamp formatter", + "%{[@timestamp]}: %{+YYYY.MM.dd}", + common.MapStr{ + "@timestamp": common.Time( + time.Date(2015, 5, 1, 20, 12, 34, 0, time.Local), + ), + "key": "timestamp", + }, + "2015-05-01T20:12:34.000Z: 2015.05.01", + []string{"@timestamp"}, + }, } for i, test := range tests {