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