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/trace_log/ctx.go

90 lines
2.4 KiB

package trace_log
import (
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/log"
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
const TraceLogKey = "trace_log"
func NewCtx(c *gin.Context, api string) context.Context {
req := &ApiInfo{ApiName: api, GinCtx: c, Funcs: &[]FuncInfo{}}
return context.WithValue(c, TraceLogKey, req)
}
func SetOperationID(ctx context.Context, operationID string) {
ctx.Value(TraceLogKey).(*ApiInfo).OperationID = operationID
}
func ShowLog(ctx context.Context) {
t := ctx.Value(TraceLogKey).(*ApiInfo)
log.Info(t.OperationID, "api: ", t.ApiName)
for _, v := range *t.Funcs {
if v.Err != nil {
log.Error(t.OperationID, "func: ", v.FuncName, " args: ", v.Args, v.Err.Error())
} else {
log.Info(t.OperationID, "func: ", v.FuncName, " args: ", v.Args)
}
}
}
func WriteErrorResponse(ctx context.Context, funcName string, err error, args ...interface{}) {
SetContextInfo(ctx, funcName, err, args)
e := new(constant.ErrInfo)
switch {
case errors.As(err, &e):
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": e.ErrCode, "errMsg": e.ErrMsg})
return
default:
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": constant.ErrDefaultOther.ErrCode, "errMsg": constant.ErrDefaultOther.ErrMsg})
return
}
}
type ApiInfo struct {
ApiName string
OperationID string
Funcs *[]FuncInfo
GinCtx *gin.Context
}
type FuncInfo struct {
FuncName string
Args map[string]interface{}
Err error
}
func SetContextInfo(ctx context.Context, funcName string, err error, args ...interface{}) {
t := ctx.Value(TraceLogKey).(*ApiInfo)
var funcInfo FuncInfo
funcInfo.Args = make(map[string]interface{})
argsHandle(args, funcInfo.Args)
funcInfo.FuncName = funcName
funcInfo.Err = err
*t.Funcs = append(*t.Funcs, funcInfo)
}
func SetSuccess(ctx context.Context, funcName string, data interface{}) {
SetContextInfo(ctx, funcName, nil, "data", data)
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": data})
}
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])] = ""
}
}
}
func GetApiErr(errCode int32, errMsg string) constant.ErrInfo {
return constant.ErrInfo{ErrCode: errCode, ErrMsg: errMsg}
}