diff --git a/.golangci.yml b/.golangci.yml index 496809d..a74f56c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,18 +9,25 @@ linters-settings: min-occurrences: 2 gocyclo: min-complexity: 15 - golint: - min-confidence: 0 govet: check-shadowing: true lll: line-length: 140 - maligned: + fieldalignment: suggest-new: true misspell: locale: US exhaustive: default-signifies-exhaustive: true + tagliatelle: + case: + use-field-name: true + rules: + json: snake + yaml: camel + xml: camel + bson: camel + avro: snake linters: enable-all: true @@ -31,3 +38,4 @@ issues: - path: _test\.go linters: - gomnd + - exhaustivestruct diff --git a/entry/entry.go b/entry/entry.go index da0e58c..40bc14b 100644 --- a/entry/entry.go +++ b/entry/entry.go @@ -13,9 +13,9 @@ const ( type Option func(*Entry) -func WithCapacity(cap int) Option { +func WithCapacity(c int) Option { return func(e *Entry) { - e.fields = make(field.Fields, 0, cap+1) + e.fields = make(field.Fields, 0, c+1) } } @@ -41,6 +41,7 @@ func New(opts ...Option) *Entry { e := &Entry{ fields: make(field.Fields, 0, defaultCap+1), level: level.Debug, + msg: "", } for _, opt := range opts { diff --git a/entry/pool.go b/entry/pool.go index 26e42eb..5229eac 100644 --- a/entry/pool.go +++ b/entry/pool.go @@ -9,6 +9,7 @@ var pool = sync.Pool{ }, } +//nolint: forcetypeassert func Get() *Entry { e := pool.Get().(*Entry) e.Reset() diff --git a/field/field.go b/field/field.go index fbbbfc3..ab9bdfb 100644 --- a/field/field.go +++ b/field/field.go @@ -255,7 +255,7 @@ type Field struct { value Value } -//nolint: gocyclo +//nolint: gocyclo,cyclop func (f Field) AddTo(enc Encoder) { key := string(f.key) diff --git a/field/key.go b/field/key.go index 1f4c281..731c4d3 100644 --- a/field/key.go +++ b/field/key.go @@ -6,7 +6,7 @@ import ( type Key string -//nolint: gocyclo, funlen +//nolint: gocyclo,funlen,cyclop func (k Key) Any(value interface{}) Field { switch v := value.(type) { case string: @@ -120,8 +120,10 @@ func (k Key) Any(value interface{}) Field { return Field{ key: k, value: Value{ - value: value, - vtype: TypeAny, + value: value, + vtype: TypeAny, + numeric: 0, + stringly: "", }, } } diff --git a/field/value.go b/field/value.go index fc1cf8e..73cd951 100644 --- a/field/value.go +++ b/field/value.go @@ -16,10 +16,15 @@ type Value struct { } func (v Value) MarshalJSON() ([]byte, error) { - return json.Marshal(v.AsInterface()) + b, err := json.Marshal(v.AsInterface()) + if err != nil { + return nil, fmt.Errorf("marshal err: %w", err) + } + + return b, nil } -//nolint: gocyclo +//nolint: gocyclo,gomnd,cyclop func (v Value) String() string { switch { case v.vtype.IsArray(), v.vtype.IsAny(): @@ -57,7 +62,7 @@ func (v Value) String() string { return fmt.Sprintf("%+v", v.AsInterface()) } -//nolint: gocyclo +//nolint: gocyclo,cyclop func (v Value) AsInterface() interface{} { switch { case v.vtype.IsArray(): @@ -288,20 +293,29 @@ func (v Value) asError() error { } func nilValue(t Type) Value { - return Value{vtype: t | TypeNil} + return Value{ + vtype: t | TypeNil, + value: nil, + numeric: 0, + stringly: "", + } } func stringValue(v string) Value { return Value{ stringly: v, vtype: TypeString, + numeric: 0, + value: nil, } } func stringsValue(v []string) Value { return Value{ - value: v, - vtype: TypeString | TypeArray, + value: v, + vtype: TypeString | TypeArray, + numeric: 0, + stringly: "", } } @@ -316,20 +330,27 @@ func stringpValue(v *string) Value { func boolValue(b bool) Value { if b { return Value{ - numeric: 1, - vtype: TypeBool, + numeric: 1, + vtype: TypeBool, + value: nil, + stringly: "", } } return Value{ - vtype: TypeBool, + vtype: TypeBool, + value: nil, + numeric: 0, + stringly: "", } } func boolsValue(b []bool) Value { return Value{ - value: b, - vtype: TypeBool | TypeArray, + value: b, + vtype: TypeBool | TypeArray, + numeric: 0, + stringly: "", } } @@ -343,15 +364,19 @@ func boolpValue(b *bool) Value { func intValue(i int) Value { return Value{ - vtype: TypeInt, - numeric: uint64(i), + vtype: TypeInt, + numeric: uint64(i), + value: nil, + stringly: "", } } func intsValue(i []int) Value { return Value{ - value: i, - vtype: TypeInt | TypeArray, + value: i, + vtype: TypeInt | TypeArray, + numeric: 0, + stringly: "", } } @@ -365,15 +390,19 @@ func intpValue(in *int) Value { func int8Value(i int8) Value { return Value{ - vtype: TypeInt8, - numeric: uint64(i), + vtype: TypeInt8, + numeric: uint64(i), + value: nil, + stringly: "", } } func int8sValue(i []int8) Value { return Value{ - value: i, - vtype: TypeInt8 | TypeArray, + value: i, + vtype: TypeInt8 | TypeArray, + numeric: 0, + stringly: "", } } @@ -387,15 +416,19 @@ func int8pValue(in *int8) Value { func int16Value(i int16) Value { return Value{ - vtype: TypeInt16, - numeric: uint64(i), + vtype: TypeInt16, + numeric: uint64(i), + value: 0, + stringly: "", } } func int16sValue(i []int16) Value { return Value{ - value: i, - vtype: TypeInt16 | TypeArray, + value: i, + vtype: TypeInt16 | TypeArray, + numeric: 0, + stringly: "", } } @@ -409,15 +442,19 @@ func int16pValue(in *int16) Value { func int32Value(i int32) Value { return Value{ - vtype: TypeInt32, - numeric: uint64(i), + vtype: TypeInt32, + numeric: uint64(i), + value: nil, + stringly: "", } } func int32sValue(i []int32) Value { return Value{ - value: i, - vtype: TypeInt32 | TypeArray, + value: i, + vtype: TypeInt32 | TypeArray, + numeric: 0, + stringly: "", } } @@ -431,15 +468,19 @@ func int32pValue(in *int32) Value { func int64Value(i int64) Value { return Value{ - vtype: TypeInt64, - numeric: uint64(i), + vtype: TypeInt64, + numeric: uint64(i), + value: nil, + stringly: "", } } func int64sValue(i []int64) Value { return Value{ - value: i, - vtype: TypeInt64 | TypeArray, + value: i, + vtype: TypeInt64 | TypeArray, + numeric: 0, + stringly: "", } } @@ -453,15 +494,19 @@ func int64pValue(in *int64) Value { func uintValue(i uint) Value { return Value{ - vtype: TypeUint, - numeric: uint64(i), + vtype: TypeUint, + numeric: uint64(i), + value: nil, + stringly: "", } } func uintsValue(i []uint) Value { return Value{ - value: i, - vtype: TypeUint | TypeArray, + value: i, + vtype: TypeUint | TypeArray, + numeric: 0, + stringly: "", } } @@ -475,15 +520,19 @@ func uintpValue(in *uint) Value { func uint8Value(i uint8) Value { return Value{ - vtype: TypeUint8, - numeric: uint64(i), + vtype: TypeUint8, + numeric: uint64(i), + value: nil, + stringly: "", } } func uint8sValue(i []uint8) Value { return Value{ - value: i, - vtype: TypeUint8 | TypeArray, + value: i, + vtype: TypeUint8 | TypeArray, + numeric: 0, + stringly: "", } } @@ -497,15 +546,19 @@ func uint8pValue(in *uint8) Value { func uint16Value(i uint16) Value { return Value{ - vtype: TypeUint16, - numeric: uint64(i), + vtype: TypeUint16, + numeric: uint64(i), + value: nil, + stringly: "", } } func uint16sValue(i []uint16) Value { return Value{ - value: i, - vtype: TypeUint16 | TypeArray, + value: i, + vtype: TypeUint16 | TypeArray, + numeric: 0, + stringly: "", } } @@ -519,15 +572,19 @@ func uint16pValue(in *uint16) Value { func uint32Value(i uint32) Value { return Value{ - vtype: TypeUint32, - numeric: uint64(i), + vtype: TypeUint32, + numeric: uint64(i), + value: nil, + stringly: "", } } func uint32sValue(i []uint32) Value { return Value{ - value: i, - vtype: TypeUint32 | TypeArray, + value: i, + vtype: TypeUint32 | TypeArray, + numeric: 0, + stringly: "", } } @@ -541,15 +598,19 @@ func uint32pValue(in *uint32) Value { func uint64Value(i uint64) Value { return Value{ - vtype: TypeUint64, - numeric: i, + vtype: TypeUint64, + numeric: i, + value: nil, + stringly: "", } } func uint64sValue(i []uint64) Value { return Value{ - value: i, - vtype: TypeUint64 | TypeArray, + value: i, + vtype: TypeUint64 | TypeArray, + numeric: 0, + stringly: "", } } @@ -563,15 +624,19 @@ func uint64pValue(in *uint64) Value { func float32Value(i float32) Value { return Value{ - vtype: TypeFloat32, - numeric: uint64(math.Float32bits(i)), + vtype: TypeFloat32, + numeric: uint64(math.Float32bits(i)), + value: nil, + stringly: "", } } func float32sValue(i []float32) Value { return Value{ - value: i, - vtype: TypeFloat32 | TypeArray, + value: i, + vtype: TypeFloat32 | TypeArray, + numeric: 0, + stringly: "", } } @@ -585,15 +650,19 @@ func float32pValue(in *float32) Value { func float64Value(i float64) Value { return Value{ - vtype: TypeFloat64, - numeric: math.Float64bits(i), + vtype: TypeFloat64, + numeric: math.Float64bits(i), + value: nil, + stringly: "", } } func float64sValue(i []float64) Value { return Value{ - value: i, - vtype: TypeFloat64 | TypeArray, + value: i, + vtype: TypeFloat64 | TypeArray, + numeric: 0, + stringly: "", } } @@ -607,15 +676,19 @@ func float64pValue(in *float64) Value { func complex64Value(in complex64) Value { return Value{ - vtype: TypeComplex64, - value: in, + vtype: TypeComplex64, + value: in, + numeric: 0, + stringly: "", } } func complex64sValue(in []complex64) Value { return Value{ - vtype: TypeComplex64 | TypeArray, - value: in, + vtype: TypeComplex64 | TypeArray, + value: in, + numeric: 0, + stringly: "", } } @@ -629,15 +702,19 @@ func complex64pValue(in *complex64) Value { func complex128Value(in complex128) Value { return Value{ - vtype: TypeComplex64, - value: in, + vtype: TypeComplex64, + value: in, + numeric: 0, + stringly: "", } } func complex128sValue(in []complex128) Value { return Value{ - vtype: TypeComplex128 | TypeArray, - value: in, + vtype: TypeComplex128 | TypeArray, + value: in, + numeric: 0, + stringly: "", } } @@ -651,15 +728,19 @@ func complex128pValue(in *complex128) Value { func uintptrValue(in uintptr) Value { return Value{ - vtype: TypeUintptr, - value: in, + vtype: TypeUintptr, + numeric: 0, + stringly: "", + value: in, } } func uintptrsValue(in []uintptr) Value { return Value{ - vtype: TypeUintptr | TypeArray, - value: in, + vtype: TypeUintptr | TypeArray, + value: in, + numeric: 0, + stringly: "", } } @@ -673,22 +754,28 @@ func uintptrpValue(in *uintptr) Value { func bytesValue(in []byte) Value { return Value{ - vtype: TypeBinary, - value: in, + vtype: TypeBinary, + value: in, + numeric: 0, + stringly: "", } } func durationValue(in time.Duration) Value { return Value{ - vtype: TypeDuration, - value: in, + vtype: TypeDuration, + value: in, + numeric: 0, + stringly: "", } } func durationsValue(in []time.Duration) Value { return Value{ - vtype: TypeDuration | TypeArray, - value: in, + vtype: TypeDuration | TypeArray, + value: in, + numeric: 0, + stringly: "", } } @@ -717,6 +804,7 @@ func formatTimeValue(format string, in time.Time) Value { vtype: TypeTime, value: in, stringly: format, + numeric: 0, } } @@ -725,6 +813,7 @@ func formatTimesValue(format string, in []time.Time) Value { vtype: TypeTime | TypeArray, value: in, stringly: format, + numeric: 0, } } @@ -739,8 +828,10 @@ func formatTimepValue(format string, in *time.Time) Value { func errorValue(in error) Value { if in != nil { return Value{ - vtype: TypeError, - value: in, + vtype: TypeError, + value: in, + numeric: 0, + stringly: "", } } @@ -749,7 +840,9 @@ func errorValue(in error) Value { func errorsValue(in []error) Value { return Value{ - vtype: TypeError | TypeArray, - value: in, + vtype: TypeError | TypeArray, + value: in, + numeric: 0, + stringly: "", } } diff --git a/handler/logrus/logger.go b/handler/logrus/logger.go index 21999fb..7e1942a 100644 --- a/handler/logrus/logger.go +++ b/handler/logrus/logger.go @@ -21,20 +21,22 @@ func New(log *logrus.Logger) log.Logger { for _, field := range e.Fields() { lrgFields[string(field.Key())] = field.AsInterface() } - write := log.WithContext(ctx).WithFields(lrgFields) + + entry := log.WithContext(ctx).WithFields(lrgFields) + switch e.Level() { case level.Emergency: - write.Panic(e.Message()) + entry.Panic(e.Message()) case level.Alert: - write.Fatal(e.Message()) + entry.Fatal(e.Message()) case level.Critical, level.Error: - write.Error(e.Message()) + entry.Error(e.Message()) case level.Warning: - write.Warn(e.Message()) + entry.Warn(e.Message()) case level.Notice, level.Info: - write.Info(e.Message()) + entry.Info(e.Message()) case level.Debug: - write.Debug(e.Message()) + entry.Debug(e.Message()) } return 0, nil diff --git a/handler/logrus/logger_test.go b/handler/logrus/logger_test.go index f818e34..2dbc3dd 100644 --- a/handler/logrus/logger_test.go +++ b/handler/logrus/logger_test.go @@ -13,6 +13,8 @@ import ( ) func TestNew(t *testing.T) { + t.Parallel() + ctx := context.Background() buf := &bytes.Buffer{} diff --git a/handler/zap/logger_test.go b/handler/zap/logger_test.go index f6de757..88e9907 100644 --- a/handler/zap/logger_test.go +++ b/handler/zap/logger_test.go @@ -14,6 +14,8 @@ import ( ) func TestNew(t *testing.T) { + t.Parallel() + ctx := context.Background() buf := &bytes.Buffer{} core := zapcore.NewCore(zapcore.NewJSONEncoder(zapcore.EncoderConfig{ diff --git a/level/level.go b/level/level.go index 6ce25a7..975fa2f 100644 --- a/level/level.go +++ b/level/level.go @@ -2,6 +2,7 @@ package level import ( "encoding/json" + "fmt" "strings" ) @@ -28,7 +29,12 @@ const ( ) func (l Level) MarshalJSON() ([]byte, error) { - return json.Marshal(l.String()) + b, err := json.Marshal(l.String()) + if err != nil { + return nil, fmt.Errorf("marshal err: %w", err) + } + + return b, nil } func (l Level) Is(level Level) bool { @@ -42,7 +48,7 @@ func (l Level) Enabled(level Level) bool { func (l *Level) UnmarshalJSON(in []byte) error { var v string if err := json.Unmarshal(in, &v); err != nil { - return err + return fmt.Errorf("unmarshal err: %w", err) } lvl := Parse(v) diff --git a/logger_test.go b/logger_test.go index b638402..a3752a5 100644 --- a/logger_test.go +++ b/logger_test.go @@ -17,6 +17,8 @@ import ( var requestID ctxKey = "requestID" func TestFields(t *testing.T) { + t.Parallel() + type rObj struct { id string } @@ -58,6 +60,8 @@ func TestFields(t *testing.T) { } func TestWriter(t *testing.T) { + t.Parallel() + ctx := context.Background() success := "msg=\"info message\" err=file already exists requestID=6a5fa048-7181-11ea-bc55-0242ac1311113 level=info\n" @@ -84,6 +88,8 @@ func TestWriter(t *testing.T) { } func TestLogger(t *testing.T) { + t.Parallel() + ctx := context.Background() buf := &bytes.Buffer{} logger := log.New(log.WithWriter(buf)).With(log.WithContextValue(requestID), log.WithLevel("level", level.Info)) diff --git a/writter.go b/writter.go index 3b2b78c..9c069d9 100644 --- a/writter.go +++ b/writter.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "fmt" "io" "os" "strings" @@ -23,10 +24,15 @@ func New(opts ...Option) Logger { return func(_ context.Context, entry *entry.Entry) (int, error) { b, err := l.e(entry) if err != nil { - return 0, err + return 0, fmt.Errorf("enode err: %w", err) } - return l.w.Write(b) + n, err := l.w.Write(b) + if err != nil { + return 0, fmt.Errorf("failed write: %w", err) + } + + return n, nil } } @@ -70,6 +76,7 @@ func WithJSONFormat() Option { return WithEncode(jsonFormat) } +//nolint: forcetypeassert func stringFormat() func(entry *entry.Entry) ([]byte, error) { pool := sync.Pool{ New: func() interface{} { @@ -105,7 +112,7 @@ func stringFormat() func(entry *entry.Entry) ([]byte, error) { func jsonFormat(entry *entry.Entry) ([]byte, error) { res, err := json.Marshal(entry.AddString("msg", entry.Message()).Fields().AsMap()) if err != nil { - return nil, err + return nil, fmt.Errorf("marshal err: %w", err) } return append(res, []byte("\n")...), nil