Remove syslog dep and write some more tests
This commit is contained in:
parent
d0a1bd7e3e
commit
ded037b89a
25
syslog.go
25
syslog.go
|
@ -2,17 +2,26 @@
|
|||
|
||||
package zerolog
|
||||
|
||||
import (
|
||||
"log/syslog"
|
||||
)
|
||||
import "io"
|
||||
|
||||
type syslogWriter struct {
|
||||
w *syslog.Writer
|
||||
// SyslogWriter is an interface matching a syslog.Writer struct.
|
||||
type SyslogWriter interface {
|
||||
io.Writer
|
||||
Debug(m string) error
|
||||
Info(m string) error
|
||||
Warning(m string) error
|
||||
Err(m string) error
|
||||
Emerg(m string) error
|
||||
Crit(m string) error
|
||||
}
|
||||
|
||||
// SyslogWriter wraps a syslog.Writer and set the right syslog level
|
||||
// matching the log even level.
|
||||
func SyslogWriter(w *syslog.Writer) LevelWriter {
|
||||
type syslogWriter struct {
|
||||
w SyslogWriter
|
||||
}
|
||||
|
||||
// SyslogLevelWriter wraps a SyslogWriter and call the right syslog level
|
||||
// method matching the zerolog level.
|
||||
func SyslogLevelWriter(w SyslogWriter) LevelWriter {
|
||||
return syslogWriter{w}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
package zerolog
|
||||
|
||||
import "testing"
|
||||
import "reflect"
|
||||
|
||||
type syslogEvent struct {
|
||||
level string
|
||||
msg string
|
||||
}
|
||||
type syslogTestWriter struct {
|
||||
events []syslogEvent
|
||||
}
|
||||
|
||||
func (w *syslogTestWriter) Write(p []byte) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
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))
|
||||
log.Debug().Msg("debug")
|
||||
log.Info().Msg("info")
|
||||
log.Warn().Msg("warn")
|
||||
log.Error().Msg("error")
|
||||
want := []syslogEvent{
|
||||
{"Debug", `{"level":"debug","message":"debug"}` + "\n"},
|
||||
{"Info", `{"level":"info","message":"info"}` + "\n"},
|
||||
{"Warning", `{"level":"warning","message":"warn"}` + "\n"},
|
||||
{"Err", `{"level":"error","message":"error"}` + "\n"},
|
||||
}
|
||||
if got := sw.events; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("Invalid syslog message routing: want %v, got %v", want, got)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package zerolog
|
||||
|
||||
import (
|
||||
"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")
|
||||
want := []syslogEvent{
|
||||
{"Debug", `{"level":"debug","message":"debug"}` + "\n"},
|
||||
{"Info", `{"level":"info","message":"info"}` + "\n"},
|
||||
{"Warning", `{"level":"warning","message":"warn"}` + "\n"},
|
||||
{"Err", `{"level":"error","message":"error"}` + "\n"},
|
||||
}
|
||||
if got := sw.events; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("Invalid syslog message routing: want %v, got %v", want, got)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue