fix log field (#3)
parent
f98cd0a5c0
commit
d0b98db00c
@ -0,0 +1,58 @@ |
||||
package log |
||||
|
||||
import ( |
||||
"math" |
||||
"time" |
||||
|
||||
"github.com/bilibili/Kratos/pkg/log/internal/core" |
||||
) |
||||
|
||||
// D represents a map of entry level data used for structured logging.
|
||||
// type D map[string]interface{}
|
||||
type D = core.Field |
||||
|
||||
// KVString construct Field with string value.
|
||||
func KVString(key string, value string) D { |
||||
return D{Key: key, Type: core.StringType, StringVal: value} |
||||
} |
||||
|
||||
// KVInt construct Field with int value.
|
||||
func KVInt(key string, value int) D { |
||||
return D{Key: key, Type: core.IntTpye, Int64Val: int64(value)} |
||||
} |
||||
|
||||
// KVInt64 construct D with int64 value.
|
||||
func KVInt64(key string, value int64) D { |
||||
return D{Key: key, Type: core.Int64Type, Int64Val: value} |
||||
} |
||||
|
||||
// KVUint construct Field with uint value.
|
||||
func KVUint(key string, value uint) D { |
||||
return D{Key: key, Type: core.UintType, Int64Val: int64(value)} |
||||
} |
||||
|
||||
// KVUint64 construct Field with uint64 value.
|
||||
func KVUint64(key string, value uint64) D { |
||||
return D{Key: key, Type: core.Uint64Type, Int64Val: int64(value)} |
||||
} |
||||
|
||||
// KVFloat32 construct Field with float32 value.
|
||||
func KVFloat32(key string, value float32) D { |
||||
return D{Key: key, Type: core.Float32Type, Int64Val: int64(math.Float32bits(value))} |
||||
} |
||||
|
||||
// KVFloat64 construct Field with float64 value.
|
||||
func KVFloat64(key string, value float64) D { |
||||
return D{Key: key, Type: core.Float64Type, Int64Val: int64(math.Float64bits(value))} |
||||
} |
||||
|
||||
// KVDuration construct Field with Duration value.
|
||||
func KVDuration(key string, value time.Duration) D { |
||||
return D{Key: key, Type: core.DurationType, Int64Val: int64(value)} |
||||
} |
||||
|
||||
// KV return a log kv for logging field.
|
||||
// NOTE: use KV{type name} can avoid object alloc and get better performance. []~( ̄▽ ̄)~*干杯
|
||||
func KV(key string, value interface{}) D { |
||||
return D{Key: key, Value: value} |
||||
} |
@ -0,0 +1,122 @@ |
||||
package core |
||||
|
||||
import ( |
||||
"fmt" |
||||
"math" |
||||
"time" |
||||
|
||||
xtime "github.com/bilibili/Kratos/pkg/time" |
||||
) |
||||
|
||||
// FieldType represent D value type
|
||||
type FieldType int32 |
||||
|
||||
// DType enum
|
||||
const ( |
||||
UnknownType FieldType = iota |
||||
StringType |
||||
IntTpye |
||||
Int64Type |
||||
UintType |
||||
Uint64Type |
||||
Float32Type |
||||
Float64Type |
||||
DurationType |
||||
) |
||||
|
||||
// Field is for encoder
|
||||
type Field struct { |
||||
Key string |
||||
Value interface{} |
||||
Type FieldType |
||||
StringVal string |
||||
Int64Val int64 |
||||
} |
||||
|
||||
// AddTo exports a field through the ObjectEncoder interface. It's primarily
|
||||
// useful to library authors, and shouldn't be necessary in most applications.
|
||||
func (f Field) AddTo(enc ObjectEncoder) { |
||||
if f.Type == UnknownType { |
||||
f.assertAddTo(enc) |
||||
return |
||||
} |
||||
switch f.Type { |
||||
case StringType: |
||||
enc.AddString(f.Key, f.StringVal) |
||||
case IntTpye: |
||||
enc.AddInt(f.Key, int(f.Int64Val)) |
||||
case Int64Type: |
||||
enc.AddInt64(f.Key, f.Int64Val) |
||||
case UintType: |
||||
enc.AddUint(f.Key, uint(f.Int64Val)) |
||||
case Uint64Type: |
||||
enc.AddUint64(f.Key, uint64(f.Int64Val)) |
||||
case Float32Type: |
||||
enc.AddFloat32(f.Key, math.Float32frombits(uint32(f.Int64Val))) |
||||
case Float64Type: |
||||
enc.AddFloat64(f.Key, math.Float64frombits(uint64(f.Int64Val))) |
||||
case DurationType: |
||||
enc.AddDuration(f.Key, time.Duration(f.Int64Val)) |
||||
default: |
||||
panic(fmt.Sprintf("unknown field type: %v", f)) |
||||
} |
||||
} |
||||
|
||||
func (f Field) assertAddTo(enc ObjectEncoder) { |
||||
// assert interface
|
||||
switch val := f.Value.(type) { |
||||
case bool: |
||||
enc.AddBool(f.Key, val) |
||||
case complex128: |
||||
enc.AddComplex128(f.Key, val) |
||||
case complex64: |
||||
enc.AddComplex64(f.Key, val) |
||||
case float64: |
||||
enc.AddFloat64(f.Key, val) |
||||
case float32: |
||||
enc.AddFloat32(f.Key, val) |
||||
case int: |
||||
enc.AddInt(f.Key, val) |
||||
case int64: |
||||
enc.AddInt64(f.Key, val) |
||||
case int32: |
||||
enc.AddInt32(f.Key, val) |
||||
case int16: |
||||
enc.AddInt16(f.Key, val) |
||||
case int8: |
||||
enc.AddInt8(f.Key, val) |
||||
case string: |
||||
enc.AddString(f.Key, val) |
||||
case uint: |
||||
enc.AddUint(f.Key, val) |
||||
case uint64: |
||||
enc.AddUint64(f.Key, val) |
||||
case uint32: |
||||
enc.AddUint32(f.Key, val) |
||||
case uint16: |
||||
enc.AddUint16(f.Key, val) |
||||
case uint8: |
||||
enc.AddUint8(f.Key, val) |
||||
case []byte: |
||||
enc.AddByteString(f.Key, val) |
||||
case uintptr: |
||||
enc.AddUintptr(f.Key, val) |
||||
case time.Time: |
||||
enc.AddTime(f.Key, val) |
||||
case xtime.Time: |
||||
enc.AddTime(f.Key, val.Time()) |
||||
case time.Duration: |
||||
enc.AddDuration(f.Key, val) |
||||
case xtime.Duration: |
||||
enc.AddDuration(f.Key, time.Duration(val)) |
||||
case error: |
||||
enc.AddString(f.Key, val.Error()) |
||||
case fmt.Stringer: |
||||
enc.AddString(f.Key, val.String()) |
||||
default: |
||||
err := enc.AddReflected(f.Key, val) |
||||
if err != nil { |
||||
enc.AddString(fmt.Sprintf("%sError", f.Key), err.Error()) |
||||
} |
||||
} |
||||
} |
@ -1,6 +0,0 @@ |
||||
package core |
||||
|
||||
// Field is for encoder
|
||||
type Field interface { |
||||
AddTo(enc ObjectEncoder) |
||||
} |
Loading…
Reference in new issue