diff --git a/logfmt_handler.go b/logfmt_handler.go index 68c2acc7..f3a9647c 100644 --- a/logfmt_handler.go +++ b/logfmt_handler.go @@ -57,6 +57,9 @@ func (h *LogfmtHandler) UnmarshalLogfmt(data []byte) bool { val := dec.Value() if h.Time.IsZero() { foundTime := checkEachUntilFound(h.Opts.TimeFields, func(field string) bool { + if !bytes.Equal(key, []byte(field)) { + return false + } time, ok := tryParseTime(string(val)) if ok { h.Time = time diff --git a/time_parse.go b/time_parse.go index 3bbb4ff8..90ede7bb 100644 --- a/time_parse.go +++ b/time_parse.go @@ -1,6 +1,7 @@ package humanlog import ( + "strconv" "time" ) @@ -54,6 +55,11 @@ func tryParseTime(value interface{}) (time.Time, bool) { return t, true } } + // try to parse unix time number from string + floatVal, err := strconv.ParseFloat(v, 64) + if err == nil { + return parseTimeFloat64(floatVal), true + } case float32: return parseTimeFloat64(float64(v)), true case float64: diff --git a/time_parse_test.go b/time_parse_test.go index ac7ee495..53887970 100644 --- a/time_parse_test.go +++ b/time_parse_test.go @@ -1,7 +1,9 @@ package humanlog import ( + "fmt" "testing" + "time" ) func TestTimeParseFloat64(t *testing.T) { @@ -34,3 +36,41 @@ func TestTimeParseFloat64(t *testing.T) { } }) } + +func TestTryParseFloatTime(t *testing.T) { + testTime := time.Now() + + t.Run("microseconds", func(t *testing.T) { + actualTime, ok := tryParseTime(fmt.Sprintf("%d", testTime.UnixMicro())) + if !ok { + t.Fatal("time not parsed") + } + + if actualTime.UnixMicro() != testTime.UnixMicro() { + t.Fatalf("time not equal: %d != %d", actualTime.UnixMicro(), testTime.UnixMicro()) + } + }) + + t.Run("milliseconds", func(t *testing.T) { + actualTime, ok := tryParseTime(fmt.Sprintf("%d", testTime.UnixMilli())) + if !ok { + t.Fatal("time not parsed") + } + + if actualTime.UnixMilli() != testTime.UnixMilli() { + t.Fatalf("time not equal: %d != %d", actualTime.UnixMilli(), testTime.UnixMilli()) + } + }) + + t.Run("seconds", func(t *testing.T) { + actualTime, ok := tryParseTime(fmt.Sprintf("%d", testTime.Unix())) + if !ok { + t.Fatal("time not parsed") + } + + if actualTime.Unix() != testTime.Unix() { + t.Fatalf("time not equal: %d != %d", actualTime.Unix(), testTime.Unix()) + } + }) + +}