Skip to content

Commit

Permalink
Fix console writer when unix time stamp and/or no message field is used
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Apr 25, 2019
1 parent 3e85c4b commit 2a07580
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
11 changes: 10 additions & 1 deletion console.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,13 @@ func consoleDefaultFormatTimestamp(timeFormat string, noColor bool) Formatter {
t = ts.Format(timeFormat)
}
case json.Number:
t = tt.String()
i, err := tt.Int64()
if err != nil {
t = tt.String()
} else {
ts := time.Unix(i, 0)
t = ts.Format(timeFormat)
}
}
return colorize(t, colorDarkGray, noColor)
}
Expand Down Expand Up @@ -347,6 +353,9 @@ func consoleDefaultFormatCaller(noColor bool) Formatter {
}

func consoleDefaultFormatMessage(i interface{}) string {
if i == nil {
return ""
}
return fmt.Sprintf("%s", i)
}

Expand Down
58 changes: 48 additions & 10 deletions console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestConsoleWriter(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, PartsOrder: []string{"foo"}}

_, err := w.Write([]byte(`{"foo" : "DEFAULT"}`))
_, err := w.Write([]byte(`{"foo": "DEFAULT"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
Expand All @@ -92,7 +92,7 @@ func TestConsoleWriter(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: false}

_, err := w.Write([]byte(`{"level" : "warn", "message" : "Foobar"}`))
_, err := w.Write([]byte(`{"level": "warn", "message": "Foobar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
Expand All @@ -109,7 +109,7 @@ func TestConsoleWriter(t *testing.T) {
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}

d := time.Unix(0, 0).UTC().Format(time.RFC3339)
_, err := w.Write([]byte(`{"time" : "` + d + `", "level" : "debug", "message" : "Foobar", "foo" : "bar"}`))
_, err := w.Write([]byte(`{"time": "` + d + `", "level": "debug", "message": "Foobar", "foo": "bar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
Expand All @@ -121,11 +121,49 @@ func TestConsoleWriter(t *testing.T) {
}
})

t.Run("Unix timestamp input format", func(t *testing.T) {
of := zerolog.TimeFieldFormat
defer func() {
zerolog.TimeFieldFormat = of
}()
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix

buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}

_, err := w.Write([]byte(`{"time": 0, "level": "debug", "message": "Foobar", "foo": "bar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}

expectedOutput := "4:00PM DBG Foobar foo=bar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})

t.Run("No message field", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}

_, err := w.Write([]byte(`{"level": "debug", "foo": "bar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}

expectedOutput := "<nil> DBG foo=bar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})

t.Run("Write colorized fields", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: false}

_, err := w.Write([]byte(`{"level" : "warn", "message" : "Foobar", "foo": "bar"}`))
_, err := w.Write([]byte(`{"level": "warn", "message": "Foobar", "foo": "bar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
Expand All @@ -142,7 +180,7 @@ func TestConsoleWriter(t *testing.T) {
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}

d := time.Unix(0, 0).UTC().Format(time.RFC3339)
evt := `{"time" : "` + d + `", "level" : "error", "message" : "Foobar", "aaa" : "bbb", "error" : "Error"}`
evt := `{"time": "` + d + `", "level": "error", "message": "Foobar", "aaa": "bbb", "error": "Error"}`
// t.Log(evt)

_, err := w.Write([]byte(evt))
Expand All @@ -167,7 +205,7 @@ func TestConsoleWriter(t *testing.T) {
}

d := time.Unix(0, 0).UTC().Format(time.RFC3339)
evt := `{"time" : "` + d + `", "level" : "debug", "message" : "Foobar", "foo" : "bar", "caller" : "` + cwd + `/foo/bar.go"}`
evt := `{"time": "` + d + `", "level": "debug", "message": "Foobar", "foo": "bar", "caller": "` + cwd + `/foo/bar.go"}`
// t.Log(evt)

_, err = w.Write([]byte(evt))
Expand All @@ -186,7 +224,7 @@ func TestConsoleWriter(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}

evt := `{"level" : "debug", "message" : "Foobar", "foo" : [1, 2, 3], "bar" : true}`
evt := `{"level": "debug", "message": "Foobar", "foo": [1, 2, 3], "bar": true}`
// t.Log(evt)

_, err := w.Write([]byte(evt))
Expand All @@ -208,7 +246,7 @@ func TestConsoleWriterConfiguration(t *testing.T) {
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, TimeFormat: time.RFC3339}

d := time.Unix(0, 0).UTC().Format(time.RFC3339)
evt := `{"time" : "` + d + `", "level" : "info", "message" : "Foobar"}`
evt := `{"time": "` + d + `", "level": "info", "message": "Foobar"}`

_, err := w.Write([]byte(evt))
if err != nil {
Expand All @@ -226,7 +264,7 @@ func TestConsoleWriterConfiguration(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, PartsOrder: []string{"message", "level"}}

evt := `{"level" : "info", "message" : "Foobar"}`
evt := `{"level": "info", "message": "Foobar"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
Expand All @@ -244,7 +282,7 @@ func BenchmarkConsoleWriter(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()

var msg = []byte(`{"level" : "info", "foo" : "bar", "message" : "HELLO", "time" : "1990-01-01"}`)
var msg = []byte(`{"level": "info", "foo": "bar", "message": "HELLO", "time": "1990-01-01"}`)

w := zerolog.ConsoleWriter{Out: ioutil.Discard, NoColor: false}

Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
module github.com/rs/zerolog

go 1.12

require (
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e
github.com/pkg/errors v0.8.1
github.com/rs/xid v1.2.1
github.com/zenazn/goji v0.9.0
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc
)

0 comments on commit 2a07580

Please sign in to comment.