diff --git a/README.md b/README.md index 664c0d3..e9daca0 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,6 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/imperfectgo/zap-syslog)](https://goreportcard.com/report/github.com/imperfectgo/zap-syslog) [![Coverage](https://codecov.io/gh/imperfectgo/zap-syslog/branch/master/graph/badge.svg)](https://codecov.io/gh/imperfectgo/zap-syslog) -Syslog (RFC5424) encoder & syncer for [zap](https://github.com/uber-go/zap). +Syslog ([RFC5424]) encoder & syncer for [zap](https://github.com/uber-go/zap). + +[RFC5424]: https://tools.ietf.org/html/rfc5424 diff --git a/encoder.go b/encoder.go index 2446706..63a4de8 100644 --- a/encoder.go +++ b/encoder.go @@ -46,12 +46,9 @@ const ( var ( _ zapcore.Encoder = &syslogEncoder{} - _ jsonEncoder = zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()).(jsonEncoder) + _ = zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()).(jsonEncoder) ) -// Framing configures RFC6587 TCP transport framing. -type Framing int - // Framing. const ( NonTransparentFraming Framing = iota @@ -59,6 +56,9 @@ const ( DefaultFraming = NonTransparentFraming ) +// Framing configures RFC6587 TCP transport framing. +type Framing int + type jsonEncoder interface { zapcore.Encoder zapcore.ArrayEncoder @@ -92,7 +92,7 @@ func toRFC5424CompliantASCIIString(s string) string { return strings.Map(rfc5424CompliantASCIIMapper, s) } -// NewSyslogEncoder creates a syslogEncoder that you should not use because I'm not done with it yet. +// NewSyslogEncoder creates a syslogEncoder. func NewSyslogEncoder(cfg SyslogEncoderConfig) zapcore.Encoder { if cfg.Hostname == "" { hostname, _ := os.Hostname() @@ -243,12 +243,12 @@ func (enc *syslogEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) msg.AppendInt(version) // SP TIMESTAMP - ts := ent.Time.UTC().Format(timestampFormat) - if ts == "" { - ts = nilValue - } msg.AppendByte(' ') - msg.AppendString(ts) + if ent.Time.IsZero() { + msg.AppendString(nilValue) + } else { + msg.AppendString(ent.Time.Format(timestampFormat)) + } // SP HOSTNAME msg.AppendByte(' ') diff --git a/encoder_test.go b/encoder_test.go index 9053ac6..dbf215d 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -32,6 +32,7 @@ import ( "github.com/imperfectgo/zap-syslog/syslog" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.uber.org/multierr" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -66,9 +67,7 @@ func TestToRFC5424CompliantASCIIString(t *testing.T) { for _, f := range fixtures { actual := toRFC5424CompliantASCIIString(f.s) - if !assert.Equal(t, f.expected, actual) { - return - } + require.Equal(t, f.expected, actual) } } @@ -308,17 +307,13 @@ func assertOutput(t testing.TB, desc string, expected string, f func(zapcore.Enc }).(*syslogEncoder) f(enc) buf, err := enc.EncodeEntry(testEntry, nil) - if !assert.NoError(t, err) { - return - } + require.NoError(t, err) defer buf.Free() const bom = "\xef\xbb\xbf" output := buf.String() i := strings.Index(output, "\xef\xbb\xbf") - if !assert.Condition(t, func() (success bool) { return i > 0 }, "not a valid syslog output") { - return - } + require.Condition(t, func() (success bool) { return i > 0 }, "not a valid syslog output") jsonString := output[i+len(bom):] assert.Contains(t, jsonString, expected) @@ -417,7 +412,7 @@ func testSyslogEncoderFraming(t *testing.T, framing Framing) { } if !strings.HasPrefix(msg, msgPrefix) { - t.Errorf("Wrong syslog output for framing: %s", framing) + t.Errorf("Wrong syslog output for framing: %d", framing) t.Log(msg) t.Log(msgPrefix) return diff --git a/syncer.go b/syncer.go index 86fe589..df99a87 100644 --- a/syncer.go +++ b/syncer.go @@ -30,6 +30,7 @@ var ( _ zapcore.WriteSyncer = &ConnSyncer{} ) +// ConnSyncer describes connection sink for syslog. type ConnSyncer struct { network string raddr string @@ -83,6 +84,7 @@ func (s *ConnSyncer) Write(p []byte) (n int, err error) { return s.conn.Write(p) } +// Sync implements zapcore.WriteSyncer interface. func (s *ConnSyncer) Sync() error { return nil } diff --git a/syslog/syslog_test.go b/syslog/syslog_test.go index 709db33..0eb36b8 100644 --- a/syslog/syslog_test.go +++ b/syslog/syslog_test.go @@ -10,7 +10,7 @@ func TestFacilityPriority(t *testing.T) { } if actual != priority { - t.Fatalf("Expected returned priority: %s, actual: %s", facility, actual) + t.Fatalf("Expected returned priority: %d, actual: %d", priority, actual) } }