代码拉取完成,页面将自动刷新
package logger
import (
"fmt"
"github.com/uptrace/opentelemetry-go-extra/otelutil"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.uber.org/zap/zapcore"
"math"
"reflect"
"strconv"
"time"
)
func appendField(attrs []attribute.KeyValue, f zapcore.Field) []attribute.KeyValue {
switch f.Type {
case zapcore.BoolType:
attr := attribute.Bool(f.Key, f.Integer == 1)
return append(attrs, attr)
case zapcore.Int8Type, zapcore.Int16Type, zapcore.Int32Type, zapcore.Int64Type,
zapcore.Uint32Type, zapcore.Uint8Type, zapcore.Uint16Type, zapcore.Uint64Type,
zapcore.UintptrType:
attr := attribute.Int64(f.Key, f.Integer)
return append(attrs, attr)
case zapcore.Float32Type, zapcore.Float64Type:
attr := attribute.Float64(f.Key, math.Float64frombits(uint64(f.Integer)))
return append(attrs, attr)
case zapcore.Complex64Type:
s := strconv.FormatComplex(complex128(f.Interface.(complex64)), 'E', -1, 64)
attr := attribute.String(f.Key, s)
return append(attrs, attr)
case zapcore.Complex128Type:
s := strconv.FormatComplex(f.Interface.(complex128), 'E', -1, 128)
attr := attribute.String(f.Key, s)
return append(attrs, attr)
case zapcore.StringType:
attr := attribute.String(f.Key, f.String)
return append(attrs, attr)
case zapcore.BinaryType, zapcore.ByteStringType:
attr := attribute.String(f.Key, string(f.Interface.([]byte)))
return append(attrs, attr)
case zapcore.StringerType:
attr := attribute.String(f.Key, f.Interface.(fmt.Stringer).String())
return append(attrs, attr)
case zapcore.DurationType, zapcore.TimeType:
attr := attribute.Int64(f.Key, f.Integer)
return append(attrs, attr)
case zapcore.TimeFullType:
attr := attribute.Int64(f.Key, f.Interface.(time.Time).UnixNano())
return append(attrs, attr)
case zapcore.ErrorType:
err := f.Interface.(error)
typ := reflect.TypeOf(err).String()
attrs = append(attrs, semconv.ExceptionTypeKey.String(typ))
attrs = append(attrs, semconv.ExceptionMessageKey.String(err.Error()))
return attrs
case zapcore.ReflectType:
attr := otelutil.Attribute(f.Key, f.Interface)
return append(attrs, attr)
case zapcore.SkipType:
return attrs
case zapcore.ArrayMarshalerType:
var attr attribute.KeyValue
arrayEncoder := &bufferArrayEncoder{
stringsSlice: []string{},
}
err := f.Interface.(zapcore.ArrayMarshaler).MarshalLogArray(arrayEncoder)
if err != nil {
attr = attribute.String(f.Key+"_error", fmt.Sprintf("otelzap: unable to marshal array: %v", err))
} else {
attr = attribute.StringSlice(f.Key, arrayEncoder.stringsSlice)
}
return append(attrs, attr)
case zapcore.ObjectMarshalerType:
attr := attribute.String(f.Key+"_error", "otelzap: zapcore.ObjectMarshalerType is not implemented")
return append(attrs, attr)
default:
attr := attribute.String(f.Key+"_error", fmt.Sprintf("otelzap: unknown field type: %v", f))
return append(attrs, attr)
}
}
func levelString(lvl zapcore.Level) string {
if lvl == zapcore.DPanicLevel {
return "PANIC"
}
return lvl.CapitalString()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。