Do not print large ints using scientific notation with ConsoleWriter

Fixes #55
This commit is contained in:
Olivier Poitrey 2018-04-19 13:12:29 -07:00
parent 711d95f5f1
commit d2b7a51951
2 changed files with 20 additions and 2 deletions

View File

@ -40,7 +40,9 @@ type ConsoleWriter struct {
func (w ConsoleWriter) Write(p []byte) (n int, err error) { func (w ConsoleWriter) Write(p []byte) (n int, err error) {
var event map[string]interface{} var event map[string]interface{}
p = decodeIfBinaryToBytes(p) p = decodeIfBinaryToBytes(p)
err = json.Unmarshal(p, &event) d := json.NewDecoder(bytes.NewReader(p))
d.UseNumber()
err = d.Decode(&event)
if err != nil { if err != nil {
return return
} }
@ -76,7 +78,7 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) {
} else { } else {
buf.WriteString(value) buf.WriteString(value)
} }
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: case json.Number:
fmt.Fprint(buf, value) fmt.Fprint(buf, value)
default: default:
b, err := json.Marshal(value) b, err := json.Marshal(value)

View File

@ -1,7 +1,10 @@
package zerolog_test package zerolog_test
import ( import (
"bytes"
"os" "os"
"strings"
"testing"
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
@ -12,3 +15,16 @@ func ExampleConsoleWriter_Write() {
log.Info().Msg("hello world") log.Info().Msg("hello world")
// Output: <nil> |INFO| hello world // Output: <nil> |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()), "<nil> |INFO| msg big=1152921504606846976 float=1.23 small=123"; got != want {
t.Errorf("\ngot:\n%s\nwant:\n%s", got, want)
}
}