diff --git a/.travis.yml b/.travis.yml index 920ec3e..736e8b1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ go: - '1.8.x' - '1.9.x' - '1.10.x' -go_import_path: github.com/yumm007/zap-syslog +go_import_path: github.com/SGNL-ai/zap-syslog cache: directories: - vendor diff --git a/README.md b/README.md index e9daca0..74b4918 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # zap-syslog +SGNL zap syslog library + [![GoDoc](https://godoc.org/github.com/imperfectgo/zap-syslog?status.svg)](https://godoc.org/github.com/imperfectgo/zap-syslog) [![Build Status](https://travis-ci.org/imperfectgo/zap-syslog.svg?branch=master)](https://travis-ci.org/imperfectgo/zap-syslog) [![Go Report Card](https://goreportcard.com/badge/github.com/imperfectgo/zap-syslog)](https://goreportcard.com/report/github.com/imperfectgo/zap-syslog) diff --git a/encoder.go b/encoder.go index 77e03ad..1aaa5cb 100644 --- a/encoder.go +++ b/encoder.go @@ -21,14 +21,15 @@ package zapsyslog import ( - "github.com/yumm007/zap-syslog/internal" - "github.com/yumm007/zap-syslog/internal/bufferpool" - "github.com/yumm007/zap-syslog/syslog" "os" "path" "strings" "time" + "github.com/SGNL-ai/zap-syslog/internal" + "github.com/SGNL-ai/zap-syslog/internal/bufferpool" + "github.com/SGNL-ai/zap-syslog/syslog" + "go.uber.org/zap" "go.uber.org/zap/buffer" "go.uber.org/zap/zapcore" diff --git a/encoder_test.go b/encoder_test.go index ad45560..12782c4 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -22,419 +22,419 @@ package zapsyslog import ( - "encoding/json" - "errors" - "fmt" - "math" - "strings" - "testing" - "time" + "encoding/json" + "errors" + "fmt" + "math" + "strings" + "testing" + "time" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "github.com/yumm007/zap-syslog/syslog" - "go.uber.org/multierr" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" + "github.com/SGNL-ai/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" ) var ( - testEntry = zapcore.Entry{ - Time: time.Date(2017, 1, 2, 3, 4, 5, 123456789, time.UTC), - Message: "fake", - Level: zap.DebugLevel, - } + testEntry = zapcore.Entry{ + Time: time.Date(2017, 1, 2, 3, 4, 5, 123456789, time.UTC), + Message: "fake", + Level: zap.DebugLevel, + } ) func TestToRFC5424CompliantASCIIString(t *testing.T) { - fixtures := []struct { - s string - expected string - }{ - { - s: " abc ", - expected: "_abc_", - }, - { - s: "δΈ­ζ–‡", - expected: "__", - }, - { - s: "\x00\x01\x02\x03\x04test", - expected: "_____test", - }, - } + fixtures := []struct { + s string + expected string + }{ + { + s: " abc ", + expected: "_abc_", + }, + { + s: "δΈ­ζ–‡", + expected: "__", + }, + { + s: "\x00\x01\x02\x03\x04test", + expected: "_____test", + }, + } - for _, f := range fixtures { - actual := toRFC5424CompliantASCIIString(f.s) - require.Equal(t, f.expected, actual) - } + for _, f := range fixtures { + actual := toRFC5424CompliantASCIIString(f.s) + require.Equal(t, f.expected, actual) + } } func TestJSONEncoderObjectFields(t *testing.T) { - tests := []struct { - desc string - expected string - f func(zapcore.Encoder) - }{ - {"binary", `"k":"YWIxMg=="`, func(e zapcore.Encoder) { e.AddBinary("k", []byte("ab12")) }}, - {"bool", `"k\\":true`, func(e zapcore.Encoder) { e.AddBool(`k\`, true) }}, // test key escaping once - {"bool", `"k":true`, func(e zapcore.Encoder) { e.AddBool("k", true) }}, - {"bool", `"k":false`, func(e zapcore.Encoder) { e.AddBool("k", false) }}, - {"byteString", `"k":"v\\"`, func(e zapcore.Encoder) { e.AddByteString(`k`, []byte(`v\`)) }}, - {"byteString", `"k":"v"`, func(e zapcore.Encoder) { e.AddByteString("k", []byte("v")) }}, - {"byteString", `"k":""`, func(e zapcore.Encoder) { e.AddByteString("k", []byte{}) }}, - {"byteString", `"k":""`, func(e zapcore.Encoder) { e.AddByteString("k", nil) }}, - {"complex128", `"k":"1+2i"`, func(e zapcore.Encoder) { e.AddComplex128("k", 1+2i) }}, - {"complex64", `"k":"1+2i"`, func(e zapcore.Encoder) { e.AddComplex64("k", 1+2i) }}, - {"duration", `"k":0.000000001`, func(e zapcore.Encoder) { e.AddDuration("k", 1) }}, - {"float64", `"k":1`, func(e zapcore.Encoder) { e.AddFloat64("k", 1.0) }}, - {"float64", `"k":10000000000`, func(e zapcore.Encoder) { e.AddFloat64("k", 1e10) }}, - {"float64", `"k":"NaN"`, func(e zapcore.Encoder) { e.AddFloat64("k", math.NaN()) }}, - {"float64", `"k":"+Inf"`, func(e zapcore.Encoder) { e.AddFloat64("k", math.Inf(1)) }}, - {"float64", `"k":"-Inf"`, func(e zapcore.Encoder) { e.AddFloat64("k", math.Inf(-1)) }}, - {"float32", `"k":1`, func(e zapcore.Encoder) { e.AddFloat32("k", 1.0) }}, - {"float32", `"k":10000000000`, func(e zapcore.Encoder) { e.AddFloat32("k", 1e10) }}, - {"float32", `"k":"NaN"`, func(e zapcore.Encoder) { e.AddFloat32("k", float32(math.NaN())) }}, - {"float32", `"k":"+Inf"`, func(e zapcore.Encoder) { e.AddFloat32("k", float32(math.Inf(1))) }}, - {"float32", `"k":"-Inf"`, func(e zapcore.Encoder) { e.AddFloat32("k", float32(math.Inf(-1))) }}, - {"int", `"k":42`, func(e zapcore.Encoder) { e.AddInt("k", 42) }}, - {"int64", `"k":42`, func(e zapcore.Encoder) { e.AddInt64("k", 42) }}, - {"int32", `"k":42`, func(e zapcore.Encoder) { e.AddInt32("k", 42) }}, - {"int16", `"k":42`, func(e zapcore.Encoder) { e.AddInt16("k", 42) }}, - {"int8", `"k":42`, func(e zapcore.Encoder) { e.AddInt8("k", 42) }}, - {"string", `"k":"v\\"`, func(e zapcore.Encoder) { e.AddString(`k`, `v\`) }}, - {"string", `"k":"v"`, func(e zapcore.Encoder) { e.AddString("k", "v") }}, - {"string", `"k":""`, func(e zapcore.Encoder) { e.AddString("k", "") }}, - {"time", `"k":1`, func(e zapcore.Encoder) { e.AddTime("k", time.Unix(1, 0)) }}, - {"uint", `"k":42`, func(e zapcore.Encoder) { e.AddUint("k", 42) }}, - {"uint64", `"k":42`, func(e zapcore.Encoder) { e.AddUint64("k", 42) }}, - {"uint32", `"k":42`, func(e zapcore.Encoder) { e.AddUint32("k", 42) }}, - {"uint16", `"k":42`, func(e zapcore.Encoder) { e.AddUint16("k", 42) }}, - {"uint8", `"k":42`, func(e zapcore.Encoder) { e.AddUint8("k", 42) }}, - {"uintptr", `"k":42`, func(e zapcore.Encoder) { e.AddUintptr("k", 42) }}, - { - desc: "object (success)", - expected: `"k":{"loggable":"yes"}`, - f: func(e zapcore.Encoder) { - assert.NoError(t, e.AddObject("k", loggable{true}), "Unexpected error calling MarshalLogObject.") - }, - }, - { - desc: "object (error)", - expected: `"k":{}`, - f: func(e zapcore.Encoder) { - assert.Error(t, e.AddObject("k", loggable{false}), "Expected an error calling MarshalLogObject.") - }, - }, - { - desc: "object (with nested array)", - expected: `"turducken":{"ducks":[{"in":"chicken"},{"in":"chicken"}]}`, - f: func(e zapcore.Encoder) { - assert.NoError( - t, - e.AddObject("turducken", turducken{}), - "Unexpected error calling MarshalLogObject with nested ObjectMarshalers and ArrayMarshalers.", - ) - }, - }, - { - desc: "array (with nested object)", - expected: `"turduckens":[{"ducks":[{"in":"chicken"},{"in":"chicken"}]},{"ducks":[{"in":"chicken"},{"in":"chicken"}]}]`, - f: func(e zapcore.Encoder) { - assert.NoError( - t, - e.AddArray("turduckens", turduckens(2)), - "Unexpected error calling MarshalLogObject with nested ObjectMarshalers and ArrayMarshalers.", - ) - }, - }, - { - desc: "array (success)", - expected: `"k":[true]`, - f: func(e zapcore.Encoder) { - assert.NoError(t, e.AddArray(`k`, loggable{true}), "Unexpected error calling MarshalLogArray.") - }, - }, - { - desc: "array (error)", - expected: `"k":[]`, - f: func(e zapcore.Encoder) { - assert.Error(t, e.AddArray("k", loggable{false}), "Expected an error calling MarshalLogArray.") - }, - }, - { - desc: "reflect (success)", - expected: `"k":{"loggable":"yes"}`, - f: func(e zapcore.Encoder) { - assert.NoError(t, e.AddReflected("k", map[string]string{"loggable": "yes"}), "Unexpected error JSON-serializing a map.") - }, - }, - { - desc: "reflect (failure)", - expected: "", - f: func(e zapcore.Encoder) { - assert.Error(t, e.AddReflected("k", noJSON{}), "Unexpected success JSON-serializing a noJSON.") - }, - }, - { - desc: "namespace", - // EncodeEntry is responsible for closing all open namespaces. - expected: `"outermost":{"outer":{"foo":1,"inner":{"foo":2,"innermost":{`, - f: func(e zapcore.Encoder) { - e.OpenNamespace("outermost") - e.OpenNamespace("outer") - e.AddInt("foo", 1) - e.OpenNamespace("inner") - e.AddInt("foo", 2) - e.OpenNamespace("innermost") - }, - }, - } + tests := []struct { + desc string + expected string + f func(zapcore.Encoder) + }{ + {"binary", `"k":"YWIxMg=="`, func(e zapcore.Encoder) { e.AddBinary("k", []byte("ab12")) }}, + {"bool", `"k\\":true`, func(e zapcore.Encoder) { e.AddBool(`k\`, true) }}, // test key escaping once + {"bool", `"k":true`, func(e zapcore.Encoder) { e.AddBool("k", true) }}, + {"bool", `"k":false`, func(e zapcore.Encoder) { e.AddBool("k", false) }}, + {"byteString", `"k":"v\\"`, func(e zapcore.Encoder) { e.AddByteString(`k`, []byte(`v\`)) }}, + {"byteString", `"k":"v"`, func(e zapcore.Encoder) { e.AddByteString("k", []byte("v")) }}, + {"byteString", `"k":""`, func(e zapcore.Encoder) { e.AddByteString("k", []byte{}) }}, + {"byteString", `"k":""`, func(e zapcore.Encoder) { e.AddByteString("k", nil) }}, + {"complex128", `"k":"1+2i"`, func(e zapcore.Encoder) { e.AddComplex128("k", 1+2i) }}, + {"complex64", `"k":"1+2i"`, func(e zapcore.Encoder) { e.AddComplex64("k", 1+2i) }}, + {"duration", `"k":0.000000001`, func(e zapcore.Encoder) { e.AddDuration("k", 1) }}, + {"float64", `"k":1`, func(e zapcore.Encoder) { e.AddFloat64("k", 1.0) }}, + {"float64", `"k":10000000000`, func(e zapcore.Encoder) { e.AddFloat64("k", 1e10) }}, + {"float64", `"k":"NaN"`, func(e zapcore.Encoder) { e.AddFloat64("k", math.NaN()) }}, + {"float64", `"k":"+Inf"`, func(e zapcore.Encoder) { e.AddFloat64("k", math.Inf(1)) }}, + {"float64", `"k":"-Inf"`, func(e zapcore.Encoder) { e.AddFloat64("k", math.Inf(-1)) }}, + {"float32", `"k":1`, func(e zapcore.Encoder) { e.AddFloat32("k", 1.0) }}, + {"float32", `"k":10000000000`, func(e zapcore.Encoder) { e.AddFloat32("k", 1e10) }}, + {"float32", `"k":"NaN"`, func(e zapcore.Encoder) { e.AddFloat32("k", float32(math.NaN())) }}, + {"float32", `"k":"+Inf"`, func(e zapcore.Encoder) { e.AddFloat32("k", float32(math.Inf(1))) }}, + {"float32", `"k":"-Inf"`, func(e zapcore.Encoder) { e.AddFloat32("k", float32(math.Inf(-1))) }}, + {"int", `"k":42`, func(e zapcore.Encoder) { e.AddInt("k", 42) }}, + {"int64", `"k":42`, func(e zapcore.Encoder) { e.AddInt64("k", 42) }}, + {"int32", `"k":42`, func(e zapcore.Encoder) { e.AddInt32("k", 42) }}, + {"int16", `"k":42`, func(e zapcore.Encoder) { e.AddInt16("k", 42) }}, + {"int8", `"k":42`, func(e zapcore.Encoder) { e.AddInt8("k", 42) }}, + {"string", `"k":"v\\"`, func(e zapcore.Encoder) { e.AddString(`k`, `v\`) }}, + {"string", `"k":"v"`, func(e zapcore.Encoder) { e.AddString("k", "v") }}, + {"string", `"k":""`, func(e zapcore.Encoder) { e.AddString("k", "") }}, + {"time", `"k":1`, func(e zapcore.Encoder) { e.AddTime("k", time.Unix(1, 0)) }}, + {"uint", `"k":42`, func(e zapcore.Encoder) { e.AddUint("k", 42) }}, + {"uint64", `"k":42`, func(e zapcore.Encoder) { e.AddUint64("k", 42) }}, + {"uint32", `"k":42`, func(e zapcore.Encoder) { e.AddUint32("k", 42) }}, + {"uint16", `"k":42`, func(e zapcore.Encoder) { e.AddUint16("k", 42) }}, + {"uint8", `"k":42`, func(e zapcore.Encoder) { e.AddUint8("k", 42) }}, + {"uintptr", `"k":42`, func(e zapcore.Encoder) { e.AddUintptr("k", 42) }}, + { + desc: "object (success)", + expected: `"k":{"loggable":"yes"}`, + f: func(e zapcore.Encoder) { + assert.NoError(t, e.AddObject("k", loggable{true}), "Unexpected error calling MarshalLogObject.") + }, + }, + { + desc: "object (error)", + expected: `"k":{}`, + f: func(e zapcore.Encoder) { + assert.Error(t, e.AddObject("k", loggable{false}), "Expected an error calling MarshalLogObject.") + }, + }, + { + desc: "object (with nested array)", + expected: `"turducken":{"ducks":[{"in":"chicken"},{"in":"chicken"}]}`, + f: func(e zapcore.Encoder) { + assert.NoError( + t, + e.AddObject("turducken", turducken{}), + "Unexpected error calling MarshalLogObject with nested ObjectMarshalers and ArrayMarshalers.", + ) + }, + }, + { + desc: "array (with nested object)", + expected: `"turduckens":[{"ducks":[{"in":"chicken"},{"in":"chicken"}]},{"ducks":[{"in":"chicken"},{"in":"chicken"}]}]`, + f: func(e zapcore.Encoder) { + assert.NoError( + t, + e.AddArray("turduckens", turduckens(2)), + "Unexpected error calling MarshalLogObject with nested ObjectMarshalers and ArrayMarshalers.", + ) + }, + }, + { + desc: "array (success)", + expected: `"k":[true]`, + f: func(e zapcore.Encoder) { + assert.NoError(t, e.AddArray(`k`, loggable{true}), "Unexpected error calling MarshalLogArray.") + }, + }, + { + desc: "array (error)", + expected: `"k":[]`, + f: func(e zapcore.Encoder) { + assert.Error(t, e.AddArray("k", loggable{false}), "Expected an error calling MarshalLogArray.") + }, + }, + { + desc: "reflect (success)", + expected: `"k":{"loggable":"yes"}`, + f: func(e zapcore.Encoder) { + assert.NoError(t, e.AddReflected("k", map[string]string{"loggable": "yes"}), "Unexpected error JSON-serializing a map.") + }, + }, + { + desc: "reflect (failure)", + expected: "", + f: func(e zapcore.Encoder) { + assert.Error(t, e.AddReflected("k", noJSON{}), "Unexpected success JSON-serializing a noJSON.") + }, + }, + { + desc: "namespace", + // EncodeEntry is responsible for closing all open namespaces. + expected: `"outermost":{"outer":{"foo":1,"inner":{"foo":2,"innermost":{`, + f: func(e zapcore.Encoder) { + e.OpenNamespace("outermost") + e.OpenNamespace("outer") + e.AddInt("foo", 1) + e.OpenNamespace("inner") + e.AddInt("foo", 2) + e.OpenNamespace("innermost") + }, + }, + } - for _, tt := range tests { - assertOutput(t, tt.desc, tt.expected, tt.f) - } + for _, tt := range tests { + assertOutput(t, tt.desc, tt.expected, tt.f) + } } func TestJSONEncoderArrays(t *testing.T) { - tests := []struct { - desc string - expected string // expect f to be called twice - f func(zapcore.ArrayEncoder) - }{ - {"bool", `[true,true]`, func(e zapcore.ArrayEncoder) { e.AppendBool(true) }}, - {"byteString", `["k","k"]`, func(e zapcore.ArrayEncoder) { e.AppendByteString([]byte("k")) }}, - {"byteString", `["k\\","k\\"]`, func(e zapcore.ArrayEncoder) { e.AppendByteString([]byte(`k\`)) }}, - {"complex128", `["1+2i","1+2i"]`, func(e zapcore.ArrayEncoder) { e.AppendComplex128(1 + 2i) }}, - {"complex64", `["1+2i","1+2i"]`, func(e zapcore.ArrayEncoder) { e.AppendComplex64(1 + 2i) }}, - {"durations", `[0.000000002,0.000000002]`, func(e zapcore.ArrayEncoder) { e.AppendDuration(2) }}, - {"float64", `[3.14,3.14]`, func(e zapcore.ArrayEncoder) { e.AppendFloat64(3.14) }}, - {"float32", `[3.14,3.14]`, func(e zapcore.ArrayEncoder) { e.AppendFloat32(3.14) }}, - {"int", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendInt(42) }}, - {"int64", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendInt64(42) }}, - {"int32", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendInt32(42) }}, - {"int16", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendInt16(42) }}, - {"int8", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendInt8(42) }}, - {"string", `["k","k"]`, func(e zapcore.ArrayEncoder) { e.AppendString("k") }}, - {"string", `["k\\","k\\"]`, func(e zapcore.ArrayEncoder) { e.AppendString(`k\`) }}, - {"times", `[1,1]`, func(e zapcore.ArrayEncoder) { e.AppendTime(time.Unix(1, 0)) }}, - {"uint", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUint(42) }}, - {"uint64", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUint64(42) }}, - {"uint32", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUint32(42) }}, - {"uint16", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUint16(42) }}, - {"uint8", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUint8(42) }}, - {"uintptr", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUintptr(42) }}, - { - desc: "arrays (success)", - expected: `[[true],[true]]`, - f: func(arr zapcore.ArrayEncoder) { - assert.NoError(t, arr.AppendArray(zapcore.ArrayMarshalerFunc(func(inner zapcore.ArrayEncoder) error { - inner.AppendBool(true) - return nil - })), "Unexpected error appending an array.") - }, - }, - { - desc: "arrays (error)", - expected: `[[true],[true]]`, - f: func(arr zapcore.ArrayEncoder) { - assert.Error(t, arr.AppendArray(zapcore.ArrayMarshalerFunc(func(inner zapcore.ArrayEncoder) error { - inner.AppendBool(true) - return errors.New("fail") - })), "Expected an error appending an array.") - }, - }, - { - desc: "objects (success)", - expected: `[{"loggable":"yes"},{"loggable":"yes"}]`, - f: func(arr zapcore.ArrayEncoder) { - assert.NoError(t, arr.AppendObject(loggable{true}), "Unexpected error appending an object.") - }, - }, - { - desc: "objects (error)", - expected: `[{},{}]`, - f: func(arr zapcore.ArrayEncoder) { - assert.Error(t, arr.AppendObject(loggable{false}), "Expected an error appending an object.") - }, - }, - { - desc: "reflect (success)", - expected: `[{"foo":5},{"foo":5}]`, - f: func(arr zapcore.ArrayEncoder) { - assert.NoError( - t, - arr.AppendReflected(map[string]int{"foo": 5}), - "Unexpected an error appending an object with reflection.", - ) - }, - }, - { - desc: "reflect (error)", - expected: `[]`, - f: func(arr zapcore.ArrayEncoder) { - assert.Error( - t, - arr.AppendReflected(noJSON{}), - "Unexpected an error appending an object with reflection.", - ) - }, - }, - } + tests := []struct { + desc string + expected string // expect f to be called twice + f func(zapcore.ArrayEncoder) + }{ + {"bool", `[true,true]`, func(e zapcore.ArrayEncoder) { e.AppendBool(true) }}, + {"byteString", `["k","k"]`, func(e zapcore.ArrayEncoder) { e.AppendByteString([]byte("k")) }}, + {"byteString", `["k\\","k\\"]`, func(e zapcore.ArrayEncoder) { e.AppendByteString([]byte(`k\`)) }}, + {"complex128", `["1+2i","1+2i"]`, func(e zapcore.ArrayEncoder) { e.AppendComplex128(1 + 2i) }}, + {"complex64", `["1+2i","1+2i"]`, func(e zapcore.ArrayEncoder) { e.AppendComplex64(1 + 2i) }}, + {"durations", `[0.000000002,0.000000002]`, func(e zapcore.ArrayEncoder) { e.AppendDuration(2) }}, + {"float64", `[3.14,3.14]`, func(e zapcore.ArrayEncoder) { e.AppendFloat64(3.14) }}, + {"float32", `[3.14,3.14]`, func(e zapcore.ArrayEncoder) { e.AppendFloat32(3.14) }}, + {"int", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendInt(42) }}, + {"int64", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendInt64(42) }}, + {"int32", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendInt32(42) }}, + {"int16", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendInt16(42) }}, + {"int8", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendInt8(42) }}, + {"string", `["k","k"]`, func(e zapcore.ArrayEncoder) { e.AppendString("k") }}, + {"string", `["k\\","k\\"]`, func(e zapcore.ArrayEncoder) { e.AppendString(`k\`) }}, + {"times", `[1,1]`, func(e zapcore.ArrayEncoder) { e.AppendTime(time.Unix(1, 0)) }}, + {"uint", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUint(42) }}, + {"uint64", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUint64(42) }}, + {"uint32", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUint32(42) }}, + {"uint16", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUint16(42) }}, + {"uint8", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUint8(42) }}, + {"uintptr", `[42,42]`, func(e zapcore.ArrayEncoder) { e.AppendUintptr(42) }}, + { + desc: "arrays (success)", + expected: `[[true],[true]]`, + f: func(arr zapcore.ArrayEncoder) { + assert.NoError(t, arr.AppendArray(zapcore.ArrayMarshalerFunc(func(inner zapcore.ArrayEncoder) error { + inner.AppendBool(true) + return nil + })), "Unexpected error appending an array.") + }, + }, + { + desc: "arrays (error)", + expected: `[[true],[true]]`, + f: func(arr zapcore.ArrayEncoder) { + assert.Error(t, arr.AppendArray(zapcore.ArrayMarshalerFunc(func(inner zapcore.ArrayEncoder) error { + inner.AppendBool(true) + return errors.New("fail") + })), "Expected an error appending an array.") + }, + }, + { + desc: "objects (success)", + expected: `[{"loggable":"yes"},{"loggable":"yes"}]`, + f: func(arr zapcore.ArrayEncoder) { + assert.NoError(t, arr.AppendObject(loggable{true}), "Unexpected error appending an object.") + }, + }, + { + desc: "objects (error)", + expected: `[{},{}]`, + f: func(arr zapcore.ArrayEncoder) { + assert.Error(t, arr.AppendObject(loggable{false}), "Expected an error appending an object.") + }, + }, + { + desc: "reflect (success)", + expected: `[{"foo":5},{"foo":5}]`, + f: func(arr zapcore.ArrayEncoder) { + assert.NoError( + t, + arr.AppendReflected(map[string]int{"foo": 5}), + "Unexpected an error appending an object with reflection.", + ) + }, + }, + { + desc: "reflect (error)", + expected: `[]`, + f: func(arr zapcore.ArrayEncoder) { + assert.Error( + t, + arr.AppendReflected(noJSON{}), + "Unexpected an error appending an object with reflection.", + ) + }, + }, + } - for _, tt := range tests { - f := func(enc zapcore.Encoder) error { - return enc.AddArray("array", zapcore.ArrayMarshalerFunc(func(arr zapcore.ArrayEncoder) error { - tt.f(arr) - tt.f(arr) - return nil - })) - } - assertOutput(t, tt.desc, `"array":`+tt.expected, func(enc zapcore.Encoder) { - err := f(enc) - assert.NoError(t, err, "Unexpected error adding array to JSON zapcore.Encoder.") - }) - } + for _, tt := range tests { + f := func(enc zapcore.Encoder) error { + return enc.AddArray("array", zapcore.ArrayMarshalerFunc(func(arr zapcore.ArrayEncoder) error { + tt.f(arr) + tt.f(arr) + return nil + })) + } + assertOutput(t, tt.desc, `"array":`+tt.expected, func(enc zapcore.Encoder) { + err := f(enc) + assert.NoError(t, err, "Unexpected error adding array to JSON zapcore.Encoder.") + }) + } } func assertOutput(t testing.TB, desc string, expected string, f func(zapcore.Encoder)) { - enc := NewSyslogEncoder(SyslogEncoderConfig{ - EncoderConfig: zapcore.EncoderConfig{ - EncodeTime: zapcore.EpochTimeEncoder, - EncodeDuration: zapcore.SecondsDurationEncoder, - }, - }).(*syslogEncoder) - f(enc) - buf, err := enc.EncodeEntry(testEntry, nil) - require.NoError(t, err) - defer buf.Free() + enc := NewSyslogEncoder(SyslogEncoderConfig{ + EncoderConfig: zapcore.EncoderConfig{ + EncodeTime: zapcore.EpochTimeEncoder, + EncodeDuration: zapcore.SecondsDurationEncoder, + }, + }).(*syslogEncoder) + f(enc) + buf, err := enc.EncodeEntry(testEntry, nil) + require.NoError(t, err) + defer buf.Free() - const bom = "\xef\xbb\xbf" - output := buf.String() - i := strings.Index(output, "\xef\xbb\xbf") - require.Condition(t, func() (success bool) { return i > 0 }, "not a valid syslog output") + const bom = "\xef\xbb\xbf" + output := buf.String() + i := strings.Index(output, "\xef\xbb\xbf") + require.Condition(t, func() (success bool) { return i > 0 }, "not a valid syslog output") - jsonString := output[i+len(bom):] - assert.Contains(t, jsonString, expected) + jsonString := output[i+len(bom):] + assert.Contains(t, jsonString, expected) } // Nested Array- and ObjectMarshalers. type turducken struct{} func (t turducken) MarshalLogObject(enc zapcore.ObjectEncoder) error { - return enc.AddArray("ducks", zapcore.ArrayMarshalerFunc(func(arr zapcore.ArrayEncoder) error { - for i := 0; i < 2; i++ { - arr.AppendObject(zapcore.ObjectMarshalerFunc(func(inner zapcore.ObjectEncoder) error { - inner.AddString("in", "chicken") - return nil - })) - } - return nil - })) + return enc.AddArray("ducks", zapcore.ArrayMarshalerFunc(func(arr zapcore.ArrayEncoder) error { + for i := 0; i < 2; i++ { + arr.AppendObject(zapcore.ObjectMarshalerFunc(func(inner zapcore.ObjectEncoder) error { + inner.AddString("in", "chicken") + return nil + })) + } + return nil + })) } type turduckens int func (t turduckens) MarshalLogArray(enc zapcore.ArrayEncoder) error { - var err error - tur := turducken{} - for i := 0; i < int(t); i++ { - err = multierr.Append(err, enc.AppendObject(tur)) - } - return err + var err error + tur := turducken{} + for i := 0; i < int(t); i++ { + err = multierr.Append(err, enc.AppendObject(tur)) + } + return err } type loggable struct{ bool } func (l loggable) MarshalLogObject(enc zapcore.ObjectEncoder) error { - if !l.bool { - return errors.New("can't marshal") - } - enc.AddString("loggable", "yes") - return nil + if !l.bool { + return errors.New("can't marshal") + } + enc.AddString("loggable", "yes") + return nil } func (l loggable) MarshalLogArray(enc zapcore.ArrayEncoder) error { - if !l.bool { - return errors.New("can't marshal") - } - enc.AppendBool(true) - return nil + if !l.bool { + return errors.New("can't marshal") + } + enc.AppendBool(true) + return nil } type noJSON struct{} func (nj noJSON) MarshalJSON() ([]byte, error) { - return nil, errors.New("no") + return nil, errors.New("no") } func testEncoderConfig(framing Framing) SyslogEncoderConfig { - return SyslogEncoderConfig{ - EncoderConfig: zapcore.EncoderConfig{ - MessageKey: "msg", - NameKey: "name", - CallerKey: "caller", - StacktraceKey: "stacktrace", - EncodeTime: zapcore.EpochTimeEncoder, - EncodeLevel: zapcore.LowercaseLevelEncoder, - EncodeDuration: zapcore.SecondsDurationEncoder, - EncodeCaller: zapcore.ShortCallerEncoder, - }, + return SyslogEncoderConfig{ + EncoderConfig: zapcore.EncoderConfig{ + MessageKey: "msg", + NameKey: "name", + CallerKey: "caller", + StacktraceKey: "stacktrace", + EncodeTime: zapcore.EpochTimeEncoder, + EncodeLevel: zapcore.LowercaseLevelEncoder, + EncodeDuration: zapcore.SecondsDurationEncoder, + EncodeCaller: zapcore.ShortCallerEncoder, + }, - Framing: framing, - Hostname: "localhost", - App: "encoder_test", - PID: 9876, - Facility: syslog.LOG_LOCAL0, - } + Framing: framing, + Hostname: "localhost", + App: "encoder_test", + PID: 9876, + Facility: syslog.LOG_LOCAL0, + } } func testSyslogEncoderFraming(t *testing.T, framing Framing) { - enc := NewSyslogEncoder(testEncoderConfig(framing)) - enc.AddString("str", "foo") - enc.AddInt64("int64-1", 1) - enc.AddInt64("int64-2", 2) - enc.AddFloat64("float64", 1.0) - enc.AddString("string1", "\n") - enc.AddString("string2", "πŸ’©") - enc.AddString("string3", "πŸ€”") - enc.AddString("string4", "πŸ™Š") - enc.AddBool("bool", true) - buf, _ := enc.EncodeEntry(testEntry, nil) - defer buf.Free() + enc := NewSyslogEncoder(testEncoderConfig(framing)) + enc.AddString("str", "foo") + enc.AddInt64("int64-1", 1) + enc.AddInt64("int64-2", 2) + enc.AddFloat64("float64", 1.0) + enc.AddString("string1", "\n") + enc.AddString("string2", "πŸ’©") + enc.AddString("string3", "πŸ€”") + enc.AddString("string4", "πŸ™Š") + enc.AddBool("bool", true) + buf, _ := enc.EncodeEntry(testEntry, nil) + defer buf.Free() - msg := buf.String() - msgPrefix := "<135>1 2017-01-02T03:04:05.123456Z localhost encoder_test 9876 - - \xef\xbb\xbf" - if framing == OctetCountingFraming { - spacePos := strings.Index(msg, " ") + 1 - msgPrefix = fmt.Sprintf("%d %s", buf.Len()-spacePos, msgPrefix) - } + msg := buf.String() + msgPrefix := "<135>1 2017-01-02T03:04:05.123456Z localhost encoder_test 9876 - - \xef\xbb\xbf" + if framing == OctetCountingFraming { + spacePos := strings.Index(msg, " ") + 1 + msgPrefix = fmt.Sprintf("%d %s", buf.Len()-spacePos, msgPrefix) + } - if !strings.HasPrefix(msg, msgPrefix) { - t.Errorf("Wrong syslog output for framing: %d", framing) - t.Log(msg) - t.Log(msgPrefix) - return - } + if !strings.HasPrefix(msg, msgPrefix) { + t.Errorf("Wrong syslog output for framing: %d", framing) + t.Log(msg) + t.Log(msgPrefix) + return + } - if framing == OctetCountingFraming && strings.HasSuffix(msg, "\n") { - t.Errorf("syslog output for OctetCountingFraming should not ends with `\\n`") - return - } + if framing == OctetCountingFraming && strings.HasSuffix(msg, "\n") { + t.Errorf("syslog output for OctetCountingFraming should not ends with `\\n`") + return + } - jsonString := msg[len(msgPrefix):] - var m map[string]interface{} - err := json.Unmarshal([]byte(jsonString), &m) - if err != nil { - t.Errorf("message part of syslog output is not a valid json string: %s", err) - t.Logf("actual got: %s", jsonString) - return - } + jsonString := msg[len(msgPrefix):] + var m map[string]interface{} + err := json.Unmarshal([]byte(jsonString), &m) + if err != nil { + t.Errorf("message part of syslog output is not a valid json string: %s", err) + t.Logf("actual got: %s", jsonString) + return + } } func TestSyslogEncoder(t *testing.T) { - for _, framing := range []Framing{NonTransparentFraming, OctetCountingFraming} { - testSyslogEncoderFraming(t, framing) - } + for _, framing := range []Framing{NonTransparentFraming, OctetCountingFraming} { + testSyslogEncoderFraming(t, framing) + } } diff --git a/example/docker-compose.yml b/example/docker-compose.yml deleted file mode 100644 index 8adb8d9..0000000 --- a/example/docker-compose.yml +++ /dev/null @@ -1,40 +0,0 @@ -version: '2' -services: - # MongoDB: https://hub.docker.com/_/mongo/ - mongo: - image: mongo:3 - # Elasticsearch: https://www.elastic.co/guide/en/elasticsearch/reference/5.5/docker.html - elasticsearch: - image: docker.elastic.co/elasticsearch/elasticsearch:5.5.1 - environment: - - http.host=0.0.0.0 - # Disable X-Pack security: https://www.elastic.co/guide/en/elasticsearch/reference/5.5/security-settings.html#general-security-settings - - xpack.security.enabled=false - - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - ulimits: - memlock: - soft: -1 - hard: -1 - mem_limit: 1g - # Graylog: https://hub.docker.com/r/graylog/graylog/ - graylog: - image: graylog/graylog:2.3.0-1 - environment: - # CHANGE ME! - - GRAYLOG_PASSWORD_SECRET=somepasswordpepper - - GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918 - - GRAYLOG_WEB_ENDPOINT_URI=http://127.0.0.1:9000/api - depends_on: - - mongo - - elasticsearch - ports: - # Graylog web interface and REST API - - 9000:9000 - # Syslog TCP - - 514:514 - # Syslog UDP -# - 514:514/udp - # GELF TCP -# - 12201:12201 - # GELF UDP -# - 12201:12201/udp diff --git a/example/example.go b/example/example.go index 8f29421..b7efee3 100644 --- a/example/example.go +++ b/example/example.go @@ -3,10 +3,11 @@ package main import ( "crypto/rand" "encoding/hex" - "github.com/yumm007/zap-syslog" - "github.com/yumm007/zap-syslog/syslog" "os" + zapsyslog "github.com/SGNL-ai/zap-syslog" + "github.com/SGNL-ai/zap-syslog/syslog" + "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -30,7 +31,7 @@ func main() { App: "zapsyslog-test", }) - sink, err := zapsyslog.NewConnSyncer("tcp", "localhost:514") + sink, err := zapsyslog.NewConnSyncer("tcp", "localhost:514", true) if err != nil { panic(err) } diff --git a/go.mod b/go.mod index 53cc7bd..7a1d19b 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,17 @@ -module github.com/yumm007/zap-syslog +module github.com/SGNL-ai/zap-syslog -go 1.17 +go 1.18 require ( - github.com/stretchr/testify v1.7.0 - go.uber.org/multierr v1.7.0 - go.uber.org/zap v1.21.0 + github.com/stretchr/testify v1.8.1 + go.uber.org/atomic v1.10.0 + go.uber.org/multierr v1.8.0 + go.uber.org/zap v1.23.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - go.uber.org/atomic v1.7.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + github.com/stretchr/objx v0.5.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index c01e8af..4ad8f3c 100644 --- a/go.sum +++ b/go.sum @@ -1,61 +1,31 @@ github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= -github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= +go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= -go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= -go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= -go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec= -go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= -go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= -go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8= +go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= +go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY= +go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/syncer.go b/syncer.go index e460b70..c251219 100644 --- a/syncer.go +++ b/syncer.go @@ -21,10 +21,11 @@ package zapsyslog import ( - "go.uber.org/atomic" "net" "strings" + "go.uber.org/atomic" + "go.uber.org/zap/zapcore" ) @@ -79,7 +80,7 @@ func (s *ConnSyncer) close() { // connect makes a connection to the syslog server. func (s *ConnSyncer) connect() error { - if s.enabled.Load() == false { + if !s.enabled.Load() { return nil } s.close() @@ -106,7 +107,7 @@ func (s *ConnSyncer) connect() error { // Write writes to syslog with retry. func (s *ConnSyncer) Write(p []byte) (n int, err error) { - if s.enabled.Load() == false { + if !s.enabled.Load() { s.close() return n, nil } diff --git a/syncer_test.go b/syncer_test.go index b925a4d..022185b 100644 --- a/syncer_test.go +++ b/syncer_test.go @@ -23,7 +23,6 @@ package zapsyslog import ( "bufio" "io" - "io/ioutil" "log" "net" "os" @@ -100,7 +99,8 @@ func startServer(n, la string, done chan<- string) (addr string, sock io.Closer, // unix and unixgram: choose an address if none given if la == "" { // use ioutil.TempFile to get a name that is unique - f, err := ioutil.TempFile("", "syslogtest") + // f, err := ioutil.TempFile("", "syslogtest") + f, err := os.CreateTemp("", "syslogtest") if err != nil { log.Fatal("TempFile: ", err) } @@ -147,7 +147,7 @@ func TestWrite(t *testing.T) { addr, sock, srvWG := startServer("udp", "", done) defer srvWG.Wait() defer sock.Close() - l, err := NewConnSyncer("udp", addr) + l, err := NewConnSyncer("udp", addr, true) if err != nil { t.Fatalf("NewConnSyncer() failed: %v", err) } @@ -166,7 +166,7 @@ func TestConcurrentWrite(t *testing.T) { addr, sock, srvWG := startServer("udp", "", make(chan string, 1)) defer srvWG.Wait() defer sock.Close() - s, err := NewConnSyncer("udp", addr) + s, err := NewConnSyncer("udp", addr, true) if err != nil { t.Fatalf("NewConnSyncer() failed: %v", err) } @@ -215,7 +215,7 @@ func TestConcurrentReconnect(t *testing.T) { for i := 0; i < N; i++ { go func() { defer wg.Done() - s, err := NewConnSyncer("tcp", addr) + s, err := NewConnSyncer("tcp", addr, true) if err != nil { t.Errorf("NewConnSyncer() failed: %v", err) return @@ -252,7 +252,7 @@ func TestFailedToConnect(t *testing.T) { sock.Close() srvWG.Wait() - _, err := NewConnSyncer("tcp", addr) + _, err := NewConnSyncer("tcp", addr, true) if err == nil { t.Fatalf("NewConnSyncer() connect to an invalid address should returns error") } @@ -263,7 +263,7 @@ func TestSync(t *testing.T) { defer srvWG.Wait() defer sock.Close() // Should always be nil - s, err := NewConnSyncer("udp", addr) + s, err := NewConnSyncer("udp", addr, true) if err != nil { t.Fatalf("NewConnSyncer() failed: %v", err) }