From fb469685aa8f19d742eff3f56a0bd51a23265829 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Thu, 17 May 2018 16:09:39 -0700 Subject: [PATCH] Fix ConsoleWriter when zerolog is configured to use UNIX timestamp --- console.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/console.go b/console.go index 69e08f5..e9a0d84 100644 --- a/console.go +++ b/console.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" "sync" + "time" ) const ( @@ -57,7 +58,7 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) { level = strings.ToUpper(l)[0:4] } fmt.Fprintf(buf, "%s |%s| %s", - colorize(event[TimestampFieldName], cDarkGray, !w.NoColor), + colorize(formatTime(event[TimestampFieldName]), cDarkGray, !w.NoColor), colorize(level, lvlColor, !w.NoColor), colorize(event[MessageFieldName], cReset, !w.NoColor)) fields := make([]string, 0, len(event)) @@ -95,6 +96,17 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) { return } +func formatTime(t interface{}) string { + switch t := t.(type) { + case string: + return t + case json.Number: + u, _ := t.Int64() + return time.Unix(u, 0).Format(time.RFC3339) + } + return "" +} + func colorize(s interface{}, color int, enabled bool) string { if !enabled { return fmt.Sprintf("%v", s)