From d2b7a51951c626ef9e7ca5ba8fe1cf1c4adefdba Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Thu, 19 Apr 2018 13:12:29 -0700 Subject: [PATCH] Do not print large ints using scientific notation with ConsoleWriter Fixes #55 --- console.go | 6 ++++-- console_test.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/console.go b/console.go index ac3e3cd..69e08f5 100644 --- a/console.go +++ b/console.go @@ -40,7 +40,9 @@ type ConsoleWriter struct { func (w ConsoleWriter) Write(p []byte) (n int, err error) { var event map[string]interface{} p = decodeIfBinaryToBytes(p) - err = json.Unmarshal(p, &event) + d := json.NewDecoder(bytes.NewReader(p)) + d.UseNumber() + err = d.Decode(&event) if err != nil { return } @@ -76,7 +78,7 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) { } else { buf.WriteString(value) } - case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: + case json.Number: fmt.Fprint(buf, value) default: b, err := json.Marshal(value) diff --git a/console_test.go b/console_test.go index a287f5b..a6e7e06 100644 --- a/console_test.go +++ b/console_test.go @@ -1,7 +1,10 @@ package zerolog_test import ( + "bytes" "os" + "strings" + "testing" "github.com/rs/zerolog" ) @@ -12,3 +15,16 @@ func ExampleConsoleWriter_Write() { log.Info().Msg("hello world") // Output: |INFO| hello world } + +func TestConsoleWriterNumbers(t *testing.T) { + buf := &bytes.Buffer{} + log := zerolog.New(zerolog.ConsoleWriter{Out: buf, NoColor: true}) + log.Info(). + Float64("float", 1.23). + Uint64("small", 123). + Uint64("big", 1152921504606846976). + Msg("msg") + if got, want := strings.TrimSpace(buf.String()), " |INFO| msg big=1152921504606846976 float=1.23 small=123"; got != want { + t.Errorf("\ngot:\n%s\nwant:\n%s", got, want) + } +}