Skip to content

Commit

Permalink
@timestamp doesnt get printed when specfied in message codec
Browse files Browse the repository at this point in the history
  • Loading branch information
vjsamuel committed Mar 28, 2017
1 parent c7017db commit ed79feb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libbeat/common/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ 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 {
ts, err := ParseTime(timespec)
if err != nil {
panic(err)
}

return ts
}
3 changes: 3 additions & 0 deletions libbeat/common/fmtstr/formatevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions libbeat/common/fmtstr/formatevents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ed79feb

Please sign in to comment.