Browse Source

update linter

pull/1/head
andrey1s 3 years ago
parent
commit
4323f5e3b3
  1. 14
      .golangci.yml
  2. 5
      entry/entry.go
  3. 1
      entry/pool.go
  4. 2
      field/field.go
  5. 4
      field/key.go
  6. 101
      field/value.go
  7. 16
      handler/logrus/logger.go
  8. 2
      handler/logrus/logger_test.go
  9. 2
      handler/zap/logger_test.go
  10. 10
      level/level.go
  11. 6
      logger_test.go
  12. 13
      writter.go

14
.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

5
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 {

1
entry/pool.go

@ -9,6 +9,7 @@ var pool = sync.Pool{
},
}
//nolint: forcetypeassert
func Get() *Entry {
e := pool.Get().(*Entry)
e.Reset()

2
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)

4
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:
@ -122,6 +122,8 @@ func (k Key) Any(value interface{}) Field {
value: Value{
value: value,
vtype: TypeAny,
numeric: 0,
stringly: "",
},
}
}

101
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,13 +293,20 @@ 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,
}
}
@ -302,6 +314,8 @@ func stringsValue(v []string) Value {
return Value{
value: v,
vtype: TypeString | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -318,11 +332,16 @@ func boolValue(b bool) Value {
return Value{
numeric: 1,
vtype: TypeBool,
value: nil,
stringly: "",
}
}
return Value{
vtype: TypeBool,
value: nil,
numeric: 0,
stringly: "",
}
}
@ -330,6 +349,8 @@ func boolsValue(b []bool) Value {
return Value{
value: b,
vtype: TypeBool | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -345,6 +366,8 @@ func intValue(i int) Value {
return Value{
vtype: TypeInt,
numeric: uint64(i),
value: nil,
stringly: "",
}
}
@ -352,6 +375,8 @@ func intsValue(i []int) Value {
return Value{
value: i,
vtype: TypeInt | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -367,6 +392,8 @@ func int8Value(i int8) Value {
return Value{
vtype: TypeInt8,
numeric: uint64(i),
value: nil,
stringly: "",
}
}
@ -374,6 +401,8 @@ func int8sValue(i []int8) Value {
return Value{
value: i,
vtype: TypeInt8 | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -389,6 +418,8 @@ func int16Value(i int16) Value {
return Value{
vtype: TypeInt16,
numeric: uint64(i),
value: 0,
stringly: "",
}
}
@ -396,6 +427,8 @@ func int16sValue(i []int16) Value {
return Value{
value: i,
vtype: TypeInt16 | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -411,6 +444,8 @@ func int32Value(i int32) Value {
return Value{
vtype: TypeInt32,
numeric: uint64(i),
value: nil,
stringly: "",
}
}
@ -418,6 +453,8 @@ func int32sValue(i []int32) Value {
return Value{
value: i,
vtype: TypeInt32 | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -433,6 +470,8 @@ func int64Value(i int64) Value {
return Value{
vtype: TypeInt64,
numeric: uint64(i),
value: nil,
stringly: "",
}
}
@ -440,6 +479,8 @@ func int64sValue(i []int64) Value {
return Value{
value: i,
vtype: TypeInt64 | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -455,6 +496,8 @@ func uintValue(i uint) Value {
return Value{
vtype: TypeUint,
numeric: uint64(i),
value: nil,
stringly: "",
}
}
@ -462,6 +505,8 @@ func uintsValue(i []uint) Value {
return Value{
value: i,
vtype: TypeUint | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -477,6 +522,8 @@ func uint8Value(i uint8) Value {
return Value{
vtype: TypeUint8,
numeric: uint64(i),
value: nil,
stringly: "",
}
}
@ -484,6 +531,8 @@ func uint8sValue(i []uint8) Value {
return Value{
value: i,
vtype: TypeUint8 | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -499,6 +548,8 @@ func uint16Value(i uint16) Value {
return Value{
vtype: TypeUint16,
numeric: uint64(i),
value: nil,
stringly: "",
}
}
@ -506,6 +557,8 @@ func uint16sValue(i []uint16) Value {
return Value{
value: i,
vtype: TypeUint16 | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -521,6 +574,8 @@ func uint32Value(i uint32) Value {
return Value{
vtype: TypeUint32,
numeric: uint64(i),
value: nil,
stringly: "",
}
}
@ -528,6 +583,8 @@ func uint32sValue(i []uint32) Value {
return Value{
value: i,
vtype: TypeUint32 | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -543,6 +600,8 @@ func uint64Value(i uint64) Value {
return Value{
vtype: TypeUint64,
numeric: i,
value: nil,
stringly: "",
}
}
@ -550,6 +609,8 @@ func uint64sValue(i []uint64) Value {
return Value{
value: i,
vtype: TypeUint64 | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -565,6 +626,8 @@ func float32Value(i float32) Value {
return Value{
vtype: TypeFloat32,
numeric: uint64(math.Float32bits(i)),
value: nil,
stringly: "",
}
}
@ -572,6 +635,8 @@ func float32sValue(i []float32) Value {
return Value{
value: i,
vtype: TypeFloat32 | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -587,6 +652,8 @@ func float64Value(i float64) Value {
return Value{
vtype: TypeFloat64,
numeric: math.Float64bits(i),
value: nil,
stringly: "",
}
}
@ -594,6 +661,8 @@ func float64sValue(i []float64) Value {
return Value{
value: i,
vtype: TypeFloat64 | TypeArray,
numeric: 0,
stringly: "",
}
}
@ -609,6 +678,8 @@ func complex64Value(in complex64) Value {
return Value{
vtype: TypeComplex64,
value: in,
numeric: 0,
stringly: "",
}
}
@ -616,6 +687,8 @@ func complex64sValue(in []complex64) Value {
return Value{
vtype: TypeComplex64 | TypeArray,
value: in,
numeric: 0,
stringly: "",
}
}
@ -631,6 +704,8 @@ func complex128Value(in complex128) Value {
return Value{
vtype: TypeComplex64,
value: in,
numeric: 0,
stringly: "",
}
}
@ -638,6 +713,8 @@ func complex128sValue(in []complex128) Value {
return Value{
vtype: TypeComplex128 | TypeArray,
value: in,
numeric: 0,
stringly: "",
}
}
@ -652,6 +729,8 @@ func complex128pValue(in *complex128) Value {
func uintptrValue(in uintptr) Value {
return Value{
vtype: TypeUintptr,
numeric: 0,
stringly: "",
value: in,
}
}
@ -660,6 +739,8 @@ func uintptrsValue(in []uintptr) Value {
return Value{
vtype: TypeUintptr | TypeArray,
value: in,
numeric: 0,
stringly: "",
}
}
@ -675,6 +756,8 @@ func bytesValue(in []byte) Value {
return Value{
vtype: TypeBinary,
value: in,
numeric: 0,
stringly: "",
}
}
@ -682,6 +765,8 @@ func durationValue(in time.Duration) Value {
return Value{
vtype: TypeDuration,
value: in,
numeric: 0,
stringly: "",
}
}
@ -689,6 +774,8 @@ func durationsValue(in []time.Duration) Value {
return Value{
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,
}
}
@ -741,6 +830,8 @@ func errorValue(in error) Value {
return Value{
vtype: TypeError,
value: in,
numeric: 0,
stringly: "",
}
}
@ -751,5 +842,7 @@ func errorsValue(in []error) Value {
return Value{
vtype: TypeError | TypeArray,
value: in,
numeric: 0,
stringly: "",
}
}

16
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

2
handler/logrus/logger_test.go

@ -13,6 +13,8 @@ import (
)
func TestNew(t *testing.T) {
t.Parallel()
ctx := context.Background()
buf := &bytes.Buffer{}

2
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{

10
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)

6
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))

13
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

Loading…
Cancel
Save