1 Star 0 Fork 0

天雨流芳 / go-micro-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
types.go 3.01 KB
一键复制 编辑 原始数据 按行查看 历史
天雨流芳 提交于 2024-03-14 19:42 . 日志模块
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()
}
1
https://gitee.com/tylf2018/go-micro-framework.git
git@gitee.com:tylf2018/go-micro-framework.git
tylf2018
go-micro-framework
go-micro-framework
e87e0c3d7074

搜索帮助