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
|
|
|
|
|
2019-11-19 00:31:13 +00:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
2017-05-15 21:36:49 +00:00
|
|
|
|
|
|
|
type syslogEvent struct {
|
|
|
|
level string
|
|
|
|
msg string
|
|
|
|
}
|
|
|
|
type syslogTestWriter struct {
|
|
|
|
events []syslogEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *syslogTestWriter) Write(p []byte) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2019-11-04 19:39:22 +00:00
|
|
|
func (w *syslogTestWriter) Trace(m string) error {
|
|
|
|
w.events = append(w.events, syslogEvent{"Trace", m})
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-15 21:36:49 +00:00
|
|
|
func (w *syslogTestWriter) Debug(m string) error {
|
|
|
|
w.events = append(w.events, syslogEvent{"Debug", m})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (w *syslogTestWriter) Info(m string) error {
|
|
|
|
w.events = append(w.events, syslogEvent{"Info", m})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (w *syslogTestWriter) Warning(m string) error {
|
|
|
|
w.events = append(w.events, syslogEvent{"Warning", m})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (w *syslogTestWriter) Err(m string) error {
|
|
|
|
w.events = append(w.events, syslogEvent{"Err", m})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (w *syslogTestWriter) Emerg(m string) error {
|
|
|
|
w.events = append(w.events, syslogEvent{"Emerg", m})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (w *syslogTestWriter) Crit(m string) error {
|
|
|
|
w.events = append(w.events, syslogEvent{"Crit", m})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyslogWriter(t *testing.T) {
|
|
|
|
sw := &syslogTestWriter{}
|
|
|
|
log := New(SyslogLevelWriter(sw))
|
2019-11-04 19:39:22 +00:00
|
|
|
log.Trace().Msg("trace")
|
2017-05-15 21:36:49 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|