2018-03-28 18:49:41 +00:00
|
|
|
// +build !binary_log
|
2017-11-27 18:01:32 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2017-05-15 21:36:49 +00:00
|
|
|
package zerolog
|
|
|
|
|
|
|
|
import (
|
2021-01-20 16:03:52 +00:00
|
|
|
"errors"
|
|
|
|
"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
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "All valid writers",
|
|
|
|
writers: []io.Writer{
|
|
|
|
mockedWriter {
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
mockedWriter {
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "All invalid writers",
|
|
|
|
writers: []io.Writer{
|
|
|
|
mockedWriter {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
mockedWriter {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "First invalid writer",
|
|
|
|
writers: []io.Writer{
|
|
|
|
mockedWriter {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
mockedWriter {
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "First valid writer",
|
|
|
|
writers: []io.Writer{
|
|
|
|
mockedWriter {
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
mockedWriter {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|