2021-10-04 23:07:40 +00:00
|
|
|
//go:build !binary_log && !windows
|
|
|
|
// +build !binary_log,!windows
|
2017-11-27 18:01:32 +00:00
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
package zlog
|
2017-05-15 21:36:49 +00:00
|
|
|
|
|
|
|
import (
|
2021-10-04 23:07:40 +00:00
|
|
|
"bytes"
|
2021-01-20 16:03:52 +00:00
|
|
|
"errors"
|
2021-10-04 23:07:40 +00:00
|
|
|
"fmt"
|
2021-01-20 16:03:52 +00:00
|
|
|
"io"
|
2017-05-15 21:36:49 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMultiSyslogWriter(t *testing.T) {
|
|
|
|
sw := &syslogTestWriter{}
|
|
|
|
log := New(MultiLevelWriter(SyslogLevelWriter(sw)))
|
|
|
|
log.Debug().Msg("debug")
|
|
|
|
log.Info().Msg("info")
|
|
|
|
log.Warn().Msg("warn")
|
|
|
|
log.Error().Msg("error")
|
2017-12-01 17:52:37 +00:00
|
|
|
log.Log().Msg("nolevel")
|
2017-05-15 21:36:49 +00:00
|
|
|
want := []syslogEvent{
|
|
|
|
{"Debug", `{"level":"debug","message":"debug"}` + "\n"},
|
|
|
|
{"Info", `{"level":"info","message":"info"}` + "\n"},
|
2017-06-08 17:03:03 +00:00
|
|
|
{"Warning", `{"level":"warn","message":"warn"}` + "\n"},
|
2017-05-15 21:36:49 +00:00
|
|
|
{"Err", `{"level":"error","message":"error"}` + "\n"},
|
2017-12-01 17:52:37 +00:00
|
|
|
{"Info", `{"message":"nolevel"}` + "\n"},
|
2017-05-15 21:36:49 +00:00
|
|
|
}
|
|
|
|
if got := sw.events; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("Invalid syslog message routing: want %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
}
|
2021-01-20 16:03:52 +00:00
|
|
|
|
|
|
|
var writeCalls int
|
|
|
|
|
|
|
|
type mockedWriter struct {
|
|
|
|
wantErr bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c mockedWriter) Write(p []byte) (int, error) {
|
|
|
|
writeCalls++
|
|
|
|
|
|
|
|
if c.wantErr {
|
|
|
|
return -1, errors.New("Expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a new writer is only used if it actually works.
|
|
|
|
func TestResilientMultiWriter(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
writers []io.Writer
|
|
|
|
}{
|
|
|
|
{
|
2021-10-04 23:07:40 +00:00
|
|
|
name: "All valid writers",
|
2021-01-20 16:03:52 +00:00
|
|
|
writers: []io.Writer{
|
2021-10-04 23:07:40 +00:00
|
|
|
mockedWriter{
|
2021-01-20 16:03:52 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
2021-10-04 23:07:40 +00:00
|
|
|
mockedWriter{
|
2021-01-20 16:03:52 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-04 23:07:40 +00:00
|
|
|
name: "All invalid writers",
|
2021-01-20 16:03:52 +00:00
|
|
|
writers: []io.Writer{
|
2021-10-04 23:07:40 +00:00
|
|
|
mockedWriter{
|
2021-01-20 16:03:52 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2021-10-04 23:07:40 +00:00
|
|
|
mockedWriter{
|
2021-01-20 16:03:52 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-04 23:07:40 +00:00
|
|
|
name: "First invalid writer",
|
2021-01-20 16:03:52 +00:00
|
|
|
writers: []io.Writer{
|
2021-10-04 23:07:40 +00:00
|
|
|
mockedWriter{
|
2021-01-20 16:03:52 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2021-10-04 23:07:40 +00:00
|
|
|
mockedWriter{
|
2021-01-20 16:03:52 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-04 23:07:40 +00:00
|
|
|
name: "First valid writer",
|
2021-01-20 16:03:52 +00:00
|
|
|
writers: []io.Writer{
|
2021-10-04 23:07:40 +00:00
|
|
|
mockedWriter{
|
2021-01-20 16:03:52 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
2021-10-04 23:07:40 +00:00
|
|
|
mockedWriter{
|
2021-01-20 16:03:52 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
writers := tt.writers
|
|
|
|
multiWriter := MultiLevelWriter(writers...)
|
|
|
|
|
|
|
|
logger := New(multiWriter).With().Timestamp().Logger().Level(InfoLevel)
|
|
|
|
logger.Info().Msg("Test msg")
|
|
|
|
|
|
|
|
if len(writers) != writeCalls {
|
|
|
|
t.Errorf("Expected %d writers to have been called but only %d were.", len(writers), writeCalls)
|
|
|
|
}
|
|
|
|
writeCalls = 0
|
|
|
|
}
|
2021-10-04 23:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type testingLog struct {
|
|
|
|
testing.TB
|
|
|
|
buf bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testingLog) Log(args ...interface{}) {
|
|
|
|
if _, err := t.buf.WriteString(fmt.Sprint(args...)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testingLog) Logf(format string, args ...interface{}) {
|
|
|
|
if _, err := t.buf.WriteString(fmt.Sprintf(format, args...)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTestWriter(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
write []byte
|
|
|
|
want []byte
|
|
|
|
}{{
|
|
|
|
name: "newline",
|
|
|
|
write: []byte("newline\n"),
|
|
|
|
want: []byte("newline"),
|
|
|
|
}, {
|
|
|
|
name: "oneline",
|
|
|
|
write: []byte("oneline"),
|
|
|
|
want: []byte("oneline"),
|
|
|
|
}, {
|
|
|
|
name: "twoline",
|
|
|
|
write: []byte("twoline\n\n"),
|
|
|
|
want: []byte("twoline"),
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tb := &testingLog{TB: t} // Capture TB log buffer.
|
|
|
|
w := TestWriter{T: tb}
|
|
|
|
|
|
|
|
n, err := w.Write(tt.write)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if n != len(tt.write) {
|
|
|
|
t.Errorf("Expected %d write length but got %d", len(tt.write), n)
|
|
|
|
}
|
|
|
|
p := tb.buf.Bytes()
|
|
|
|
if !bytes.Equal(tt.want, p) {
|
|
|
|
t.Errorf("Expected %q, got %q.", tt.want, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
log := New(NewConsoleWriter(ConsoleTestWriter(t)))
|
|
|
|
log.Info().Str("name", tt.name).Msg("Success!")
|
|
|
|
|
|
|
|
tb.buf.Reset()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|