You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/tracelog/ctx.go

168 lines
3.9 KiB

2 years ago
package tracelog
import (
2 years ago
"OpenIM/pkg/common/constant"
2 years ago
"OpenIM/pkg/utils"
"context"
2 years ago
"github.com/sirupsen/logrus"
"runtime"
2 years ago
"strings"
//"errors"
"fmt"
"github.com/gin-gonic/gin"
)
2 years ago
const TraceLogKey = "tracelog"
2 years ago
func SetFuncInfos(c context.Context, rootFuncName string, operationID string) context.Context {
req := &FuncInfos{RootFuncName: rootFuncName, Funcs: &[]FuncInfo{}}
ctx := context.WithValue(c, TraceLogKey, req)
SetOperationID(ctx, operationID)
return ctx
2 years ago
}
2 years ago
func NewCtx(rootFuncName string, operationID string) context.Context {
c := context.Background()
req := &FuncInfos{RootFuncName: rootFuncName, Funcs: &[]FuncInfo{}}
ctx := context.WithValue(c, TraceLogKey, req)
SetOperationID(ctx, operationID)
return ctx
}
func SetOperationID(ctx context.Context, operationID string) {
2 years ago
ctx.Value(TraceLogKey).(*FuncInfos).OperationID = operationID
}
2 years ago
func GetOperationID(ctx context.Context) string {
2 years ago
if ctx.Value(TraceLogKey) != nil {
f, ok := ctx.Value(TraceLogKey).(*FuncInfos)
if ok {
return f.OperationID
}
2 years ago
}
2 years ago
return ""
2 years ago
}
2 years ago
func GetOpUserID(ctx context.Context) string {
2 years ago
if ctx.Value(constant.OpUserID) != "" {
s, ok := ctx.Value(constant.OpUserID).(string)
if ok {
return s
}
}
return ""
2 years ago
}
2 years ago
func Unwrap(err error) error {
for err != nil {
unwrap, ok := err.(interface {
Unwrap() error
})
if !ok {
break
}
err = unwrap.Unwrap()
}
return err
}
2 years ago
type FuncInfos struct {
RootFuncName string
OperationID string
Funcs *[]FuncInfo
GinCtx *gin.Context
}
type FuncInfo struct {
FuncName string
2 years ago
Args Args
Err error
2 years ago
LogLevel logrus.Level
File string
}
2 years ago
type Args map[string]interface{}
2 years ago
func (a Args) String() string {
var s string
var hasElement bool
for k, v := range a {
if !hasElement {
s += "{"
hasElement = true
}
2 years ago
s += fmt.Sprintf("%s: %v", k, v)
2 years ago
}
if hasElement {
s += "}"
}
return s
}
2 years ago
func SetCtxDebug(ctx context.Context, funcName string, err error, args ...interface{}) {
SetContextInfo(ctx, funcName, logrus.DebugLevel, err, args)
}
func SetCtxInfo(ctx context.Context, funcName string, err error, args ...interface{}) {
SetContextInfo(ctx, funcName, logrus.InfoLevel, err, args)
}
func SetCtxWarn(ctx context.Context, funcName string, err error, args ...interface{}) {
SetContextInfo(ctx, funcName, logrus.WarnLevel, err, args)
}
func SetContextInfo(ctx context.Context, funcName string, logLevel logrus.Level, err error, args ...interface{}) {
2 years ago
var t *FuncInfos
var ok bool
if t, ok = ctx.Value(TraceLogKey).(*FuncInfos); !ok {
return
}
var funcInfo FuncInfo
funcInfo.Args = make(map[string]interface{})
argsHandle(args, funcInfo.Args)
funcInfo.FuncName = funcName
funcInfo.Err = err
2 years ago
funcInfo.LogLevel = logLevel
2 years ago
_, file, line, _ := runtime.Caller(3)
2 years ago
var s string
2 years ago
i := strings.SplitAfter(file, "/")
if len(i) > 3 {
2 years ago
s = i[len(i)-3] + i[len(i)-2] + i[len(i)-1] + ":" + utils.IntToString(line)
2 years ago
}
2 years ago
funcInfo.File = s
*t.Funcs = append(*t.Funcs, funcInfo)
}
func SetRpcReqInfo(ctx context.Context, funcName string, req string) {
2 years ago
t := ctx.Value(TraceLogKey).(*FuncInfos)
var funcInfo FuncInfo
funcInfo.Args = make(map[string]interface{})
var args []interface{}
args = append(args, " rpc req ", req)
argsHandle(args, funcInfo.Args)
funcInfo.FuncName = funcName
*t.Funcs = append(*t.Funcs, funcInfo)
}
func SetRpcRespInfo(ctx context.Context, funcName string, resp string) {
2 years ago
t := ctx.Value(TraceLogKey).(*FuncInfos)
var funcInfo FuncInfo
funcInfo.Args = make(map[string]interface{})
var args []interface{}
args = append(args, " rpc resp ", resp)
argsHandle(args, funcInfo.Args)
funcInfo.FuncName = funcName
*t.Funcs = append(*t.Funcs, funcInfo)
}
func argsHandle(args []interface{}, fields map[string]interface{}) {
for i := 0; i < len(args); i += 2 {
if i+1 < len(args) {
fields[fmt.Sprintf("%v", args[i])] = fmt.Sprintf("%+v", args[i+1])
} else {
fields[fmt.Sprintf("%v", args[i])] = ""
}
}
}