Skip to content

Commit

Permalink
Fix ConsoleWriter when zerolog is configured to use UNIX timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed May 17, 2018
1 parent a025d45 commit fb46968
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion console.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"sync"
"time"
)

const (
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fb46968

Please sign in to comment.