Skip to content

Commit

Permalink
optimize span log
Browse files Browse the repository at this point in the history
  • Loading branch information
yinheli committed Jul 7, 2021
1 parent c7f49e6 commit 93e6416
Showing 1 changed file with 40 additions and 88 deletions.
128 changes: 40 additions & 88 deletions pkg/log/spanlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package log

import (
"context"
"fmt"
"math"
"time"

"github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -73,95 +75,45 @@ func (t spanLogger) CallerSkip(skip int) Logger {
}

func (t spanLogger) logToSpan(level string, msg string, fields ...zapcore.Field) {
fa := fieldAdapter(make([]log.Field, 0, 2+len(fields)))
fa = append(fa, log.String("msg", msg))
fa = append(fa, log.String("level", level))
fs := make([]log.Field, 0, 2+len(fields))
fs = append(fs, log.String("msg", msg))
fs = append(fs, log.String("level", level))
for _, field := range fields {
field.AddTo(&fa)
fs = append(fs, zapFieldToLogField(field))
}
t.span.LogFields(fa...)
}

type fieldAdapter []log.Field

func (fa *fieldAdapter) AddBool(key string, value bool) {
*fa = append(*fa, log.Bool(key, value))
}

func (fa *fieldAdapter) AddFloat64(key string, value float64) {
*fa = append(*fa, log.Float64(key, value))
}

func (fa *fieldAdapter) AddFloat32(key string, value float32) {
*fa = append(*fa, log.Float64(key, float64(value)))
}

func (fa *fieldAdapter) AddInt(key string, value int) {
*fa = append(*fa, log.Int(key, value))
}

func (fa *fieldAdapter) AddInt64(key string, value int64) {
*fa = append(*fa, log.Int64(key, value))
}

func (fa *fieldAdapter) AddInt32(key string, value int32) {
*fa = append(*fa, log.Int64(key, int64(value)))
}

func (fa *fieldAdapter) AddInt16(key string, value int16) {
*fa = append(*fa, log.Int64(key, int64(value)))
}

func (fa *fieldAdapter) AddInt8(key string, value int8) {
*fa = append(*fa, log.Int64(key, int64(value)))
}

func (fa *fieldAdapter) AddUint(key string, value uint) {
*fa = append(*fa, log.Uint64(key, uint64(value)))
}

func (fa *fieldAdapter) AddUint64(key string, value uint64) {
*fa = append(*fa, log.Uint64(key, value))
}

func (fa *fieldAdapter) AddUint32(key string, value uint32) {
*fa = append(*fa, log.Uint64(key, uint64(value)))
}

func (fa *fieldAdapter) AddUint16(key string, value uint16) {
*fa = append(*fa, log.Uint64(key, uint64(value)))
}

func (fa *fieldAdapter) AddUint8(key string, value uint8) {
*fa = append(*fa, log.Uint64(key, uint64(value)))
}

func (fa *fieldAdapter) AddUintptr(key string, value uintptr) {}
func (fa *fieldAdapter) AddArray(key string, marshaler zapcore.ArrayMarshaler) error { return nil }
func (fa *fieldAdapter) AddComplex128(key string, value complex128) {}
func (fa *fieldAdapter) AddComplex64(key string, value complex64) {}
func (fa *fieldAdapter) AddObject(key string, value zapcore.ObjectMarshaler) error { return nil }
func (fa *fieldAdapter) AddReflected(key string, value interface{}) error { return nil }
func (fa *fieldAdapter) OpenNamespace(key string) {}

func (fa *fieldAdapter) AddDuration(key string, value time.Duration) {
*fa = append(*fa, log.String(key, value.String()))
}

func (fa *fieldAdapter) AddTime(key string, value time.Time) {
*fa = append(*fa, log.String(key, value.String()))
}

func (fa *fieldAdapter) AddBinary(key string, value []byte) {
*fa = append(*fa, log.Object(key, value))
}

func (fa *fieldAdapter) AddByteString(key string, value []byte) {
*fa = append(*fa, log.Object(key, value))
}

func (fa *fieldAdapter) AddString(key, value string) {
if key != "" && value != "" {
*fa = append(*fa, log.String(key, value))
t.span.LogFields(fs...)
}

// zapFieldToLogField to opentracing log field
func zapFieldToLogField(field zapcore.Field) log.Field {
switch field.Type {
case zapcore.BoolType:
val := false
if field.Integer >= 1 {
val = true
}
return log.Bool(field.Key, val)
case zapcore.Float32Type:
return log.Float32(field.Key, math.Float32frombits(uint32(field.Integer)))
case zapcore.Float64Type:
return log.Float64(field.Key, math.Float64frombits(uint64(field.Integer)))
case zapcore.Int64Type:
return log.Int64(field.Key, int64(field.Integer))
case zapcore.Int32Type:
return log.Int32(field.Key, int32(field.Integer))
case zapcore.StringType:
return log.String(field.Key, field.String)
case zapcore.StringerType:
return log.String(field.Key, field.Interface.(fmt.Stringer).String())
case zapcore.Uint64Type:
return log.Uint64(field.Key, uint64(field.Integer))
case zapcore.Uint32Type:
return log.Uint32(field.Key, uint32(field.Integer))
case zapcore.DurationType:
return log.String(field.Key, time.Duration(field.Integer).String())
case zapcore.ErrorType:
return log.Error(field.Interface.(error))
default:
return log.Object(field.Key, field.Interface)
}
}

0 comments on commit 93e6416

Please sign in to comment.