1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-24 03:46:37 +02:00
kratos/pkg/log/util.go

70 lines
1.8 KiB
Go
Raw Normal View History

2019-03-06 19:11:45 +08:00
package log
import (
"context"
2019-04-11 19:55:27 +08:00
"math"
2019-03-06 19:11:45 +08:00
"runtime"
"strconv"
2019-04-11 19:55:27 +08:00
"time"
2019-03-06 19:11:45 +08:00
2019-04-11 15:15:11 +08:00
"github.com/bilibili/kratos/pkg/conf/env"
2019-04-11 19:55:27 +08:00
"github.com/bilibili/kratos/pkg/log/internal/core"
2019-04-11 15:15:11 +08:00
"github.com/bilibili/kratos/pkg/net/metadata"
"github.com/bilibili/kratos/pkg/net/trace"
2019-03-06 19:11:45 +08:00
)
func addExtraField(ctx context.Context, fields map[string]interface{}) {
if t, ok := trace.FromContext(ctx); ok {
2019-06-09 20:57:28 +08:00
fields[_tid] = t.TraceID()
2019-03-06 19:11:45 +08:00
}
if caller := metadata.String(ctx, metadata.Caller); caller != "" {
fields[_caller] = caller
}
if color := metadata.String(ctx, metadata.Color); color != "" {
fields[_color] = color
}
2019-06-09 20:57:28 +08:00
if env.Color != "" {
fields[_envColor] = env.Color
}
2019-03-06 19:11:45 +08:00
if cluster := metadata.String(ctx, metadata.Cluster); cluster != "" {
fields[_cluster] = cluster
}
fields[_deplyEnv] = env.DeployEnv
fields[_zone] = env.Zone
2019-06-09 20:57:28 +08:00
fields[_appID] = c.Family
2019-03-06 19:11:45 +08:00
fields[_instanceID] = c.Host
2019-06-09 20:57:28 +08:00
if metadata.String(ctx, metadata.Mirror) != "" {
2019-03-06 19:11:45 +08:00
fields[_mirror] = true
}
}
2019-06-09 20:57:28 +08:00
// funcName get func name.
func funcName(skip int) (name string) {
if _, file, lineNo, ok := runtime.Caller(skip); ok {
return file + ":" + strconv.Itoa(lineNo)
}
return "unknown:0"
}
2019-04-11 19:55:27 +08:00
// toMap convert D slice to map[string]interface{} for legacy file and stdout.
func toMap(args ...D) map[string]interface{} {
d := make(map[string]interface{}, 10+len(args))
for _, arg := range args {
switch arg.Type {
2019-06-09 20:57:28 +08:00
case core.UintType, core.Uint64Type, core.IntTpye, core.Int64Type:
2019-04-11 19:55:27 +08:00
d[arg.Key] = arg.Int64Val
case core.StringType:
d[arg.Key] = arg.StringVal
case core.Float32Type:
d[arg.Key] = math.Float32frombits(uint32(arg.Int64Val))
case core.Float64Type:
d[arg.Key] = math.Float64frombits(uint64(arg.Int64Val))
case core.DurationType:
d[arg.Key] = time.Duration(arg.Int64Val)
default:
d[arg.Key] = arg.Value
}
}
return d
}