2022-03-20 19:19:42 +00:00
|
|
|
package zlog_test
|
2018-03-28 18:49:41 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 20:12:29 +00:00
|
|
|
"bytes"
|
2018-11-05 10:15:13 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-03-28 18:49:41 +00:00
|
|
|
"os"
|
2018-04-19 20:12:29 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-11-05 10:15:13 +00:00
|
|
|
"time"
|
2018-03-28 18:49:41 +00:00
|
|
|
|
2022-11-03 15:18:09 +00:00
|
|
|
"tuxpa.in/a/zlog"
|
2018-03-28 18:49:41 +00:00
|
|
|
)
|
|
|
|
|
2018-11-05 10:15:13 +00:00
|
|
|
func ExampleConsoleWriter() {
|
2022-03-20 19:19:42 +00:00
|
|
|
log := zlog.New(zlog.ConsoleWriter{Out: os.Stdout, NoColor: true})
|
2018-03-28 18:49:41 +00:00
|
|
|
|
2018-11-05 10:15:13 +00:00
|
|
|
log.Info().Str("foo", "bar").Msg("Hello World")
|
|
|
|
// Output: <nil> INF Hello World foo=bar
|
2018-03-28 18:49:41 +00:00
|
|
|
}
|
2018-04-19 20:12:29 +00:00
|
|
|
|
2018-11-05 10:15:13 +00:00
|
|
|
func ExampleConsoleWriter_customFormatters() {
|
2022-03-20 19:19:42 +00:00
|
|
|
out := zlog.ConsoleWriter{Out: os.Stdout, NoColor: true}
|
2018-11-05 10:15:13 +00:00
|
|
|
out.FormatLevel = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%-6s|", i)) }
|
|
|
|
out.FormatFieldName = func(i interface{}) string { return fmt.Sprintf("%s:", i) }
|
|
|
|
out.FormatFieldValue = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%s", i)) }
|
2022-03-20 19:19:42 +00:00
|
|
|
log := zlog.New(out)
|
2018-11-05 10:15:13 +00:00
|
|
|
|
|
|
|
log.Info().Str("foo", "bar").Msg("Hello World")
|
|
|
|
// Output: <nil> INFO | Hello World foo:BAR
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleNewConsoleWriter() {
|
2022-03-20 19:19:42 +00:00
|
|
|
out := zlog.NewConsoleWriter()
|
2018-11-05 10:15:13 +00:00
|
|
|
out.NoColor = true // For testing purposes only
|
2022-03-20 19:19:42 +00:00
|
|
|
log := zlog.New(out)
|
2018-11-05 10:15:13 +00:00
|
|
|
|
|
|
|
log.Debug().Str("foo", "bar").Msg("Hello World")
|
|
|
|
// Output: <nil> DBG Hello World foo=bar
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleNewConsoleWriter_customFormatters() {
|
2022-03-20 19:19:42 +00:00
|
|
|
out := zlog.NewConsoleWriter(
|
|
|
|
func(w *zlog.ConsoleWriter) {
|
2018-11-05 10:15:13 +00:00
|
|
|
// Customize time format
|
|
|
|
w.TimeFormat = time.RFC822
|
|
|
|
// Customize level formatting
|
|
|
|
w.FormatLevel = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("[%-5s]", i)) }
|
|
|
|
},
|
|
|
|
)
|
|
|
|
out.NoColor = true // For testing purposes only
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
log := zlog.New(out)
|
2018-11-05 10:15:13 +00:00
|
|
|
|
|
|
|
log.Info().Str("foo", "bar").Msg("Hello World")
|
|
|
|
// Output: <nil> [INFO ] Hello World foo=bar
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsoleLogger(t *testing.T) {
|
|
|
|
t.Run("Numbers", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
log := zlog.New(zlog.ConsoleWriter{Out: buf, NoColor: true})
|
2018-11-05 10:15:13 +00:00
|
|
|
log.Info().
|
|
|
|
Float64("float", 1.23).
|
|
|
|
Uint64("small", 123).
|
|
|
|
Uint64("big", 1152921504606846976).
|
|
|
|
Msg("msg")
|
|
|
|
if got, want := strings.TrimSpace(buf.String()), "<nil> INF msg big=1152921504606846976 float=1.23 small=123"; got != want {
|
|
|
|
t.Errorf("\ngot:\n%s\nwant:\n%s", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsoleWriter(t *testing.T) {
|
|
|
|
t.Run("Default field formatter", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true, PartsOrder: []string{"foo"}}
|
2018-11-05 10:15:13 +00:00
|
|
|
|
2019-04-25 19:01:27 +00:00
|
|
|
_, err := w.Write([]byte(`{"foo": "DEFAULT"}`))
|
2018-11-05 10:15:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := "DEFAULT foo=DEFAULT\n"
|
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Write colorized", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: false}
|
2018-11-05 10:15:13 +00:00
|
|
|
|
2019-04-25 19:01:27 +00:00
|
|
|
_, err := w.Write([]byte(`{"level": "warn", "message": "Foobar"}`))
|
2018-11-05 10:15:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-02-07 15:45:02 +00:00
|
|
|
expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar\n"
|
2018-11-05 10:15:13 +00:00
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Write fields", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
2018-11-05 10:15:13 +00:00
|
|
|
|
2022-07-16 20:02:45 +00:00
|
|
|
ts := time.Unix(0, 0)
|
|
|
|
d := ts.UTC().Format(time.RFC3339)
|
2019-04-25 19:01:27 +00:00
|
|
|
_, err := w.Write([]byte(`{"time": "` + d + `", "level": "debug", "message": "Foobar", "foo": "bar"}`))
|
2018-11-05 10:15:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-07-16 20:02:45 +00:00
|
|
|
expectedOutput := ts.Format(time.Kitchen) + " DBG Foobar foo=bar\n"
|
2018-11-05 10:15:13 +00:00
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-04-25 19:01:27 +00:00
|
|
|
t.Run("Unix timestamp input format", func(t *testing.T) {
|
2022-03-20 19:19:42 +00:00
|
|
|
of := zlog.TimeFieldFormat
|
2019-04-25 19:01:27 +00:00
|
|
|
defer func() {
|
2022-03-20 19:19:42 +00:00
|
|
|
zlog.TimeFieldFormat = of
|
2019-04-25 19:01:27 +00:00
|
|
|
}()
|
2022-03-20 19:19:42 +00:00
|
|
|
zlog.TimeFieldFormat = zlog.TimeFormatUnix
|
2019-04-25 19:01:27 +00:00
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, TimeFormat: time.StampMilli, NoColor: true}
|
2019-04-25 19:44:49 +00:00
|
|
|
|
|
|
|
_, err := w.Write([]byte(`{"time": 1234, "level": "debug", "message": "Foobar", "foo": "bar"}`))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-07-16 20:02:45 +00:00
|
|
|
expectedOutput := time.Unix(1234, 0).Format(time.StampMilli) + " DBG Foobar foo=bar\n"
|
2019-04-25 19:44:49 +00:00
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Unix timestamp ms input format", func(t *testing.T) {
|
2022-03-20 19:19:42 +00:00
|
|
|
of := zlog.TimeFieldFormat
|
2019-04-25 19:44:49 +00:00
|
|
|
defer func() {
|
2022-03-20 19:19:42 +00:00
|
|
|
zlog.TimeFieldFormat = of
|
2019-04-25 19:44:49 +00:00
|
|
|
}()
|
2022-03-20 19:19:42 +00:00
|
|
|
zlog.TimeFieldFormat = zlog.TimeFormatUnixMs
|
2019-04-25 19:44:49 +00:00
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, TimeFormat: time.StampMilli, NoColor: true}
|
2019-04-25 19:01:27 +00:00
|
|
|
|
2019-04-25 19:44:49 +00:00
|
|
|
_, err := w.Write([]byte(`{"time": 1234567, "level": "debug", "message": "Foobar", "foo": "bar"}`))
|
2019-04-25 19:01:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-07-16 20:02:45 +00:00
|
|
|
expectedOutput := time.Unix(1234, 567000000).Format(time.StampMilli) + " DBG Foobar foo=bar\n"
|
2019-04-25 19:01:27 +00:00
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-12-20 17:34:21 +00:00
|
|
|
t.Run("Unix timestamp us input format", func(t *testing.T) {
|
2022-03-20 19:19:42 +00:00
|
|
|
of := zlog.TimeFieldFormat
|
2019-12-20 17:34:21 +00:00
|
|
|
defer func() {
|
2022-03-20 19:19:42 +00:00
|
|
|
zlog.TimeFieldFormat = of
|
2019-12-20 17:34:21 +00:00
|
|
|
}()
|
2022-03-20 19:19:42 +00:00
|
|
|
zlog.TimeFieldFormat = zlog.TimeFormatUnixMicro
|
2019-12-20 17:34:21 +00:00
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, TimeFormat: time.StampMicro, NoColor: true}
|
2019-12-20 17:34:21 +00:00
|
|
|
|
|
|
|
_, err := w.Write([]byte(`{"time": 1234567891, "level": "debug", "message": "Foobar", "foo": "bar"}`))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-07-16 20:02:45 +00:00
|
|
|
expectedOutput := time.Unix(1234, 567891000).Format(time.StampMicro) + " DBG Foobar foo=bar\n"
|
2019-12-20 17:34:21 +00:00
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-04-25 19:01:27 +00:00
|
|
|
t.Run("No message field", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
2019-04-25 19:01:27 +00:00
|
|
|
|
|
|
|
_, err := w.Write([]byte(`{"level": "debug", "foo": "bar"}`))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-02-19 15:39:18 +00:00
|
|
|
expectedOutput := "<nil> DBG foo=bar\n"
|
2019-04-25 19:01:27 +00:00
|
|
|
actualOutput := buf.String()
|
2019-04-25 19:12:24 +00:00
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("No level field", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
2019-04-25 19:12:24 +00:00
|
|
|
|
|
|
|
_, err := w.Write([]byte(`{"message": "Foobar", "foo": "bar"}`))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := "<nil> ??? Foobar foo=bar\n"
|
|
|
|
actualOutput := buf.String()
|
2019-04-25 19:01:27 +00:00
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-02-07 15:45:02 +00:00
|
|
|
t.Run("Write colorized fields", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: false}
|
2019-02-07 15:45:02 +00:00
|
|
|
|
2019-04-25 19:01:27 +00:00
|
|
|
_, err := w.Write([]byte(`{"level": "warn", "message": "Foobar", "foo": "bar"}`))
|
2019-02-07 15:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar \x1b[36mfoo=\x1b[0mbar\n"
|
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-11-05 10:15:13 +00:00
|
|
|
t.Run("Write error field", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
2018-11-05 10:15:13 +00:00
|
|
|
|
2022-07-16 20:02:45 +00:00
|
|
|
ts := time.Unix(0, 0)
|
|
|
|
d := ts.UTC().Format(time.RFC3339)
|
2019-04-25 19:01:27 +00:00
|
|
|
evt := `{"time": "` + d + `", "level": "error", "message": "Foobar", "aaa": "bbb", "error": "Error"}`
|
2018-11-05 10:15:13 +00:00
|
|
|
// t.Log(evt)
|
|
|
|
|
|
|
|
_, err := w.Write([]byte(evt))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-07-16 20:02:45 +00:00
|
|
|
expectedOutput := ts.Format(time.Kitchen) + " ERR Foobar error=Error aaa=bbb\n"
|
2018-11-05 10:15:13 +00:00
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Write caller field", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
2018-11-05 10:15:13 +00:00
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot get working directory: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-07-16 20:02:45 +00:00
|
|
|
ts := time.Unix(0, 0)
|
|
|
|
d := ts.UTC().Format(time.RFC3339)
|
2019-04-25 19:01:27 +00:00
|
|
|
evt := `{"time": "` + d + `", "level": "debug", "message": "Foobar", "foo": "bar", "caller": "` + cwd + `/foo/bar.go"}`
|
2018-11-05 10:15:13 +00:00
|
|
|
// t.Log(evt)
|
|
|
|
|
|
|
|
_, err = w.Write([]byte(evt))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-07-16 20:02:45 +00:00
|
|
|
expectedOutput := ts.Format(time.Kitchen) + " DBG foo/bar.go > Foobar foo=bar\n"
|
2018-11-05 10:15:13 +00:00
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Write JSON field", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
2018-11-05 10:15:13 +00:00
|
|
|
|
2019-04-25 19:01:27 +00:00
|
|
|
evt := `{"level": "debug", "message": "Foobar", "foo": [1, 2, 3], "bar": true}`
|
2018-11-05 10:15:13 +00:00
|
|
|
// t.Log(evt)
|
|
|
|
|
|
|
|
_, err := w.Write([]byte(evt))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := "<nil> DBG Foobar bar=true foo=[1,2,3]\n"
|
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsoleWriterConfiguration(t *testing.T) {
|
|
|
|
t.Run("Sets TimeFormat", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true, TimeFormat: time.RFC3339}
|
2018-11-05 10:15:13 +00:00
|
|
|
|
2022-07-16 20:02:45 +00:00
|
|
|
ts := time.Unix(0, 0)
|
|
|
|
d := ts.UTC().Format(time.RFC3339)
|
2019-04-25 19:01:27 +00:00
|
|
|
evt := `{"time": "` + d + `", "level": "info", "message": "Foobar"}`
|
2018-11-05 10:15:13 +00:00
|
|
|
|
|
|
|
_, err := w.Write([]byte(evt))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-07-16 20:02:45 +00:00
|
|
|
expectedOutput := ts.Format(time.RFC3339) + " INF Foobar\n"
|
2018-11-05 10:15:13 +00:00
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Sets PartsOrder", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true, PartsOrder: []string{"message", "level"}}
|
2018-11-05 10:15:13 +00:00
|
|
|
|
2019-04-25 19:01:27 +00:00
|
|
|
evt := `{"level": "info", "message": "Foobar"}`
|
2018-11-05 10:15:13 +00:00
|
|
|
_, err := w.Write([]byte(evt))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := "Foobar INF\n"
|
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
2020-11-29 09:13:56 +00:00
|
|
|
|
|
|
|
t.Run("Sets PartsExclude", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true, PartsExclude: []string{"time"}}
|
2020-11-29 09:13:56 +00:00
|
|
|
|
|
|
|
d := time.Unix(0, 0).UTC().Format(time.RFC3339)
|
|
|
|
evt := `{"time": "` + d + `", "level": "info", "message": "Foobar"}`
|
|
|
|
_, err := w.Write([]byte(evt))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := "INF Foobar\n"
|
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
2022-02-27 17:33:36 +00:00
|
|
|
|
|
|
|
t.Run("Sets FieldsExclude", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true, FieldsExclude: []string{"foo"}}
|
2022-02-27 17:33:36 +00:00
|
|
|
|
|
|
|
evt := `{"level": "info", "message": "Foobar", "foo":"bar", "baz":"quux"}`
|
|
|
|
_, err := w.Write([]byte(evt))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := "<nil> INF Foobar baz=quux\n"
|
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
2022-07-18 22:00:50 +00:00
|
|
|
|
|
|
|
t.Run("Sets FormatExtra", func(t *testing.T) {
|
|
|
|
buf := &bytes.Buffer{}
|
2022-12-28 09:41:00 +00:00
|
|
|
w := zlog.ConsoleWriter{
|
2022-07-18 22:00:50 +00:00
|
|
|
Out: buf, NoColor: true, PartsOrder: []string{"level", "message"},
|
|
|
|
FormatExtra: func(evt map[string]interface{}, buf *bytes.Buffer) error {
|
|
|
|
buf.WriteString("\nAdditional stacktrace")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
evt := `{"level": "info", "message": "Foobar"}`
|
|
|
|
_, err := w.Write([]byte(evt))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := "INF Foobar\nAdditional stacktrace\n"
|
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
2022-10-21 21:32:46 +00:00
|
|
|
|
|
|
|
t.Run("Uses local time for console writer without time zone", func(t *testing.T) {
|
|
|
|
// Regression test for issue #483 (check there for more details)
|
|
|
|
|
|
|
|
timeFormat := "2006-01-02 15:04:05"
|
|
|
|
expectedOutput := "2022-10-20 20:24:50 INF Foobar\n"
|
|
|
|
evt := `{"time": "2022-10-20 20:24:50", "level": "info", "message": "Foobar"}`
|
|
|
|
|
2022-12-28 09:41:00 +00:00
|
|
|
of := zlog.TimeFieldFormat
|
2022-10-21 21:32:46 +00:00
|
|
|
defer func() {
|
2022-12-28 09:41:00 +00:00
|
|
|
zlog.TimeFieldFormat = of
|
2022-10-21 21:32:46 +00:00
|
|
|
}()
|
2022-12-28 09:41:00 +00:00
|
|
|
zlog.TimeFieldFormat = timeFormat
|
2022-10-21 21:32:46 +00:00
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2022-12-28 09:41:00 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: buf, NoColor: true, TimeFormat: timeFormat}
|
2022-10-21 21:32:46 +00:00
|
|
|
_, err := w.Write([]byte(evt))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when writing output: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actualOutput := buf.String()
|
|
|
|
if actualOutput != expectedOutput {
|
|
|
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 10:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkConsoleWriter(b *testing.B) {
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
2019-04-25 19:01:27 +00:00
|
|
|
var msg = []byte(`{"level": "info", "foo": "bar", "message": "HELLO", "time": "1990-01-01"}`)
|
2018-11-05 10:15:13 +00:00
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
w := zlog.ConsoleWriter{Out: ioutil.Discard, NoColor: false}
|
2018-11-05 10:15:13 +00:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
w.Write(msg)
|
2018-04-19 20:12:29 +00:00
|
|
|
}
|
|
|
|
}
|