Fix ConsoleWriter when zerolog is configured to use UNIX timestamp

This commit is contained in:
Olivier Poitrey 2018-05-17 16:09:39 -07:00
parent a025d45231
commit fb469685aa
1 changed files with 13 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
) )
const ( const (
@ -57,7 +58,7 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) {
level = strings.ToUpper(l)[0:4] level = strings.ToUpper(l)[0:4]
} }
fmt.Fprintf(buf, "%s |%s| %s", 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(level, lvlColor, !w.NoColor),
colorize(event[MessageFieldName], cReset, !w.NoColor)) colorize(event[MessageFieldName], cReset, !w.NoColor))
fields := make([]string, 0, len(event)) fields := make([]string, 0, len(event))
@ -95,6 +96,17 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) {
return 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 { func colorize(s interface{}, color int, enabled bool) string {
if !enabled { if !enabled {
return fmt.Sprintf("%v", s) return fmt.Sprintf("%v", s)