Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@timestamp doesnt get printed when specfied in message codec #3721

Merged
merged 1 commit into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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